This commit is contained in:
2026-10-04 03:08:37 +07:00
parent fb62c67c23
commit 07f90215cd
3 changed files with 12 additions and 8 deletions
+9 -5
View File
@@ -354,28 +354,32 @@ func customErrorHandler(
) {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
writeTooLargeError(w, marshaler)
writeTooLargeError(w, marshaler, r.URL.Path)
return
}
if status.Code(err) == codes.ResourceExhausted {
switch r.URL.Path {
case "/api/files/upload", "/api/scenarios/archive":
writeTooLargeError(w, marshaler)
writeTooLargeError(w, marshaler, r.URL.Path)
return
}
}
if status.Code(err) == codes.InvalidArgument &&
strings.Contains(status.Convert(err).Message(), "request body too large") {
writeTooLargeError(w, marshaler)
writeTooLargeError(w, marshaler, r.URL.Path)
return
}
runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
}
func writeTooLargeError(w http.ResponseWriter, marshaler runtime.Marshaler) {
func writeTooLargeError(w http.ResponseWriter, marshaler runtime.Marshaler, route string) {
limit := maxFileSize
if route == "/api/scenarios/archive" {
limit = scenario_archive.MaxArchiveSize()
}
w.Header().Set("Content-Type", marshaler.ContentType(nil))
w.WriteHeader(http.StatusRequestEntityTooLarge)
_ = marshaler.NewEncoder(w).Encode(map[string]string{
"error": fmt.Sprintf("файл слишком большой (лимит %d МБ)", maxFileSize>>20),
"error": fmt.Sprintf("файл слишком большой (лимит %d МБ)", limit>>20),
})
}