-
Notifications
You must be signed in to change notification settings - Fork 7
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
Make ApiUsageException return a more appropriate response code #4396
base: develop
Are you sure you want to change the base?
Conversation
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.
I'd like Matt to comment here, as I know he has strong (and totally reasonable) feelings on what response codes are appropriate.
} | ||
|
||
public ApiUsageException(Throwable cause) | ||
{ | ||
super(cause.getMessage() == null ? cause.toString() : cause.getMessage(), cause); | ||
this(cause.getMessage() == null ? cause.toString() : cause.getMessage(), cause); |
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.
I think we should just pass cause.getMessage()
here, though I see this old code that I appear to have committed.
*/ | ||
public class ApiUsageException extends RuntimeException implements SkipMothershipLogging | ||
public class ApiUsageException extends BadRequestException implements SkipMothershipLogging |
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.
I think defaulting to a 422 seems best, and cases that should return a 404 can be switched to use NotFoundException
.
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.
What a mess. A lot of usages of ApiUsageException do indeed seem equally or better served by NotFoundException or ValidationException.
I like the move to 4xx responses. I do not like using 400 BAD_REQUEST for any request that is basically well formatted but we can't handle for an explainable reason (e.g. Validation, NotFound, Unauthorized).
Separately, but not a topic for today. If I could design this all over again, I would use 200 {success:false} for most of our error cases and reserve non 200 responses for actual protocol errors (e.g. this isn't an legal end point so NOT_FOUND, rather than I can't find that rowid).
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.
I disagree. I think we should try to respond with the appropriate HTTP codes as intended. For example, 404:

404 doesn't indicate that the end point is illegal but rather that the resource was not found. Not finding the thing with "rowId" is precisely the case it is intended to cover.
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.
Thank you for taking a look at this. When I have some spare cycles, I'll take a crack at switching ApiUsageException
to return a 422 and switching around some inappropriate usages.
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.
I'm a bit ambivalent about the 404 case. I still think that generally any interaction where "I got your request", "I understood your request", "Here's my response to your request" doesn't require out of band error handling.
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.
Certainly agree that the "out of band error handling" is not the ideal way to process this kind of thing.
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.
The latest wording for a 400 error is a little less specific to malformed requests: something that is perceived to be a client error
-- compared to the original wording: The request could not be understood by the server due to malformed syntax
.
422 was added as a WebDAV specific response code but has since been pulled into the core HTTP standard.
That being said, I'm inclined to move forward with 422 for ApiUsageException
. That's what the GitHub API returns for requests that it understands but have some semantic error (e.g. missing a required field).
Rationale
The description on
ApiUsageException
indicate that it should be used when the client makes an invalid request. A500
response is not appropriate for this sort of error.ExceptionUtil.handleException
will detect this exception and return400
status but not all code paths will pass through that.Looking at usages of
ApiUsageException
; some should be404
s and many others should be422
. Changing them all to4xx
responses seem like a step in the right direction.Related Pull Requests
Changes
ApiUsageException
produce a 400 response