echoip/api/error.go

41 lines
759 B
Go
Raw Normal View History

2015-09-18 17:13:14 +02:00
package api
import "net/http"
type appError struct {
Error error
2016-04-15 20:14:16 +02:00
Message string
2015-09-18 17:13:14 +02:00
Code int
ContentType string
}
func internalServerError(err error) *appError {
2016-04-15 20:14:16 +02:00
return &appError{
Error: err,
Message: "Internal server error",
Code: http.StatusInternalServerError,
}
2015-09-18 17:13:14 +02:00
}
func notFound(err error) *appError {
return &appError{Error: err, Code: http.StatusNotFound}
}
2016-04-15 20:52:15 +02:00
func badRequest(err error) *appError {
return &appError{Error: err, Code: http.StatusBadRequest}
}
2016-04-15 20:14:16 +02:00
func (e *appError) AsJSON() *appError {
e.ContentType = APPLICATION_JSON
2015-09-18 17:13:14 +02:00
return e
}
2016-04-15 20:14:16 +02:00
func (e *appError) WithMessage(message string) *appError {
e.Message = message
2015-09-18 17:13:14 +02:00
return e
}
func (e *appError) IsJSON() bool {
return e.ContentType == APPLICATION_JSON
}