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

Return appropriate http codes #793

Merged
merged 4 commits into from
Dec 23, 2022
Merged

Conversation

dimitrovmaksim
Copy link
Collaborator

@dimitrovmaksim dimitrovmaksim commented Dec 23, 2022

Description:

Currently we return 200 status code in most cases. This PR updates the relay to return the appropriate codes:

InternalError => 500
InvalidParams (JsonRpc request params) => 400
MethodNotFound or UnsupportedMethod => 400
RateLimit => 429
JsonRpcError => 500 for InternalError, 400 for the rest

Related issue(s):

Fixes #751

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: Maksim Dimitrov <dimitrov.maksim@gmail.com>
Signed-off-by: Maksim Dimitrov <dimitrov.maksim@gmail.com>
Signed-off-by: Maksim Dimitrov <dimitrov.maksim@gmail.com>
@codecov-commenter
Copy link

codecov-commenter commented Dec 23, 2022

Codecov Report

Base: 72.58% // Head: 72.58% // No change to project coverage 👍

Coverage data is based on head (215c2b9) compared to base (725e514).
Patch has no changes to coverable lines.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #793   +/-   ##
=======================================
  Coverage   72.58%   72.58%           
=======================================
  Files          16       16           
  Lines        1262     1262           
  Branches      233      233           
=======================================
  Hits          916      916           
  Misses        284      284           
  Partials       62       62           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Signed-off-by: Maksim Dimitrov <dimitrov.maksim@gmail.com>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@dimitrovmaksim dimitrovmaksim marked this pull request as ready for review December 23, 2022 14:33
@dimitrovmaksim dimitrovmaksim requested review from natanasow, georgi-l95, Ivo-Yankov and lukelee-sl and removed request for georgi-l95 December 23, 2022 14:33
@dimitrovmaksim dimitrovmaksim changed the title 751 return appropriate http codes Return appropriate http codes Dec 23, 2022
@@ -99,18 +100,21 @@ export default class KoaJsonRpc {
ctx.request.method !== 'POST'
) {
ctx.body = jsonResp(body.id || null, new InvalidRequest(), undefined);
ctx.status = 400;
Copy link
Member

Choose a reason for hiding this comment

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

does it make sense to add the status as an optional(?) forth parameter to the jsonResp method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure I understand

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see my suggestion does not make sense. My mistake.

But I do have another question. What happens when calling jsonResp and it throws an exception?

Copy link
Collaborator Author

@dimitrovmaksim dimitrovmaksim Dec 23, 2022

Choose a reason for hiding this comment

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

Hm, good question. It seems like it's not being handled by us anywhere. I'm assuming it will return an internal error response but it won't be in json-rpc format. cc: @georgi-l95

Copy link
Collaborator Author

@dimitrovmaksim dimitrovmaksim Dec 23, 2022

Choose a reason for hiding this comment

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

Made jsonResp() to always throw an error and got this:

curl localhost:7546 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"nonExistingMethod","params": [],"id":1}'
Internal Server Error                                                                

Copy link
Member

Choose a reason for hiding this comment

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

sounds reasonable

Copy link
Member

@lukelee-sl lukelee-sl left a comment

Choose a reason for hiding this comment

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

LG

Copy link
Collaborator

@georgi-l95 georgi-l95 left a comment

Choose a reason for hiding this comment

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

LGTM

@dimitrovmaksim dimitrovmaksim merged commit ca850cd into main Dec 23, 2022
@dimitrovmaksim dimitrovmaksim deleted the 751-return-appropriate-http-codes branch December 23, 2022 20:06
@lukelee-sl lukelee-sl added this to the 0.15.0 milestone Jan 6, 2023
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.

Return appropriate http response codes
4 participants