mirror of
https://github.com/mpolden/echoip.git
synced 2025-07-15 13:33:31 +02:00
Improve error handling
This commit is contained in:
42
api/error.go
Normal file
42
api/error.go
Normal file
@ -0,0 +1,42 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user