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

Make ApiUsageException return a more appropriate response code #4396

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

labkey-tchad
Copy link
Member

@labkey-tchad labkey-tchad commented May 5, 2023

Rationale

The description on ApiUsageException indicate that it should be used when the client makes an invalid request. A 500 response is not appropriate for this sort of error. ExceptionUtil.handleException will detect this exception and return 400 status but not all code paths will pass through that.
Looking at usages of ApiUsageException; some should be 404s and many others should be 422. Changing them all to 4xx responses seem like a step in the right direction.

Related Pull Requests

  • N/A

Changes

  • Make ApiUsageException produce a 400 response

Copy link
Contributor

@labkey-jeckels labkey-jeckels left a 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);
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Contributor

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:

image

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Member Author

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

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.

4 participants