Files

33 lines
538 B
Go
Raw Permalink Normal View History

2016-04-11 20:43:18 -07:00
package errors
import (
"fmt"
"net/http"
)
2016-05-23 23:54:35 -07:00
// HTTP implements error and keeps track of http return codes.
2016-04-11 20:43:18 -07:00
type HTTP struct {
error
Message string
Code int
}
func (e HTTP) Error() string {
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
2016-05-23 23:54:35 -07:00
// ToHTTP wraps the type assertion to change an error into an HTTP.
2016-04-11 20:43:18 -07:00
func ToHTTP(err error) *HTTP {
if err == nil {
return nil
}
rerr := &HTTP{
Message: err.Error(),
Code: http.StatusInternalServerError,
}
if e, ok := err.(HTTP); ok {
rerr.Code = e.Code
}
return rerr
}