Paperful Icon
Paperfuldocs

Handle errors

Catch and respond to errors from the API.

When a request fails, the SDK throws an error with the HTTP status and details from the API. Every error extends PaperfulError, with a class for each kind of error. Streamed operations, such as uploading and streaming a search, throw the same errors when they fail midway.

Catch errors

import { NotFound } from "@paperful/sdk";

try {
  await paperful.papers.get("00000000-0000-0000-0000-000000000000");
} catch (error) {
  if (error instanceof NotFound) {
    console.log("Paper not found");
  } else {
    throw error;
  }
}

Error details

Every error includes:

FieldDescription
statusThe HTTP status.
nameThe error's name, such as "not-found". Also available as the ErrorName enum.
messageA description of what went wrong.
retrytrue when the request can be retried.
manyFor validation errors, the problem with each invalid field.
import { ValidationError } from "@paperful/sdk";

try {
  await paperful.search.query("revenue", { next: cursor });
} catch (error) {
  if (error instanceof ValidationError) {
    console.log(error.many);
  } else {
    throw error;
  }
}

Common errors

StatusClassNameDescription
400BadRequestbad-requestThe request was invalid.
400ValidationErrorvalidationOne or more parameters are invalid, such as an expired cursor.
401UnauthorizedunauthorizedThe API key is missing or invalid.
403PermissionDeniedpermission-deniedThe API key can't access this resource.
404NotFoundnot-foundThe resource doesn't exist.
408TimedOuttimed-outThe request took too long.
409ConflictconflictThe request conflicts with the resource's current state.
413EntityTooLargeentity-too-largeThe file is larger than your plan allows.
415UnsupportedMediaTypeunsupported-media-typeThe file type isn't supported, or its name couldn't be inferred.
422InvalidDatainvalid-dataThe data couldn't be processed.
429TooManyRequeststoo-many-requestsYou've hit a rate limit.
500InternalErrorinternal-errorSomething went wrong on Paperful's side.
501NotImplementednot-implementedThe operation isn't supported.
503ServiceUnavailableservice-unavailablePaperful is temporarily unavailable. Retry later.

Catch any API error

Check for PaperfulError to handle every error from the API, and let other errors, such as network failures, through.

import { PaperfulError } from "@paperful/sdk";

try {
  await paperful.papers.upload({ file });
} catch (error) {
  if (error instanceof PaperfulError) {
    console.log(error.status, error.name, error.message);
  } else {
    throw error;
  }
}

On this page