package server import ( "fmt" "net/http" "strings" ) 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) { var msg string switch e { default: msg = strings.Join(strings.Split(e.String(), " "), "_") } return mustMarshalJson(struct { Error string `json:"error"` }{ Error: msg, }) }