serokell / coffer

Multi-backend password store with multiple frontends
4 stars 2 forks source link

Return proper status codes when an error occurs #83

Closed dcastro closed 2 years ago

dcastro commented 2 years ago

Clarification and motivation

When a command fails, the Web API returns a 200 status code with an error message in the body. For example, creating the same entry twice results in:

$ curl -s -D /dev/stderr \
  -H 'token: root' \
  'localhost:8081/api/v1/content/create?path=/ex1' \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '[{"a": "b"}, {"c": "d"}]' | jq

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Fri, 29 Apr 2022 10:38:15 GMT
Server: Warp/3.3.18
Content-Type: application/json;charset=utf-8

{
  "contents": {
    "unEntryPath": [
      "ex1"
    ]
  },
  "tag": "CREntryAlreadyExists"
}

We should return an appropriate status code depending on the error (in this specific case, it should be a 409 Conflict) with an error message in the body:

{
  "error": "An entry already exists at <path>",
  "code": 123
}

Each type of error should also be associated with a numeric code. This will allow the frontend to know exactly what went wrong and act appropriately. For example, when a copy operation fails, it may return something like:

409 Conflict
{
  "error": "An entry already exists at <path>",
  "code": 123
}

Or:

409 Conflict
{
  "error": "<path> is a directory",
  "code": 456
}

And the frontend will be able to match on the numeric code and decide what to do next.

We should create a markdown table in /docs with all the error codes and what they represent.

Acceptance criteria