Skip to content

Instantly share code, notes, and snippets.

@zeyneloz
Created May 2, 2019 23:28
Show Gist options
  • Save zeyneloz/a2182456acf217eaf54a754b8b951b3c to your computer and use it in GitHub Desktop.
Save zeyneloz/a2182456acf217eaf54a754b8b951b3c to your computer and use it in GitHub Desktop.
// 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