all repos — homestead @ c7165476f375b097761e729ac14f9b8a68af98c6

Code for my website

simplify HTTP error module

Alan Pearce
commit

c7165476f375b097761e729ac14f9b8a68af98c6

parent

8267148e988f22647aa735652dafd06c692b047f

1 file changed, 15 insertions(+), 3 deletions(-)

changed files
M domain/identity/webfinger/service.godomain/identity/webfinger/service.go
@@ -21,6 +21,18 @@ providers []ResourceProvider
corsOrigin string } +var ( + ErrMissingResourceParameter = ihttp.NewError( + "Missing resource parameter", + http.StatusBadRequest, + ) + ErrFailedToEncodeResponse = ihttp.NewError( + "Failed to encode webfinger response", + http.StatusInternalServerError, + ) + ErrNotFound = ihttp.NewError("Resource not found", http.StatusNotFound) +) + type Option func(*Service) func WithCORSOrigin(origin string) Option {
@@ -50,7 +62,7 @@
func (s *Service) HandleFunc(w http.ResponseWriter, r *http.Request) ihttp.Error { resource := r.URL.Query().Get("resource") if resource == "" { - return ihttp.BadRequest("Missing resource parameter", nil) + return ErrMissingResourceParameter } for _, provider := range s.providers {
@@ -62,12 +74,12 @@ w.Header().Add("Access-Control-Allow-Origin", s.corsOrigin)
} if err := json.NewEncoder(w).Encode(provider.GetIdentityResource()); err != nil { - return ihttp.InternalServerError("Failed to encode webfinger response", err) + return ErrFailedToEncodeResponse.WithCause(err) } return nil } } - return ihttp.NotFound("Resource not found") + return ErrNotFound }