Skip to content

Commit

Permalink
Improve error BadRequest messages (#70)
Browse files Browse the repository at this point in the history
* Improve error BadRequest messages

Fixes webrpc/webrpc#301

* Regenerate examples
  • Loading branch information
VojtechVitek authored Aug 19, 2024
1 parent 63ec3bf commit 694c53c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler = s.serve{{$method.Name | firstLetterToUpper}}JSON{{if $method.StreamOutput}}Stream{{end}}
{{- end}}
default:
err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no handler for path %q", r.URL.Path))
err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no WebRPC method defined for path %v", r.URL.Path))
s.sendErrorJSON(w, r, err)
return
}

if r.Method != "POST" {
w.Header().Add("Allow", "POST") // RFC 9110.
err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method))
err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %v (only POST is allowed)", r.Method))
s.sendErrorJSON(w, r, err)
return
}
Expand All @@ -71,7 +71,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "application/json":
handler(ctx, w, r)
default:
err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type")))
err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type")))
s.sendErrorJSON(w, r, err)
}
}
Expand Down

0 comments on commit 694c53c

Please sign in to comment.