forked from mtusi2026/mtusi2026lab2
Проверка и завершение требований уровней «3» и «4»
This commit is contained in:
@@ -69,6 +69,9 @@ func JSONError(ctx context.Context, w http.ResponseWriter, err error) {
|
||||
}
|
||||
w.Header().Set(headers.HeaderRequestID, requestId.String())
|
||||
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
err = apperr.Wrap(err, apperr.CodeServiceUnavailable, "request deadline exceeded")
|
||||
}
|
||||
appErr := apperr.As(err)
|
||||
if appErr == nil {
|
||||
appErr = apperr.As(apperr.New(apperr.CodeInternal, "internal server error"))
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RequestTimeout bounds database work and waiting for a pooled connection.
|
||||
func RequestTimeout(timeout time.Duration) Middleware {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), timeout)
|
||||
defer cancel()
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user