Created
May 2, 2019 23:28
-
-
Save zeyneloz/a2182456acf217eaf54a754b8b951b3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HTTPError implements ClientError interface. | |
type HTTPError struct { | |
Cause error `json:"-"` | |
Detail string `json:"detail"` | |
Status int `json:"-"` | |
} | |
func (e *HTTPError) Error() string { | |
if e.Cause == nil { | |
return e.Detail | |
} | |
return e.Detail + " : " + e.Cause.Error() | |
} | |
// ResponseBody returns JSON response body. | |
func (e *HTTPError) ResponseBody() ([]byte, error) { | |
body, err := json.Marshal(e) | |
if err != nil { | |
return nil, fmt.Errorf("Error while parsing response body: %v", err) | |
} | |
return body, nil | |
} | |
// ResponseHeaders returns http status code and headers. | |
func (e *HTTPError) ResponseHeaders() (int, map[string]string) { | |
return e.Status, map[string]string{ | |
"Content-Type": "application/json; charset=utf-8", | |
} | |
} | |
func NewHTTPError(err error, status int, detail string) error { | |
return &HTTPError{ | |
Cause: err, | |
Detail: detail, | |
Status: status, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment