ParseError parses an error and returns the corresponding HTTP status and HTTP message.
(err error)
| 36 | // ParseError parses an error and returns the corresponding HTTP status and |
| 37 | // HTTP message. |
| 38 | func ParseError(err error) (int, string) { |
| 39 | var echoErr *echo.HTTPError |
| 40 | ok := errors.As(err, &echoErr) |
| 41 | if ok { |
| 42 | return echoErr.Code, http.StatusText(echoErr.Code) |
| 43 | } |
| 44 | |
| 45 | if errors.Is(err, context.DeadlineExceeded) { |
| 46 | return http.StatusServiceUnavailable, "The request exceeded the time limit. Increase it with --api-timeout, or reduce the workload." |
| 47 | } |
| 48 | |
| 49 | if errors.Is(err, gotenberg.ErrFiltered) { |
| 50 | return http.StatusForbidden, http.StatusText(http.StatusForbidden) |
| 51 | } |
| 52 | |
| 53 | if errors.Is(err, gotenberg.ErrMaximumQueueSizeExceeded) { |
| 54 | return http.StatusTooManyRequests, "The request queue is full. Retry shortly, or raise the limit with --chromium-max-queue-size or --libreoffice-max-queue-size." |
| 55 | } |
| 56 | |
| 57 | if errors.Is(err, gotenberg.ErrPdfSplitModeNotSupported) { |
| 58 | return http.StatusBadRequest, "The requested split mode is not supported, or no PDF engine could process it. Valid modes: 'intervals', 'pages'." |
| 59 | } |
| 60 | |
| 61 | if errors.Is(err, gotenberg.ErrPdfFormatNotSupported) { |
| 62 | return http.StatusBadRequest, "The requested PDF format is not supported, or no PDF engine could apply it. Valid formats include PDF/A-1b, PDF/A-2b, PDF/A-3b, and PDF/UA." |
| 63 | } |
| 64 | |
| 65 | if errors.Is(err, gotenberg.ErrPdfEngineMetadataValueNotSupported) { |
| 66 | return http.StatusBadRequest, "The requested metadata could not be written; ensure values are valid and free of control characters." |
| 67 | } |
| 68 | |
| 69 | if errors.Is(err, gotenberg.ErrPdfStampSourceNotSupported) { |
| 70 | return http.StatusBadRequest, "The requested stamp source is not supported, or no PDF engine could process it. Valid sources: 'text', 'image', 'pdf'." |
| 71 | } |
| 72 | |
| 73 | if errors.Is(err, gotenberg.ErrPdfRotateAngleNotSupported) { |
| 74 | return http.StatusBadRequest, "The requested rotation angle is not supported. Valid angles: 90, 180, 270." |
| 75 | } |
| 76 | |
| 77 | if invalidArgsError, ok := errors.AsType[*gotenberg.PdfEngineInvalidArgsError](err); ok { |
| 78 | return http.StatusBadRequest, invalidArgsError.Error() |
| 79 | } |
| 80 | |
| 81 | var httpErr HttpError |
| 82 | if errors.As(err, &httpErr) { |
| 83 | return httpErr.HttpError() |
| 84 | } |
| 85 | |
| 86 | // Default 500 status code. |
| 87 | return http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError) |
| 88 | } |
| 89 | |
| 90 | // httpErrorHandler is the centralized HTTP error handler. It parses the error, |
| 91 | // returns a response as "text/plain; charset=UTF-8". |
no test coverage detected