package generic import ( "encoding/json" "fmt" "net/http" ) type Error uint32 const ( Ok Error = http.StatusOK ErrorNotImplemented Error = http.StatusNotImplemented ErrorMethodNotAllowed Error = http.StatusMethodNotAllowed ErrorBadRequest Error = http.StatusBadRequest ErrorUnauthorized Error = http.StatusUnauthorized ErrorServerError Error = http.StatusInternalServerError ) func (e Error) Ok() (ok bool) { return e == Ok } func (e Error) HttpStatus() (code int) { return int(e) } func (e Error) String() (out string) { switch e { case Ok: out = "ok" case ErrorNotImplemented: out = "server not implemented" case ErrorMethodNotAllowed: out = "method not allowed" case ErrorBadRequest: out = "bad request" case ErrorUnauthorized: out = "user unauthorized" case ErrorServerError: out = "internal server error" default: out = "unhandled error" } return } func (e Error) Error() (out string) { return fmt.Sprintf("%s (%d)", e.String(), e.HttpStatus()) } func (e Error) BodyBytes() (body []byte) { body, _ = json.Marshal(struct{ Error string }{Error: e.String()}) return }