fission.Error constructor from http response
This commit is contained in:
@@ -18,6 +18,7 @@ package fission
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
@@ -28,6 +29,25 @@ func MakeError(code int, msg string) Error {
|
|||||||
return Error{Code: errorCode(code), Message: msg}
|
return Error{Code: errorCode(code), Message: msg}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MakeErrorFromHTTP(resp *http.Response) error {
|
||||||
|
if resp.StatusCode == 200 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var errCode int
|
||||||
|
switch resp.StatusCode {
|
||||||
|
case 403:
|
||||||
|
errCode = ErrorNotAuthorized
|
||||||
|
case 404:
|
||||||
|
errCode = ErrorNotFound
|
||||||
|
case 400:
|
||||||
|
errCode = ErrorInvalidArgument
|
||||||
|
default:
|
||||||
|
errCode = ErrorInternal
|
||||||
|
}
|
||||||
|
return MakeError(errCode, resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
func (err Error) HTTPStatus() int {
|
func (err Error) HTTPStatus() int {
|
||||||
var code int
|
var code int
|
||||||
switch err.Code {
|
switch err.Code {
|
||||||
|
|||||||
Reference in New Issue
Block a user