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

Standardize API Responses/HTTP Codes #4923

Closed
4 tasks
alexanderbez opened this issue Aug 19, 2019 · 7 comments
Closed
4 tasks

Standardize API Responses/HTTP Codes #4923

alexanderbez opened this issue Aug 19, 2019 · 7 comments
Labels
C:CLI good first issue help wanted T: Dev UX UX for SDK developers (i.e. how to call our code)
Milestone

Comments

@alexanderbez
Copy link
Contributor

alexanderbez commented Aug 19, 2019

Summary

Currently when querying for a resource(s) from a querier, in the non-happy path we naively return a 500 HTTP status code with the error:

res, height, err := cliCtx.QueryWithData(endpoint, bz) // or cliCtx.Query(endpoint)
if err != nil {
	rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
	return
}

This can happen for any of the following reasons:

  1. Proof verification failure (client-side)
  2. Response is not OK (server-side)
    2a. Bad querier path
    2b. Invalid query height
    2c. Invalid query height with proof
    2d. Querying against pruned state
    2e. The resource(s) does not exist

Problem Definition

We should not be returning a 500 naively for all these conditions.

  • (2a-2c) Should result in 4xx
  • (2d) Should remain as a 5xx
  • (2e) Should most likely return a 2xx with an empty/zero-value result and either the current or provided height.

Proposal

Based on the error, use a proper code in the ResponseQuery object to effectively return the correct type of error. Then, the CLIContext (or whatever upstream client) can appropriately handle this by returning a typed error. Finally, the desired HTTP status code and body can be returned based on the concrete type of the error.

e.g.

res, err := cliCtx.Query(endpoint)
if err != nil {
  switch err:
  case BadInput:
       // 4xx...
  case ResourceDoesNotExist:
        // 2xx...
  default:
       rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
       return
}

Note: I'm not sure if this will be a trivial or small PR but can be broken down into multiple PRs. Essentially each REST handler (CLI too?) and module querier method will have to be adjusted.

/cc @jackzampolin @fedekunze


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@alexanderbez alexanderbez added C:CLI REST T: Dev UX UX for SDK developers (i.e. how to call our code) labels Aug 19, 2019
@tnachen
Copy link
Contributor

tnachen commented Aug 20, 2019

How about the case of 1)? If it's a client side issue we should send invalid request then right?
Overall this looks right to me.

@alexanderbez
Copy link
Contributor Author

@tnachen in the case of (1) this means proof verification failed; i.e. the proof Tendermint sent us in the ABCI query response is invalid. Not sure what else we can do apart from what we currently do.

@tnachen
Copy link
Contributor

tnachen commented Aug 20, 2019

Should we just send a bad request back?

@alexanderbez
Copy link
Contributor Author

hmmm, maybe we should return 4xx for proof verification failure. Good idea.

@alexanderbez
Copy link
Contributor Author

We should probably tackle #4844 first and then this issue.

@alexanderbez alexanderbez added this to the v0.38.0 milestone Oct 12, 2019
@alexanderbez alexanderbez mentioned this issue Oct 12, 2019
4 tasks
@github-actions
Copy link
Contributor

github-actions bot commented Jul 8, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@tac0turtle
Copy link
Member

grpc error codes brings this to us, we just need to do a little grooming of what error is needed for each response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:CLI good first issue help wanted T: Dev UX UX for SDK developers (i.e. how to call our code)
Projects
None yet
Development

No branches or pull requests

4 participants