Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider handling errors with detailed API responses #553

Open
abdullah opened this issue Feb 22, 2025 · 1 comment
Open

Consider handling errors with detailed API responses #553

abdullah opened this issue Feb 22, 2025 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@abdullah
Copy link
Contributor

abdullah commented Feb 22, 2025

Problem Description

When the Gotenberg API fails, it provides valuable error details in the response body that explain the failure reason. Currently, in https://github.com/cherfia/chromiumly/blob/main/src/common/gotenberg.utils.ts, the fetch method discards these details and only throws the HTTP status code and status text, making production debugging difficult. For example, the current error output is limited to:

400 Bad Request

or

503 Service Unavailable 

However, Gotenberg includes specific error details in the response body. By modifying the fetch method to capture and include these details, we can better understand why requests fail. Below is the improved implementation (for our use case).

Our patch version for reference

import { GotenbergError } from '#/exceptions/error';
import { GotenbergUtils } from 'chromiumly/dist/common';

GotenbergUtils.fetch = async function (
  endpoint: string,
  data: FormData,
  username?: string,
  password?: string,
  customHttpHeaders?: Record<string, string>
): Promise<Buffer> {
  console.log('fetch', endpoint, data, username, password, customHttpHeaders);

  const headers: Record<string, string> = { ...customHttpHeaders };

  if (username && password) {
    const authHeader = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
    headers.Authorization = authHeader;
  }

  const response = await fetch(endpoint, {
    method: 'POST',
    body: data,
    headers
  });

  if (!response.ok) {
    let message = await response.text().catch(() => response.statusText);

    if (response.status === 503) {
      message = 'Your request is taking longer than expected or the generated file is too large.';
    }

    throw new GotenbergError(message, response.status);
  }

  const arrayBuffer = await response.arrayBuffer();

  return Buffer.from(arrayBuffer);
};
export class GotenbergError extends Error {
  statusCode: number;

  constructor(message: string | undefined, statusCode: number) {
    super(message);
    this.statusCode = statusCode;
  }
}
@cherfia cherfia self-assigned this Feb 22, 2025
@cherfia cherfia added the enhancement New feature or request label Feb 22, 2025
@cherfia
Copy link
Owner

cherfia commented Feb 25, 2025

@abdullah Thanks a lot for reporting this and for your suggestions. The next release will have more detailed error messages 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants