echoip/api/error.go

47 lines
816 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:14:16 +02:00
func (e *appError) AsJSON() *appError {
e.ContentType = APPLICATION_JSON
2015-09-18 17:13:14 +02:00
return e
}
func (e *appError) WithCode(code int) *appError {
e.Code = code
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) WithError(err error) *appError {
e.Error = err
return e
}
func (e *appError) IsJSON() bool {
return e.ContentType == APPLICATION_JSON
}