AI Image Editor error handling
Two things happen when a generation or edit fails. The editor handles the
user: it shows a
localized message in place
and stays usable, with no wiring needed on your side. Your app gets the
uc:error event for everything the built-in UI can’t know about: logging,
metrics, or reacting to specific failures.
The event bubbles and crosses shadow DOM boundaries (composed), so a
listener on a container, or on document, works too.
The error object
detail.error is always an AiImageEditorError. Whatever actually went
wrong (a platform rejection, a network failure, even a custom provider
throwing a string) is normalized into it:
Error codes
code is the field to branch on:
Every failure is also logged to the console ([uc-ai-image-editor], with the
code and the error object), so a code you haven’t wired up still shows up while
debugging.
The known codes come in four families:
PascalCase codes come from Upload API. snake_case codes come from Uploadcare’s AI image API, except auth_token_failed, which the editor raises when your authToken function fails.
Your handler gets every code. The on-screen message is less specific: codes the user can act on get their own wording, and the rest, including every token failure, share a generic one. The localization guide lists each message.
AiImageEditorErrorCode is deliberately open: the known codes are typed as
literals (you get autocomplete for them), but the backend can introduce new
ones at any time, so any other string flows through rather than breaking.
Don’t treat the union as closed; keep a default branch.
When your auth token function fails
A throw from an authToken
function never reaches the backend, so there is no server code to report. It
arrives as code: 'auth_token_failed', with the
AuthTokenResolverError
on cause and whatever your function threw on cause.cause:
To tell a lost session from a dead endpoint, include your endpoint’s HTTP status
in what the function throws, then check it on cause.cause: a 401 means the
user has to sign in again, while a 500 or a network error is worth retrying.
Inside File Uploader you don’t set the token: the editor uses the uploader’s
token function and its cache. You can still get this error there. If that
function throws while a generation is running, it surfaces as
auth_token_failed on the editor’s uc:error, so listen for it either way.
Enabling AI Image Editor
derivative_disabled means AI generation isn’t enabled for your project. The
visitor can’t act on it, so the editor only tells them “Image generation is
disabled for this site.” Watch for the code on uc:error or in the console
instead.
The code has two causes: AI is turned off for the project, or the workspace’s
plan doesn’t include AI Image Editor. Turn it on in
Project settings > AI > Enable for this project, or
upgrade to Pro, Business, or a custom plan.
See Project AI toggle.
Workspace request limit reached
derivative_quota_exceeded means the workspace has reached its request limit
for the rolling 24 hours. The visitor sees “Too many images have been generated
recently. Please try again later.” Watch for the code on uc:error or in the
console. Requests succeed again as older ones leave the 24-hour window. If the
workspace owner set a lower limit in Billing > Usage control, they can raise
it up to the plan maximum.
See
Workspace request limit.
For direct API calls, if the response includes a Retry-After header, it gives
the number of seconds to wait before retrying.
Token operations used up
OperationsLimitExceededError means the auth token’s
uc.restrictions.limits.operations limit is used up. The visitor sees “You’ve
reached the limit for generating images for now. Please try again later.” Have
your backend sign a new token. With a token function, call
invalidateAuthToken()
so the editor fetches it before the next request. With a plain token, set
authToken to the new one. See
Token operations.
React
The React wrapper delivers the same object to
onError:
One React-specific case: if the lazily-loaded editor engine itself fails to
load (e.g. a chunk request dies on a flaky connection), onError receives an
AiImageEditorError with code: 'engine_load_failed' and the fallback stays
rendered. That code is frontend-originated, so it never appears in the backend
families above.
Customizing the in-editor messages
The message the editor shows for a code is a locale string, overridable per-code without touching your event handling:
Each code has its own key, so overriding one changes that code alone: several
share the same default text, and changing one of those leaves the others as
they were. A code with no key (one this version has never heard of) falls back
to the generic ai-image-editor-error. See
Localization for the
mechanics and the full key list.
Server-side code
instanceof AiImageEditorError works in server code too. Import the class from
the side-effect-free errors entry, which is safe where the main entry is not
(the main entry registers custom elements):
What doesn’t fire uc:error
- Cancellations. Closing the editor fires
uc:cancel; superseding or aborting an in-flight generation (Start over, unmount) is swallowed silently, since an abort is a user action rather than a failure. - Image display failures. If a result generated fine but its image fails
to load into the canvas, that surfaces as a separate
uc:image-errorevent (detail.urlnames the failing URL). The generation itself didn’t fail.