mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Add option to supply HTTP status codes with API errors
This commit is contained in:
parent
8c758e7e52
commit
140389d142
1 changed files with 48 additions and 1 deletions
|
@ -59,6 +59,41 @@ type Parameter struct {
|
||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPStatusProvider is an interface for errors to provide a custom HTTP
|
||||||
|
// status code.
|
||||||
|
type HTTPStatusProvider interface {
|
||||||
|
HTTPStatus() int
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPStatusError represents an error with an HTTP status code.
|
||||||
|
type HTTPStatusError struct {
|
||||||
|
err error
|
||||||
|
code int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error returns the error message.
|
||||||
|
func (e *HTTPStatusError) Error() string {
|
||||||
|
return e.err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unwrap return the wrapped error.
|
||||||
|
func (e *HTTPStatusError) Unwrap() error {
|
||||||
|
return e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPStatus returns the HTTP status code this error.
|
||||||
|
func (e *HTTPStatusError) HTTPStatus() int {
|
||||||
|
return e.code
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorWithStatus adds the HTTP status code to the error.
|
||||||
|
func ErrorWithStatus(err error, code int) error {
|
||||||
|
return &HTTPStatusError{
|
||||||
|
err: err,
|
||||||
|
code: code,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// ActionFunc is for simple actions with a return message for the user.
|
// ActionFunc is for simple actions with a return message for the user.
|
||||||
ActionFunc func(ar *Request) (msg string, err error)
|
ActionFunc func(ar *Request) (msg string, err error)
|
||||||
|
@ -340,7 +375,19 @@ func (e *Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Check for handler error.
|
// Check for handler error.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
// if statusProvider, ok := err.(HTTPStatusProvider); ok {
|
||||||
|
var statusProvider HTTPStatusProvider
|
||||||
|
if errors.As(err, &statusProvider) {
|
||||||
|
http.Error(w, err.Error(), statusProvider.HTTPStatus())
|
||||||
|
} else {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if there is no response data.
|
||||||
|
if len(responseData) == 0 {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue