Skip to main content

Error handling

@markpdf/nextjs reuses exceptions from @markpdf/sdk (MarkpdfAuthError, MarkpdfRateLimitError, etc.), all children of MarkpdfError. How you handle them depends on whether you are in a Route Handler or a Server Action.

In a Route Handler

createConvertRouteHandler already translates any MarkpdfError into a HTTP response with the same status code and a body { error: string }:
If you build your own Route Handler with getServerClient, capture it yourself:
app/api/convert/route.ts
Don’t propagate 401/403 from the API to the end user as is — those errors mean that your own API key is misconfigured, not that the user did something wrong. Return a generic 500 and record the detail in your server logs.

In a Server Action

Exceptions thrown within a Server Action arrive to the client as a serialized promise rejection. Capture them explicitly if you want to display a specific message in the UI instead of the generic Next.js error screen:
app/actions.ts
Returning a { ok, error } object instead of letting the exception propagate is the pattern recommended by Next.js for Server Actions — it gives you full control over what message the user sees, without exposing internal details of the API.

Reintentos

getServerClient accepts the same retry options as MarkpdfClient:
See Node.js SDK Error Handling for details on which codes are retried.