1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-23 22:11:10 +02:00
Files
imgproxy/server/errors.go
Victor Sokolov 15bd00b221 IMG-51: router -> server ns, handlers ns, added error to handler ret val (#1494)
* Introduced server, handlers, error ret in handlerfn

* Server struct with tests

* replace checkErr with return
2025-08-20 14:31:11 +02:00

76 lines
1.8 KiB
Go

package server
import (
"context"
"fmt"
"net/http"
"time"
"github.com/imgproxy/imgproxy/v3/ierrors"
)
type (
RouteNotDefinedError string
RequestCancelledError string
RequestTimeoutError string
InvalidSecretError struct{}
)
func newRouteNotDefinedError(path string) *ierrors.Error {
return ierrors.Wrap(
RouteNotDefinedError(fmt.Sprintf("Route for %s is not defined", path)),
1,
ierrors.WithStatusCode(http.StatusNotFound),
ierrors.WithPublicMessage("Not found"),
ierrors.WithShouldReport(false),
)
}
func (e RouteNotDefinedError) Error() string { return string(e) }
func newRequestCancelledError(after time.Duration) *ierrors.Error {
return ierrors.Wrap(
RequestCancelledError(fmt.Sprintf("Request was cancelled after %v", after)),
1,
ierrors.WithStatusCode(499),
ierrors.WithPublicMessage("Cancelled"),
ierrors.WithShouldReport(false),
ierrors.WithCategory(categoryTimeout),
)
}
func (e RequestCancelledError) Error() string { return string(e) }
func (e RequestCancelledError) Unwrap() error {
return context.Canceled
}
func newRequestTimeoutError(after time.Duration) *ierrors.Error {
return ierrors.Wrap(
RequestTimeoutError(fmt.Sprintf("Request was timed out after %v", after)),
1,
ierrors.WithStatusCode(http.StatusServiceUnavailable),
ierrors.WithPublicMessage("Gateway Timeout"),
ierrors.WithShouldReport(false),
ierrors.WithCategory(categoryTimeout),
)
}
func (e RequestTimeoutError) Error() string { return string(e) }
func (e RequestTimeoutError) Unwrap() error {
return context.DeadlineExceeded
}
func newInvalidSecretError() error {
return ierrors.Wrap(
InvalidSecretError{},
1,
ierrors.WithStatusCode(http.StatusForbidden),
ierrors.WithPublicMessage("Forbidden"),
ierrors.WithShouldReport(false),
)
}
func (e InvalidSecretError) Error() string { return "Invalid secret" }