-
Notifications
You must be signed in to change notification settings - Fork 24
Ajm/fix 2318 collections driver exception handler #2333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amorton
wants to merge
11
commits into
main
Choose a base branch
from
ajm/fix-2318-collections-DriverExceptionHandler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e1164d4
WIP - not compiling, just checkpoint, before getting to tests
amorton 51ab57d
WIP - compiles, not run tests
amorton 992d1f7
FMT
amorton 09b1a93
WIP - unit tests working
amorton 0558bec
test fix
amorton d66432e
WIP - test fixes
amorton 5b35f7c
WIP test fix
amorton 3f5beeb
test fix
amorton 289013c
FMT
amorton 5f2a392
bug fix
amorton 9b75669
bug fix
amorton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 0 additions & 84 deletions
84
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/CommandError.java
This file was deleted.
Oops, something went wrong.
163 changes: 163 additions & 0 deletions
163
src/main/java/io/stargate/sgv2/jsonapi/api/model/command/CommandErrorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package io.stargate.sgv2.jsonapi.api.model.command; | ||
|
|
||
| import static io.stargate.sgv2.jsonapi.exception.ErrorFormatters.errVars; | ||
|
|
||
| import io.stargate.sgv2.jsonapi.config.DebugConfigAccess; | ||
| import io.stargate.sgv2.jsonapi.exception.APIException; | ||
| import io.stargate.sgv2.jsonapi.exception.JsonApiException; | ||
| import io.stargate.sgv2.jsonapi.exception.ServerException; | ||
| import io.stargate.sgv2.jsonapi.metrics.ExceptionMetrics; | ||
| import io.stargate.sgv2.jsonapi.service.shredding.DocRowIdentifer; | ||
| import jakarta.ws.rs.core.Response; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Builder that creates a {@link CommandErrorV2} from an {@link APIException} or the legacy {@link | ||
| * io.stargate.sgv2.jsonapi.exception.JsonApiException}. | ||
| * | ||
| * <p>This class encapsulates the mapping between the APIException and the API tier to keep it out | ||
| * of the core exception classes. <b>NOTE:</b> aaron 9-oct-2024 needed to tweak this class to work | ||
| * with the new CommandErrorV2, once we have rolled out the use of CommandErrorV2 everywhere we can | ||
| * remove the legacy CommandResult.Error | ||
| */ | ||
| public class CommandErrorFactory { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(CommandErrorFactory.class); | ||
|
|
||
| private final boolean debugEnabled; | ||
|
|
||
| public CommandErrorFactory() { | ||
| this.debugEnabled = DebugConfigAccess.isDebugEnabled(); | ||
| } | ||
|
|
||
| public CommandErrorV2 create(Throwable throwable) { | ||
| return create(throwable, Collections.emptyList()); | ||
| } | ||
|
|
||
| public CommandErrorV2 create(Throwable throwable, List<? extends DocRowIdentifer> documentIds) { | ||
| Objects.requireNonNull(throwable, "throwable cannot be null"); | ||
| return switch (throwable) { | ||
| case APIException apiException -> create(apiException, documentIds); | ||
| case JsonApiException jsonApiException -> create(jsonApiException, documentIds); | ||
| case Throwable t -> create(wrapThrowable(t), documentIds); | ||
| }; | ||
| } | ||
|
|
||
| private APIException wrapThrowable(Throwable throwable) { | ||
|
|
||
| LOGGER.warn( | ||
| "An unhandled Java exception was mapped to {}", | ||
| ServerException.Code.UNEXPECTED_SERVER_ERROR.name(), | ||
| throwable); | ||
| return ServerException.Code.UNEXPECTED_SERVER_ERROR.get(errVars(throwable)); | ||
| } | ||
|
|
||
| public CommandErrorV2 create(JsonApiException jsonApiException) { | ||
| return create(jsonApiException, Collections.emptyList()); | ||
| } | ||
|
|
||
| public CommandErrorV2 create( | ||
| JsonApiException jsonApiException, List<? extends DocRowIdentifer> documentIds) { | ||
|
|
||
| Objects.requireNonNull(jsonApiException, "jsonApiException cannot be null"); | ||
| var builder = CommandErrorV2.builder(); | ||
|
|
||
| if (debugEnabled) { | ||
| builder.exceptionClass(jsonApiException.getClass().getSimpleName()); | ||
| } | ||
|
|
||
| return builder | ||
| .errorCode(jsonApiException.getErrorCode().name()) | ||
| .message(jsonApiException.getMessage()) | ||
| .httpStatus(jsonApiException.getHttpStatus()) | ||
| .metricsTags(ExceptionMetrics.tagsFor(jsonApiException)) | ||
| .family(jsonApiException.getErrorFamily().toString()) | ||
| .scope(jsonApiException.getErrorScope().toString()) | ||
| .title(jsonApiException.getTitle()) | ||
| .id(jsonApiException.getErrorId()) | ||
| .documentIds(documentIds == null ? Collections.emptyList() : documentIds) | ||
| .build(); | ||
| } | ||
|
|
||
| public CommandErrorV2 create(APIException apiException) { | ||
| return create(apiException, Collections.emptyList()); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new instance that will create a {@link CommandErrorV2} that represents the <code> | ||
| * apiException</code>. | ||
| * | ||
| * @param apiException the exception that is going to be returned. | ||
| * @return a {@link CommandErrorV2} that represents the <code>apiException</code>. | ||
| */ | ||
| public CommandErrorV2 create( | ||
| APIException apiException, List<? extends DocRowIdentifer> documentIds) { | ||
|
|
||
| Objects.requireNonNull(apiException, "apiException cannot be null"); | ||
| var builder = CommandErrorV2.builder(); | ||
|
|
||
| if (debugEnabled) { | ||
| builder.exceptionClass(apiException.getClass().getSimpleName()); | ||
| } | ||
|
|
||
| return builder | ||
| .errorCode(apiException.code) | ||
| .message(apiException.body) | ||
| .httpStatus(Response.Status.fromStatusCode(apiException.httpStatus)) | ||
| .metricsTags(ExceptionMetrics.tagsFor(apiException)) | ||
| .family(apiException.family.name()) | ||
| .scope(apiException.scope) | ||
| .title(apiException.title) | ||
| .id(apiException.errorId) | ||
| .documentIds(documentIds == null ? Collections.emptyList() : documentIds) | ||
| .build(); | ||
| } | ||
|
|
||
| // /** | ||
| // * Create a new instance that will create a {@link CommandResult.Error} that represents the | ||
| // <code> | ||
| // * apiException</code>. | ||
| // * | ||
| // * @param apiException the exception that is going to be returned. | ||
| // * @return a {@link CommandResult.Error} that represents the <code>apiException</code>. | ||
| // */ | ||
| // public CommandResult.Error buildLegacyCommandResultError(APIException apiException) { | ||
| // // Note, in the old JsonApiException the code also traverses the cause, we do not want to do | ||
| // // that in | ||
| // // error objects V2 because the proper error is created by the template etc. | ||
| // | ||
| // // aaron - 28 aug 2024 - This should change when we improve the APi classes that handle | ||
| // errors, | ||
| // // for now have to work with what we have | ||
| // Map<String, Object> errorFields = new HashMap<>(); | ||
| // // AJM - 28 aug 2024 - for now, the CommandResult.Error checks thats message is not in the | ||
| // // fields we send | ||
| // // will fix this later, keeping this here so we can see all the things we expect to pass. | ||
| // // TODO: refactor the CommandResult.Error so it has the the V2 fields and then change how we | ||
| // // create it here | ||
| // // errorFields.put(ErrorObjectV2Constants.Fields.MESSAGE, apiException.body); | ||
| // errorFields.put(ErrorObjectV2Constants.Fields.CODE, apiException.code); | ||
| // | ||
| // if (returnErrorObjectV2) { | ||
| // errorFields.put(ErrorObjectV2Constants.Fields.FAMILY, apiException.family.name()); | ||
| // errorFields.put(ErrorObjectV2Constants.Fields.SCOPE, apiException.scope); | ||
| // errorFields.put(ErrorObjectV2Constants.Fields.TITLE, apiException.title); | ||
| // errorFields.put(ErrorObjectV2Constants.Fields.ID, apiException.errorId); | ||
| // } | ||
| // if (debugEnabled) { | ||
| // errorFields.put( | ||
| // ErrorObjectV2Constants.Fields.EXCEPTION_CLASS, | ||
| // apiException.getClass().getSimpleName()); | ||
| // } | ||
| // | ||
| // return new CommandResult.Error( | ||
| // apiException.body, | ||
| // tagsForMetrics(apiException), | ||
| // errorFields, | ||
| // Response.Status.fromStatusCode(apiException.httpStatus)); | ||
| // } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems redundant as we already have:
which calls the main
which calls Yet Another
create()method...