Skip to content

Commit d9d26d6

Browse files
committed
feat: improve error codes
* remove ErrNotFound - it isn't used. If we want to type not-found errors, we should consider adding a new error "subtype" field. All the current errors are effectively transport-level errors. * remove ErrFatal - it's unclear how this should be handled. * add ErrRateLimited - requested by @MichaelMure * add ErrForbidden
1 parent 6cc6af6 commit d9d26d6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

error.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ type ErrorType uint
1111

1212
// ErrorTypes convey what category of error ocurred
1313
const (
14-
ErrNormal ErrorType = iota // general errors
15-
ErrClient // error was caused by the client, (e.g. invalid CLI usage)
16-
ErrImplementation // programmer error in the server
17-
ErrNotFound // == HTTP 404
18-
ErrFatal // abort instantly
19-
// TODO: add more types of errors for better error-specific handling
14+
// ErrNormal is a normal error. The command failed for some reason that's not a bug.
15+
ErrNormal ErrorType = iota
16+
// ErrClient means the client made an invalid request.
17+
ErrClient
18+
// ErrImplementation means there's a bug in the implementation.
19+
ErrImplementation
20+
// ErrRateLimited is returned when the operation has been rate-limited.
21+
ErrRateLimited
22+
// ErrForbidden is returned when the client doesn't have permission to
23+
// perform the requested operation.
24+
ErrForbidden
2025
)
2126

2227
// Error is a struct for marshalling errors

http/parse.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,16 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
216216
return nil, err
217217
}
218218
e.Message = string(mes)
219-
e.Code = cmds.ErrNormal
219+
switch httpRes.StatusCode {
220+
case http.StatusNotFound, http.StatusBadRequest:
221+
e.Code = cmds.ErrClient
222+
case http.StatusTooManyRequests:
223+
e.Code = cmds.ErrRateLimited
224+
case http.StatusForbidden:
225+
e.Code = cmds.ErrForbidden
226+
default:
227+
e.Code = cmds.ErrNormal
228+
}
220229
case res.dec == nil:
221230
return nil, fmt.Errorf("unknown error content type: %s", contentType)
222231
default:

0 commit comments

Comments
 (0)