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:
| Field | Description |
|---|---|
status | The HTTP status. |
name | The error's name, such as "not-found". Also available as the ErrorName enum. |
message | A description of what went wrong. |
retry | true when the request can be retried. |
many | For 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
| Status | Class | Name | Description |
|---|---|---|---|
400 | BadRequest | bad-request | The request was invalid. |
400 | ValidationError | validation | One or more parameters are invalid, such as an expired cursor. |
401 | Unauthorized | unauthorized | The API key is missing or invalid. |
403 | PermissionDenied | permission-denied | The API key can't access this resource. |
404 | NotFound | not-found | The resource doesn't exist. |
408 | TimedOut | timed-out | The request took too long. |
409 | Conflict | conflict | The request conflicts with the resource's current state. |
413 | EntityTooLarge | entity-too-large | The file is larger than your plan allows. |
415 | UnsupportedMediaType | unsupported-media-type | The file type isn't supported, or its name couldn't be inferred. |
422 | InvalidData | invalid-data | The data couldn't be processed. |
429 | TooManyRequests | too-many-requests | You've hit a rate limit. |
500 | InternalError | internal-error | Something went wrong on Paperful's side. |
501 | NotImplemented | not-implemented | The operation isn't supported. |
503 | ServiceUnavailable | service-unavailable | Paperful 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;
}
}