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 ...@@ -11,12 +11,17 @@ type ErrorType uint
// ErrorTypes convey what category of error ocurred // ErrorTypes convey what category of error ocurred
const ( const (
ErrNormal ErrorType = iota // general errors // ErrNormal is a normal error. The command failed for some reason that's not a bug.
ErrClient // error was caused by the client, (e.g. invalid CLI usage) ErrNormal ErrorType = iota
ErrImplementation // programmer error in the server // ErrClient means the client made an invalid request.
ErrNotFound // == HTTP 404 ErrClient
ErrFatal // abort instantly // ErrImplementation means there's a bug in the implementation.
// TODO: add more types of errors for better error-specific handling 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 // Error is a struct for marshalling errors
......
...@@ -216,7 +216,16 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er ...@@ -216,7 +216,16 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
return nil, err return nil, err
} }
e.Message = string(mes) 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: case res.dec == nil:
return nil, fmt.Errorf("unknown error content type: %s", contentType) return nil, fmt.Errorf("unknown error content type: %s", contentType)
default: 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