eric-maynard commented on code in PR #1348:
URL: https://github.com/apache/polaris/pull/1348#discussion_r2036178512
##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/dao/entity/BaseResult.java:
##########
@@ -57,14 +64,67 @@ public String getExtraInformation() {
return extraInformation;
}
- public boolean isSuccess() {
+ public final boolean isSuccess() {
return this.returnStatusCode == ReturnStatus.SUCCESS.getCode();
}
- public boolean alreadyExists() {
+ public final boolean alreadyExists() {
return this.returnStatusCode ==
ReturnStatus.ENTITY_ALREADY_EXISTS.getCode();
}
+ /**
+ * If this result is not a successful one, this builds an exception from the
failed result which
+ * the exception mapper can use to provide the caller some useful
information about the failure.
+ * The message relies on `extraInformation`.
+ */
+ @SuppressWarnings("FormatStringAnnotation")
+ public Optional<RuntimeException> getException() {
+ // TODO use a switch expression if the Java version here is ever raised
+ if (this.returnStatusCode == ReturnStatus.SUCCESS.getCode()) {
+ return Optional.empty();
+ } else if (this.returnStatusCode ==
ReturnStatus.UNEXPECTED_ERROR_SIGNALED.getCode()) {
+ return Optional.of(new RuntimeException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.CATALOG_PATH_CANNOT_BE_RESOLVED.getCode()) {
+ return Optional.of(new NotFoundException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.ENTITY_CANNOT_BE_RESOLVED.getCode()) {
+ return Optional.of(new NotFoundException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.ENTITY_NOT_FOUND.getCode()) {
+ return Optional.of(new NotFoundException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.GRANT_NOT_FOUND.getCode()) {
+ return Optional.of(new NotFoundException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.ENTITY_ALREADY_EXISTS.getCode()) {
+ return Optional.of(new AlreadyExistsException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.NAMESPACE_NOT_EMPTY.getCode()) {
+ return Optional.of(new
NamespaceNotEmptyException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.CATALOG_NOT_EMPTY.getCode()) {
+ return Optional.of(new BadRequestException(this.extraInformation));
+ } else if (this.returnStatusCode
+ == ReturnStatus.TARGET_ENTITY_CONCURRENTLY_MODIFIED.getCode()) {
+ return Optional.of(new CommitFailedException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.ENTITY_CANNOT_BE_RENAMED.getCode()) {
+ return Optional.of(new BadRequestException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.SUBSCOPE_CREDS_ERROR.getCode()) {
+ return Optional.of(new
UnprocessableEntityException(this.extraInformation));
+ } else if (this.returnStatusCode ==
ReturnStatus.POLICY_MAPPING_NOT_FOUND.getCode()) {
+ return Optional.of(new NotFoundException(this.extraInformation));
+ } else if (this.returnStatusCode
+ == ReturnStatus.POLICY_MAPPING_OF_SAME_TYPE_ALREADY_EXISTS.getCode()) {
+ return Optional.of(new AlreadyExistsException(this.extraInformation));
+ } else {
+ return Optional.of(new RuntimeException(this.extraInformation));
+ }
+ }
+
+ /**
+ * If this result is failed, this should throw the appropriate exception
which corresponds to the
+ * result status. See {@link BaseResult#getException} for details.
+ */
+ public void maybeThrowException() throws RuntimeException {
Review Comment:
This helper method is nice to have because you can't throw an exception from
a lambda, so you can't do e.g. `result.getException().foreach(e -> throw e)`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]