Skip to content

Use standardized API error format in tequilapi - #4920

Merged
tadaskay merged 1 commit into
masterfrom
go-rest-errors
Mar 25, 2022
Merged

Use standardized API error format in tequilapi#4920
tadaskay merged 1 commit into
masterfrom
go-rest-errors

Conversation

@tadaskay

@tadaskay tadaskay commented Mar 24, 2022

Copy link
Copy Markdown
Contributor

Problem definition

  • No consistent error format among the services. Most of them are also not easy to parse and forward to the API consumers. Chained API calls are all concatenated to a single message, e.g. server returned error: Possible error: another server returned error: you get a very long sausage: where the useful message is: in the very end.
  • No consistent format for field validation that is easy to use by API consumers.

Solution

  • Use APIError format defined in mysteriumnetwork/go-rest;
  • Use apierror.ErrorHandler middleware for Gin to forward APIErrors (that have been added to gin.Context in request scope using c.Error()) to the caller and wrap any other type of errors. Returned content type is application/vnd.mysterium.error+json;
  • Use apierror.Parse(*http.Response) *APIError to parse the incoming error response body based on Content-Type.

Scope of this PR

  • Implemented for identities, payments endpoints in Tequilapi all Tequilapi endpoints and requests.Client
  • Support this format in CLI

Related info

Sample (with field validation):

{
  "error": {
    "code": "validation_failed",
    "message": "Request validation failed",
    "detail": "Request validation failed: passphrase: 'passphrase' is required [required]",
    "fields": {
      "passphrase": {
        "code": "required",
        "message": "'passphrase' is required"
      }
    }
  },
  "status": 400,
  "path": "/identities"
}

Comment thread tequilapi/endpoints/access_policies.go
@tadaskay
tadaskay force-pushed the go-rest-errors branch 4 times, most recently from bba645d to 133fd3f Compare March 25, 2022 21:32
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Merging #4920 (e3bcb0a) into master (8aba570) will decrease coverage by 0.03%.
The diff coverage is 24.88%.

@@            Coverage Diff             @@
##           master    #4920      +/-   ##
==========================================
- Coverage   39.21%   39.17%   -0.04%     
==========================================
  Files         357      356       -1     
  Lines       19214    19018     -196     
==========================================
- Hits         7535     7451      -84     
+ Misses      10948    10845     -103     
+ Partials      731      722       -9     
Impacted Files Coverage Δ
requests/client.go 70.37% <0.00%> (+8.07%) ⬆️
session/pingpong/hermes_promise_settler.go 34.03% <0.00%> (ø)
tequilapi/client/client.go 3.44% <0.00%> (+0.05%) ⬆️
tequilapi/contract/common.go 0.00% <0.00%> (ø)
tequilapi/contract/connection.go 0.00% <0.00%> (ø)
tequilapi/contract/entertainment.go 0.00% <0.00%> (ø)
tequilapi/contract/identity.go 0.00% <0.00%> (ø)
tequilapi/contract/mmn.go 0.00% <0.00%> (ø)
tequilapi/contract/pageable.go 0.00% <0.00%> (ø)
tequilapi/contract/session.go 0.00% <0.00%> (ø)
... and 34 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8aba570...e3bcb0a. Read the comment docs.

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.

5 participants