all repos — homestead @ 802d74fc1d38b7ee64f63c2f10810b20305c828a

Code for my website

make HTTP error an interface

Alan Pearce
commit

802d74fc1d38b7ee64f63c2f10810b20305c828a

parent

c0f110119e434f188f5959c48570df12121cc663

1 file changed, 5 insertions(+), 5 deletions(-)

changed files
M shared/http/mux.goshared/http/mux.go
@@ -7,15 +7,15 @@ "alin.ovh/x/log"
) // HandleFunc is a function that handles an HTTP request and may return an Error. -type HandleFunc func(http.ResponseWriter, *http.Request) *Error +type HandleFunc func(http.ResponseWriter, *http.Request) Error // Handler is an interface for types that can serve HTTP requests and may return an Error. type Handler interface { - ServeHTTP(http.ResponseWriter, *http.Request) *Error + ServeHTTP(http.ResponseWriter, *http.Request) Error } // ErrorHandler is a function that handles HTTP errors. -type ErrorHandler func(*Error, http.ResponseWriter, *http.Request) +type ErrorHandler func(Error, http.ResponseWriter, *http.Request) // ServeMux is an HTTP request multiplexer with error handling support. // It matches the URL of each incoming request against a list of registered
@@ -30,8 +30,8 @@ // NewServeMux creates a new ServeMux with a default error handler.
func NewServeMux() *ServeMux { return &ServeMux{ ServeMux: http.NewServeMux(), - errorHandler: func(err *Error, w http.ResponseWriter, _ *http.Request) { - http.Error(w, err.Message, err.Code) + errorHandler: func(err Error, w http.ResponseWriter, _ *http.Request) { + err.WriteHTTP(w) }, } }