mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-14 19:32:15 +01:00
chore(pdfengines): better custom error message if invalid args
This commit is contained in:
@@ -3,6 +3,7 @@ package gotenberg
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -27,12 +28,27 @@ var (
|
||||
// ErrPdfEncryptionNotSupported is returned when encryption
|
||||
// is not supported by the PDF engine.
|
||||
ErrPdfEncryptionNotSupported = errors.New("encryption not supported")
|
||||
|
||||
// ErrPdfEngineEncryptionPasswordsNotSupported is returned when provided
|
||||
// passwords are not supported by the PDF engine.
|
||||
ErrPdfEngineEncryptionPasswordsNotSupported = errors.New("passwords not supported")
|
||||
)
|
||||
|
||||
// PdfEngineInvalidArgsError represents an error returned by a PDF engine when
|
||||
// invalid arguments are provided. It includes the name of the engine and a
|
||||
// detailed message describing the issue.
|
||||
type PdfEngineInvalidArgsError struct {
|
||||
engine string
|
||||
msg string
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *PdfEngineInvalidArgsError) Error() string {
|
||||
return fmt.Sprintf("%s: %s", e.engine, e.msg)
|
||||
}
|
||||
|
||||
// NewPdfEngineInvalidArgs creates a new PdfEngineInvalidArgsError with the
|
||||
// given engine name and message.
|
||||
func NewPdfEngineInvalidArgs(engine, msg string) error {
|
||||
return &PdfEngineInvalidArgsError{engine, msg}
|
||||
}
|
||||
|
||||
const (
|
||||
// SplitModeIntervals represents a mode where a PDF is split at specific
|
||||
// intervals.
|
||||
|
||||
@@ -61,6 +61,11 @@ func ParseError(err error) (int, string) {
|
||||
return http.StatusBadRequest, "At least one PDF engine cannot process the requested metadata, while others may have failed to convert due to different issues"
|
||||
}
|
||||
|
||||
var invalidArgsError *gotenberg.PdfEngineInvalidArgsError
|
||||
if errors.As(err, &invalidArgsError) {
|
||||
return http.StatusBadRequest, invalidArgsError.Error()
|
||||
}
|
||||
|
||||
var httpErr HttpError
|
||||
if errors.As(err, &httpErr) {
|
||||
return httpErr.HttpError()
|
||||
|
||||
@@ -270,16 +270,6 @@ func EncryptPdfStub(ctx *api.Context, engine gotenberg.PdfEngine, userPassword,
|
||||
for _, inputPath := range inputPaths {
|
||||
err := engine.Encrypt(ctx, ctx.Log(), inputPath, userPassword, ownerPassword)
|
||||
if err != nil {
|
||||
if errors.Is(err, gotenberg.ErrPdfEngineEncryptionPasswordsNotSupported) {
|
||||
return api.WrapError(
|
||||
err,
|
||||
api.NewSentinelHttpError(
|
||||
http.StatusBadRequest,
|
||||
"Invalid form data: both 'userPassword' and 'ownerPassword' form fields must be provided and different",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Errorf("encrypt PDF '%s': %w", inputPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func (engine *PdfTk) Encrypt(ctx context.Context, logger *zap.Logger, inputPath,
|
||||
}
|
||||
|
||||
if ownerPassword == userPassword || ownerPassword == "" {
|
||||
return gotenberg.ErrPdfEngineEncryptionPasswordsNotSupported
|
||||
return gotenberg.NewPdfEngineInvalidArgs("pdftk", "both 'userPassword' and 'ownerPassword' must be provided and different. Consider switching to another PDF engine if this behavior does not work with your workflow")
|
||||
}
|
||||
|
||||
// Create a temp output file in the same directory.
|
||||
|
||||
Reference in New Issue
Block a user