chore(pdfengines): better custom error message if invalid args

This commit is contained in:
Julien Neuhart
2025-09-03 20:41:30 +02:00
parent 84ca118524
commit d6ebc7f230
6 changed files with 27 additions and 20 deletions

4
go.sum
View File

@@ -271,8 +271,6 @@ github.com/prometheus/client_golang v1.23.0/go.mod h1:i/o0R9ByOnHX0McrTMTyhYvKE4
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/common v0.65.0 h1:QDwzd+G1twt//Kwj/Ww6E9FQq1iVMmODnILtW1t2VzE=
github.com/prometheus/common v0.65.0/go.mod h1:0gZns+BLRQ3V6NdaerOhMbwwRbNh9hkGINtQAsP5GS8=
github.com/prometheus/common v0.66.0 h1:K/rJPHrG3+AoQs50r2+0t7zMnMzek2Vbv31OFVsMeVY=
github.com/prometheus/common v0.66.0/go.mod h1:Ux6NtV1B4LatamKE63tJBntoxD++xmtI/lK0VtEplN4=
github.com/prometheus/procfs v0.17.0 h1:FuLQ+05u4ZI+SS/w9+BWEM2TXiHKsUQ9TADiRH7DuK0=
@@ -293,8 +291,6 @@ github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZ
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@@ -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.

View File

@@ -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()

View File

@@ -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)
}
}

View File

@@ -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.

View File

@@ -54,7 +54,7 @@ Feature: /forms/pdfengines/encrypt
Then the response status code should be 400
Then the response body should match string:
"""
Invalid form data: both 'userPassword' and 'ownerPassword' form fields must be provided and different
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
"""
Scenario: POST /forms/pdfengines/encrypt (PDFtk - both user and owner passwords)