1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2026-06-11 22:04:13 +02:00
Files

101 lines
3.0 KiB
Go
Raw Permalink Normal View History

2025-09-25 19:48:53 +03:00
package optionsparser
import (
2026-01-14 18:10:46 +01:00
"context"
2025-09-25 19:48:53 +03:00
"fmt"
"net/http"
"strings"
2026-05-13 18:48:55 +03:00
"github.com/imgproxy/imgproxy/v4/errctx"
2025-09-25 19:48:53 +03:00
)
2026-01-14 18:10:46 +01:00
const defaultDocsUrl = "https://docs.imgproxy.net/usage/processing"
2026-01-14 15:32:48 +01:00
2025-09-25 19:48:53 +03:00
type (
2025-11-20 01:26:21 +06:00
InvalidURLError struct{ *errctx.TextError }
UnknownOptionError struct{ *errctx.TextError }
ForbiddenOptionError struct{ *errctx.TextError }
OptionArgumentError struct{ *errctx.TextError }
SecurityOptionsError struct{ *errctx.TextError }
2025-09-25 19:48:53 +03:00
)
2026-01-14 18:10:46 +01:00
func newInvalidURLError(ctx context.Context, format string, args ...any) error {
2025-11-20 01:26:21 +06:00
return InvalidURLError{errctx.NewTextError(
fmt.Sprintf(format, args...),
2025-09-25 19:48:53 +03:00
1,
2025-11-20 01:26:21 +06:00
errctx.WithStatusCode(http.StatusNotFound),
errctx.WithPublicMessage("Invalid URL"),
2026-01-14 18:10:46 +01:00
errctx.WithDocsURL(errctx.DocsBaseURL(ctx, defaultDocsUrl)),
2025-11-20 01:26:21 +06:00
errctx.WithShouldReport(false),
)}
2025-09-25 19:48:53 +03:00
}
2026-01-14 18:10:46 +01:00
func newUnknownOptionError(ctx context.Context, kind, opt string) error {
2025-11-20 01:26:21 +06:00
return UnknownOptionError{errctx.NewTextError(
fmt.Sprintf("Unknown %s option %s", kind, opt),
2025-09-25 19:48:53 +03:00
1,
2025-11-20 01:26:21 +06:00
errctx.WithStatusCode(http.StatusNotFound),
errctx.WithPublicMessage("Invalid URL"),
2026-01-14 18:10:46 +01:00
errctx.WithDocsURL(errctx.DocsBaseURL(ctx, defaultDocsUrl)),
2025-11-20 01:26:21 +06:00
errctx.WithShouldReport(false),
)}
2025-09-25 19:48:53 +03:00
}
2026-01-14 18:10:46 +01:00
func newForbiddenOptionError(ctx context.Context, kind, opt string) error {
2025-11-20 01:26:21 +06:00
return ForbiddenOptionError{errctx.NewTextError(
fmt.Sprintf("Forbidden %s option %s", kind, opt),
2025-09-25 19:48:53 +03:00
1,
2025-11-20 01:26:21 +06:00
errctx.WithStatusCode(http.StatusNotFound),
errctx.WithPublicMessage("Invalid URL"),
errctx.WithShouldReport(false),
2026-01-14 18:10:46 +01:00
errctx.WithDocsURL(errctx.DocsBaseURL(ctx, defaultDocsUrl)),
2025-11-20 01:26:21 +06:00
)}
2025-09-25 19:48:53 +03:00
}
2026-01-14 18:10:46 +01:00
func newOptionArgumentError(ctx context.Context, key, format string, args ...any) error {
url := errctx.DocsBaseURL(ctx, defaultDocsUrl)
2026-01-14 15:32:48 +01:00
if key != "" {
url = url + "#" + errorKey(key)
}
2025-11-20 01:26:21 +06:00
return OptionArgumentError{errctx.NewTextError(
fmt.Sprintf(format, args...),
2025-09-25 19:48:53 +03:00
1,
2025-11-20 01:26:21 +06:00
errctx.WithStatusCode(http.StatusNotFound),
errctx.WithPublicMessage("Invalid URL"),
2026-01-14 15:32:48 +01:00
errctx.WithDocsURL(url),
2025-11-20 01:26:21 +06:00
errctx.WithShouldReport(false),
)}
2025-09-25 19:48:53 +03:00
}
2026-01-14 18:10:46 +01:00
func newSecurityOptionsError(ctx context.Context) error {
2025-11-20 01:26:21 +06:00
return SecurityOptionsError{errctx.NewTextError(
"Security processing options are not allowed",
2025-09-25 19:48:53 +03:00
1,
2025-11-20 01:26:21 +06:00
errctx.WithStatusCode(http.StatusForbidden),
errctx.WithPublicMessage("Invalid URL"),
2026-01-14 18:10:46 +01:00
errctx.WithDocsURL(errctx.DocsBaseURL(ctx, defaultDocsUrl)),
2025-11-20 01:26:21 +06:00
errctx.WithShouldReport(false),
)}
2025-09-25 19:48:53 +03:00
}
// newInvalidArgsError creates a standardized error for invalid arguments
2026-01-14 18:10:46 +01:00
func newInvalidArgsError(ctx context.Context, key string, args []string) error {
return newOptionArgumentError(ctx, key, "Invalid %s arguments: %s", key, args)
2025-09-25 19:48:53 +03:00
}
// newInvalidArgumentError creates a standardized error for an invalid single argument
2026-01-14 18:10:46 +01:00
func newInvalidArgumentError(ctx context.Context, key, arg string, expected ...string) error {
2025-09-25 19:48:53 +03:00
msg := "Invalid %s: %s"
if len(expected) > 0 {
msg += " (expected " + strings.Join(expected, ", ") + ")"
}
2026-01-14 18:10:46 +01:00
return newOptionArgumentError(ctx, key, msg, key, arg)
2026-01-14 15:32:48 +01:00
}
func errorKey(key string) string {
k, _, _ := strings.Cut(strings.ReplaceAll(key, "_", "-"), ".")
return k
2025-09-25 19:48:53 +03:00
}