Skip to content
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

Merged
merged 20 commits into from
Jul 12, 2024

Conversation

tatu-at-datastax
Copy link
Contributor

@tatu-at-datastax tatu-at-datastax commented Jul 1, 2024

What this PR does:

Changes plumbing wrt ErrorCode and JsonApiException to allow passing HTTP response status along with exception, to indicate status other than 200 as necessary.

Meant to help resolve #1216 but does not quite achieve that.

Which issue(s) this PR fixes:
N/A

Checklist

  • Changes manually tested
  • Automated Tests added/updated
  • Documentation added/updated
  • CLA Signed: DataStax CLA

@@ -23,6 +23,8 @@ public class JsonApiException extends RuntimeException implements Supplier<Comma

private final ErrorCode errorCode;

private final Response.Status httpStatus;
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far, I like this change

@tatu-at-datastax
Copy link
Contributor Author

Looks good so far, I like this change

Yeah. If only it actually worked... having hard time figuring out where the status code is lost. :-/
(IT fails, response is stubbornly 200)

@tatu-at-datastax tatu-at-datastax marked this pull request as ready for review July 10, 2024 23:12
@tatu-at-datastax tatu-at-datastax requested a review from a team as a code owner July 10, 2024 23:12
@tatu-at-datastax tatu-at-datastax changed the title (WIP) Fixes #1216: change "unparseable" JSON into 400 from 200 (unprocessable) Fixes #1216: change "unparseable" JSON into 400 from 200 (unprocessable) Jul 10, 2024
@tatu-at-datastax tatu-at-datastax changed the title Fixes #1216: change "unparseable" JSON into 400 from 200 (unprocessable) Refactoring to allow fixing #1216 by making JsonApiException accept HTTP status code, propagate Jul 10, 2024
@@ -188,11 +190,21 @@ public JsonApiException toApiException(String format, Object... args) {
return new JsonApiException(this, message + ": " + String.format(format, args));
}

public JsonApiException toApiException(
Copy link
Contributor Author

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.

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternate factory method when the default non-templatted ErrorCode message is used.

Copy link
Contributor

@jeffreyscarpenter jeffreyscarpenter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just left one question

@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this defaults to 200?

Copy link
Contributor Author

@tatu-at-datastax tatu-at-datastax Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; it uses Response.Status from JsonApiException, which in turn defaults to Response.Status.OK (unless constructor defines something else: in this case it's called via ErrorCode.toApiException() without specifying override).

Code is quite complicated with back and forth; here I just want to remove use of getCommandResultError() override and use one associated with JsonApiException

@tatu-at-datastax tatu-at-datastax merged commit 157066b into main Jul 12, 2024
4 checks passed
@tatu-at-datastax tatu-at-datastax deleted the tatu/1216-non-json-http-400 branch July 12, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change "unparseable JSON" error to HTTP 400 (follow-up to #1200 / #1212)
3 participants