-
Notifications
You must be signed in to change notification settings - Fork 16
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
Refactoring to allow fixing #1216 by making JsonApiException
accept HTTP status code, propagate
#1217
Refactoring to allow fixing #1216 by making JsonApiException
accept HTTP status code, propagate
#1217
Changes from 19 commits
549806a
742eb6c
da82c47
23e2e02
a3964e9
95d7f70
aafed5b
e244a3e
b1c5424
b2ea540
7056021
6fba977
e9e09b5
32edd84
159973e
4e18d18
26c3ec0
112b413
b012c26
f466d23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package io.stargate.sgv2.jsonapi.exception; | ||
|
||
import jakarta.ws.rs.core.Response; | ||
|
||
/** ErrorCode is our internal enum that provides codes and a default message for that error code. */ | ||
public enum ErrorCode { | ||
/** Command error codes. */ | ||
|
@@ -188,11 +190,21 @@ public JsonApiException toApiException(String format, Object... args) { | |
return new JsonApiException(this, message + ": " + String.format(format, args)); | ||
} | ||
|
||
public JsonApiException toApiException( | ||
Response.Status httpStatus, String format, Object... args) { | ||
return new JsonApiException( | ||
this, message + ": " + String.format(format, args), null, httpStatus); | ||
} | ||
|
||
public JsonApiException toApiException(Throwable cause, String format, Object... args) { | ||
return new JsonApiException(this, message + ": " + String.format(format, args), cause); | ||
} | ||
|
||
public JsonApiException toApiException() { | ||
return new JsonApiException(this, message); | ||
} | ||
|
||
public JsonApiException toApiException(Response.Status httpStatus) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternate factory method when the default non-templatted ErrorCode message is used. |
||
return new JsonApiException(this, message, null, httpStatus); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ public class JsonApiException extends RuntimeException implements Supplier<Comma | |
|
||
private final ErrorCode errorCode; | ||
|
||
private final Response.Status httpStatus; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will try propagating HTTP response code along with the exception so it's neither lost nor need to be carried separately. |
||
|
||
public JsonApiException(ErrorCode errorCode) { | ||
this(errorCode, errorCode.getMessage(), null); | ||
} | ||
|
@@ -36,8 +38,14 @@ public JsonApiException(ErrorCode errorCode, Throwable cause) { | |
} | ||
|
||
public JsonApiException(ErrorCode errorCode, String message, Throwable cause) { | ||
this(errorCode, message, cause, Response.Status.OK); | ||
} | ||
|
||
public JsonApiException( | ||
ErrorCode errorCode, String message, Throwable cause, Response.Status httpStatus) { | ||
super(message, cause); | ||
this.errorCode = errorCode; | ||
this.httpStatus = httpStatus; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
|
@@ -50,7 +58,7 @@ public CommandResult get() { | |
} | ||
|
||
// construct and return | ||
CommandResult.Error error = getCommandResultError(message, Response.Status.OK); | ||
CommandResult.Error error = getCommandResultError(message, httpStatus); | ||
|
||
// handle cause as well | ||
Throwable cause = getCause(); | ||
|
@@ -86,7 +94,15 @@ public CommandResult.Error getCommandResultError(Response.Status status) { | |
return getCommandResultError(getMessage(), status); | ||
} | ||
|
||
public CommandResult.Error getCommandResultError() { | ||
return getCommandResultError(getMessage(), httpStatus); | ||
} | ||
|
||
public ErrorCode getErrorCode() { | ||
return errorCode; | ||
} | ||
|
||
public Response.Status getHttpStatus() { | ||
return httpStatus; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ public final class ThrowableToErrorMapper { | |
if (jae.getErrorCode().equals(ErrorCode.SERVER_EMBEDDING_GATEWAY_NOT_AVAILABLE)) { | ||
return jae.getCommandResultError(message, Response.Status.INTERNAL_SERVER_ERROR); | ||
} | ||
return jae.getCommandResultError(message, Response.Status.OK); | ||
return jae.getCommandResultError(message, jae.getHttpStatus()); | ||
} | ||
|
||
// General Exception related to JSON handling, thrown by Jackson | ||
|
@@ -188,8 +188,8 @@ private static CommandResult.Error handleAllNodesFailedException( | |
} else if (error instanceof DriverTimeoutException) { | ||
// [data-api#1205] Need to map DriverTimeoutException as well | ||
return ErrorCode.SERVER_DRIVER_TIMEOUT | ||
.toApiException(message) | ||
.getCommandResultError(Response.Status.INTERNAL_SERVER_ERROR); | ||
.toApiException(Response.Status.INTERNAL_SERVER_ERROR, message) | ||
.getCommandResultError(); | ||
} | ||
} | ||
} | ||
|
@@ -200,11 +200,15 @@ private static CommandResult.Error handleAllNodesFailedException( | |
|
||
private static CommandResult.Error handleJsonProcessingException( | ||
JacksonException e, String message) { | ||
// Low-level parsing problem? | ||
if (e instanceof JsonParseException) { | ||
// Low-level parsing problem? Actual BAD_REQUEST (400) since we could not process | ||
return ErrorCode.INVALID_REQUEST_NOT_JSON | ||
.toApiException("underlying problem: (%s) %s", e.getClass().getName(), message) | ||
.getCommandResultError(Response.Status.OK); | ||
.toApiException( | ||
Response.Status.BAD_REQUEST, | ||
"underlying problem: (%s) %s", | ||
e.getClass().getName(), | ||
message) | ||
.getCommandResultError(); | ||
} | ||
// Unrecognized property? (note: CommandObjectMapperHandler handles some cases) | ||
if (e instanceof UnrecognizedPropertyException upe) { | ||
|
@@ -219,7 +223,7 @@ private static CommandResult.Error handleJsonProcessingException( | |
.toApiException( | ||
"\"%s\" not one of known fields (%s) at '%s'", | ||
upe.getPropertyName(), knownDesc, upe.getPathReference()) | ||
.getCommandResultError(Response.Status.OK); | ||
.getCommandResultError(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so this defaults to 200? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes; it uses Code is quite complicated with back and forth; here I just want to remove use of |
||
} | ||
|
||
// Will need to add more handling but start with above | ||
|
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.
Factory method for passing HTTP status code along: needs to be the first argument due to varargs needed to pass format String arguments.