fix(libreoffice): wrong HTTP status codes if invalid PDF formats

This commit is contained in:
Julien Neuhart
2024-01-18 12:00:32 +01:00
parent e5194b313d
commit b8926350b9
7 changed files with 44 additions and 7 deletions

View File

@@ -21,9 +21,9 @@ func init() {
} }
var ( var (
// ErrInvalidPdfFormat happens if the PDF format option cannot be handled // ErrInvalidPdfFormats happens if the PDF formats option cannot be handled
// by LibreOffice. // by LibreOffice.
ErrInvalidPdfFormat = errors.New("invalid PDF format") ErrInvalidPdfFormats = errors.New("invalid PDF formats")
// ErrMalformedPageRanges happens if the page ranges option cannot be // ErrMalformedPageRanges happens if the page ranges option cannot be
// interpreted by LibreOffice. // interpreted by LibreOffice.

View File

@@ -282,7 +282,7 @@ func (p *libreOfficeProcess) pdf(ctx context.Context, logger *zap.Logger, inputP
case gotenberg.PdfA3b: case gotenberg.PdfA3b:
args = append(args, "--export", "SelectPdfVersion=3") args = append(args, "--export", "SelectPdfVersion=3")
default: default:
return ErrInvalidPdfFormat return ErrInvalidPdfFormats
} }
if options.PdfFormats.PdfUa { if options.PdfFormats.PdfUa {

View File

@@ -235,7 +235,7 @@ func TestLibreOfficeProcess_pdf(t *testing.T) {
expectError: true, expectError: true,
}, },
{ {
scenario: "ErrInvalidPdfFormat", scenario: "ErrInvalidPdfFormats",
libreOffice: func() libreOffice { libreOffice: func() libreOffice {
p := new(libreOfficeProcess) p := new(libreOfficeProcess)
p.socketPort = 12345 p.socketPort = 12345
@@ -247,7 +247,7 @@ func TestLibreOfficeProcess_pdf(t *testing.T) {
cancelledCtx: false, cancelledCtx: false,
start: false, start: false,
expectError: true, expectError: true,
expectedError: ErrInvalidPdfFormat, expectedError: ErrInvalidPdfFormats,
}, },
{ {
scenario: "ErrMalformedPageRanges", scenario: "ErrMalformedPageRanges",

View File

@@ -64,7 +64,7 @@ func (engine *LibreOfficePdfEngine) Convert(ctx context.Context, logger *zap.Log
return nil return nil
} }
if errors.Is(err, api.ErrInvalidPdfFormat) { if errors.Is(err, api.ErrInvalidPdfFormats) {
return fmt.Errorf("convert PDF to '%+v' with LibreOffice: %w", formats, gotenberg.ErrPdfFormatNotSupported) return fmt.Errorf("convert PDF to '%+v' with LibreOffice: %w", formats, gotenberg.ErrPdfFormatNotSupported)
} }

View File

@@ -137,7 +137,7 @@ func TestLibreOfficePdfEngine_Convert(t *testing.T) {
scenario: "invalid PDF format", scenario: "invalid PDF format",
api: &api.ApiMock{ api: &api.ApiMock{
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error { PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options api.Options) error {
return api.ErrInvalidPdfFormat return api.ErrInvalidPdfFormats
}, },
}, },
expectError: true, expectError: true,

View File

@@ -67,6 +67,16 @@ func convertRoute(libreOffice libreofficeapi.Uno, engine gotenberg.PdfEngine) ap
err = libreOffice.Pdf(ctx, ctx.Log(), inputPath, outputPaths[i], options) err = libreOffice.Pdf(ctx, ctx.Log(), inputPath, outputPaths[i], options)
if err != nil { if err != nil {
if errors.Is(err, libreofficeapi.ErrInvalidPdfFormats) {
return api.WrapError(
fmt.Errorf("convert to PDF: %w", err),
api.NewSentinelHttpError(
http.StatusBadRequest,
fmt.Sprintf("A PDF format in '%+v' is not supported", pdfFormats),
),
)
}
if errors.Is(err, libreofficeapi.ErrMalformedPageRanges) { if errors.Is(err, libreofficeapi.ErrMalformedPageRanges) {
return api.WrapError( return api.WrapError(
fmt.Errorf("convert to PDF: %w", err), fmt.Errorf("convert to PDF: %w", err),

View File

@@ -37,6 +37,28 @@ func TestConvertRoute(t *testing.T) {
expectHttpStatus: http.StatusBadRequest, expectHttpStatus: http.StatusBadRequest,
expectOutputPathsCount: 0, expectOutputPathsCount: 0,
}, },
{
scenario: "ErrPdfFormatNotSupported (nativePdfFormats)",
ctx: func() *api.ContextMock {
ctx := &api.ContextMock{Context: new(api.Context)}
ctx.SetFiles(map[string]string{
"document.docx": "/document.docx",
})
return ctx
}(),
libreOffice: &libreofficeapi.ApiMock{
PdfMock: func(ctx context.Context, logger *zap.Logger, inputPath, outputPath string, options libreofficeapi.Options) error {
return libreofficeapi.ErrInvalidPdfFormats
},
ExtensionsMock: func() []string {
return []string{".docx"}
},
},
expectError: true,
expectHttpError: true,
expectHttpStatus: http.StatusBadRequest,
expectOutputPathsCount: 0,
},
{ {
scenario: "ErrMalformedPageRanges", scenario: "ErrMalformedPageRanges",
ctx: func() *api.ContextMock { ctx: func() *api.ContextMock {
@@ -44,6 +66,11 @@ func TestConvertRoute(t *testing.T) {
ctx.SetFiles(map[string]string{ ctx.SetFiles(map[string]string{
"document.docx": "/document.docx", "document.docx": "/document.docx",
}) })
ctx.SetValues(map[string][]string{
"pdfa": {
"foo",
},
})
return ctx return ctx
}(), }(),
libreOffice: &libreofficeapi.ApiMock{ libreOffice: &libreofficeapi.ApiMock{