Error cleanup
This commit is contained in:
@@ -17,7 +17,7 @@ func (e *Error) Error() string {
|
|||||||
var (
|
var (
|
||||||
ErrUnexpected = Internal.WithMsg("Unexpected internal error.")
|
ErrUnexpected = Internal.WithMsg("Unexpected internal error.")
|
||||||
ErrNotFound = NotFound.WithMsg("Not found.")
|
ErrNotFound = NotFound.WithMsg("Not found.")
|
||||||
ErrAlreadyExists = Conflict.WithMsg("AlreadyExists.")
|
ErrAlreadyExists = Conflict.WithMsg("Already exists.")
|
||||||
|
|
||||||
// Validation errors.
|
// Validation errors.
|
||||||
ErrInvalidIP = BadRequest.WithMsg("Invalid IP.")
|
ErrInvalidIP = BadRequest.WithMsg("Invalid IP.")
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package hub
|
package hub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"vppn/hub/api"
|
"vppn/hub/api"
|
||||||
|
"vppn/hub/errs"
|
||||||
|
|
||||||
"git.crumpington.com/lib/go/webutil"
|
"git.crumpington.com/lib/go/webutil"
|
||||||
)
|
)
|
||||||
@@ -21,7 +24,7 @@ func (app *App) handlePub(pattern string, fn handlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := fn(&s, w, r); err != nil {
|
if err := fn(&s, w, r); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
handleError(w, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,8 +71,7 @@ func (app *App) handlePeer(pattern string, fn peerHandlerFunc) {
|
|||||||
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
if err := fn(peer, w, r); err != nil {
|
if err := fn(peer, w, r); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
handleError(w, err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,3 +79,13 @@ func (app *App) handlePeer(pattern string, fn peerHandlerFunc) {
|
|||||||
webutil.WithLogging(
|
webutil.WithLogging(
|
||||||
wrapped))
|
wrapped))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleError(w http.ResponseWriter, err error) {
|
||||||
|
var e *errs.Error
|
||||||
|
if errors.As(err, &e) {
|
||||||
|
http.Error(w, e.Msg, e.Code)
|
||||||
|
} else {
|
||||||
|
log.Printf("Unexpected error: %v", err)
|
||||||
|
http.Error(w, "Internal server error.", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user