Unverified Commit 6069424b authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #168 from ipfs/feat/error-codes

feat: improve error codes
parents 260a5160 d9d26d6e
......@@ -11,12 +11,17 @@ type ErrorType uint
// ErrorTypes convey what category of error ocurred
const (
ErrNormal ErrorType = iota // general errors
ErrClient // error was caused by the client, (e.g. invalid CLI usage)
ErrImplementation // programmer error in the server
ErrNotFound // == HTTP 404
ErrFatal // abort instantly
// TODO: add more types of errors for better error-specific handling
// ErrNormal is a normal error. The command failed for some reason that's not a bug.
ErrNormal ErrorType = iota
// ErrClient means the client made an invalid request.
ErrClient
// ErrImplementation means there's a bug in the implementation.
ErrImplementation
// ErrRateLimited is returned when the operation has been rate-limited.
ErrRateLimited
// ErrForbidden is returned when the client doesn't have permission to
// perform the requested operation.
ErrForbidden
)
// Error is a struct for marshalling errors
......
......@@ -216,7 +216,16 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
return nil, err
}
e.Message = string(mes)
e.Code = cmds.ErrNormal
switch httpRes.StatusCode {
case http.StatusNotFound, http.StatusBadRequest:
e.Code = cmds.ErrClient
case http.StatusTooManyRequests:
e.Code = cmds.ErrRateLimited
case http.StatusForbidden:
e.Code = cmds.ErrForbidden
default:
e.Code = cmds.ErrNormal
}
case res.dec == nil:
return nil, fmt.Errorf("unknown error content type: %s", contentType)
default:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment