@@ -55,18 +55,16 @@ func H[T, O any](handle Handle[T, O]) httprouter.Handle { //nolint:gocognit,cycl
5555 return
5656 }
5757
58- req := new (T )
59-
6058 var (
59+ req = new (T )
6160 res any
62- e * HTTPError
6361 )
6462
6563 // Decode the header.
6664 if decodeHeader != nil {
6765 err = decodeHeader .Decode (& ctx .Request .Header , req )
6866 if err != nil {
69- e = Error (err , http .StatusBadRequest )
67+ res = Error (err , http .StatusBadRequest )
7068 goto Encode
7169 }
7270 }
@@ -76,7 +74,7 @@ func H[T, O any](handle Handle[T, O]) httprouter.Handle { //nolint:gocognit,cycl
7674 if q := ctx .URI ().QueryArgs (); q .Len () > 0 {
7775 err := decodeQuery .Decode (q , req )
7876 if err != nil {
79- e = Error (err , http .StatusBadRequest )
77+ res = Error (err , http .StatusBadRequest )
8078 goto Encode
8179 }
8280 }
@@ -86,7 +84,7 @@ func H[T, O any](handle Handle[T, O]) httprouter.Handle { //nolint:gocognit,cycl
8684 if decodePath != nil && len (p ) != 0 {
8785 err := decodePath .Decode (p , req )
8886 if err != nil {
89- e = Error (err , http . StatusBadRequest )
87+ res = Error (ErrNotFound , 0 )
9088 goto Encode
9189 }
9290 }
@@ -95,28 +93,24 @@ func H[T, O any](handle Handle[T, O]) httprouter.Handle { //nolint:gocognit,cycl
9593 if ctx .Request .Header .ContentLength () > 0 {
9694 dec , err := getDecoder (getEncoding (ctx .Request .Header .ContentType ()))
9795 if err != nil {
98- res = err
96+ res = Error ( err , 0 )
9997 goto Encode
10098 }
10199
102100 if err := dec (ctx , req ); err != nil {
103- e = Error (err , http .StatusBadRequest )
101+ res = Error (err , getStatusCode ( err , http .StatusBadRequest ) )
104102 goto Encode
105103 }
106104 }
107105
108106 res , err = handle (ctx , * req )
109107 if err != nil {
110- e = Error (err , 0 )
108+ res = Error (err , 0 )
111109 }
112110
113111 Encode:
114112 ctx .SetContentType (contentType + "; charset=utf-8" )
115113
116- if e != nil {
117- res = e
118- }
119-
120114 if h , ok := res .(Headerer ); ok {
121115 for k , v := range h .Header () {
122116 ctx .Response .Header .Set (k , v [0 ])
@@ -139,13 +133,12 @@ func H[T, O any](handle Handle[T, O]) httprouter.Handle { //nolint:gocognit,cycl
139133}
140134
141135func handleError (ctx * fasthttp.RequestCtx , err error ) {
142- if statusCoder , ok := err .(StatusCoder ); ok { //nolint:errorlint
143- if sc := statusCoder .StatusCode (); sc < http .StatusInternalServerError {
144- ctx .Error (err .Error ()+ "\n " , sc )
145- return
146- }
136+ code := getStatusCode (err , http .StatusInternalServerError )
137+ if code < http .StatusInternalServerError {
138+ ctx .Error (err .Error ()+ "\n " , code )
139+ return
147140 }
148- ctx .Error (fasthttp .StatusMessage (http . StatusInternalServerError )+ "\n " , http . StatusInternalServerError )
141+ ctx .Error (fasthttp .StatusMessage (code )+ "\n " , code )
149142 ctx .Logger ().Printf ("%v" , err )
150143}
151144
@@ -158,6 +151,13 @@ func getEncoding(b []byte) string {
158151 return byteconv .Btoa (bytes .TrimSpace (b ))
159152}
160153
154+ func getStatusCode (i any , fallback int ) int {
155+ if sc , ok := i .(StatusCoder ); ok {
156+ return sc .StatusCode ()
157+ }
158+ return fallback
159+ }
160+
161161const (
162162 headerTag = "header"
163163 pathTag = "path"
0 commit comments