Проверка и завершение требований уровней «3» и «4»

This commit is contained in:
Grbisba
2026-11-06 13:58:50 +03:00
parent 711754bbcd
commit 08ee3fe0b8
30 changed files with 1752 additions and 92 deletions
@@ -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))
})
}
}