mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-13 03:37:21 +01:00
43 lines
828 B
Go
43 lines
828 B
Go
|
package api
|
||
|
|
||
|
import "net/http"
|
||
|
|
||
|
type appError struct {
|
||
|
Error error
|
||
|
Response string
|
||
|
Code int
|
||
|
ContentType string
|
||
|
}
|
||
|
|
||
|
func internalServerError(err error) *appError {
|
||
|
return &appError{Error: err, Response: "Internal server error", Code: http.StatusInternalServerError}
|
||
|
}
|
||
|
|
||
|
func notFound(err error) *appError {
|
||
|
return &appError{Error: err, Code: http.StatusNotFound}
|
||
|
}
|
||
|
|
||
|
func (e *appError) WithContentType(contentType string) *appError {
|
||
|
e.ContentType = contentType
|
||
|
return e
|
||
|
}
|
||
|
|
||
|
func (e *appError) WithCode(code int) *appError {
|
||
|
e.Code = code
|
||
|
return e
|
||
|
}
|
||
|
|
||
|
func (e *appError) WithResponse(response string) *appError {
|
||
|
e.Response = response
|
||
|
return e
|
||
|
}
|
||
|
|
||
|
func (e *appError) WithError(err error) *appError {
|
||
|
e.Error = err
|
||
|
return e
|
||
|
}
|
||
|
|
||
|
func (e *appError) IsJSON() bool {
|
||
|
return e.ContentType == APPLICATION_JSON
|
||
|
}
|