Data API errors
When your AWS Amplify app calls the Amplify backend data API and the request fails, the client SDK returns an error object with a code and doesn’t throw.
The error object
Check error after each call:
const { data, error } = await backend.from('notes').select('id, body') if (error) { // code, message, details and hint; requestId when the backend sent a response console.error(error.code, error.message, error.requestId) }
requestId is the x-request-id header of the response. The access log of a deployed environment has a sample of requests. Run the following command to find a request in it:
aws amplifygen3 logs get --env production --log-type access --filter '{ $.request_id = "<request-id>" }' --since 1h
By default, the access log keeps a 5 percent sample of requests on production and none on preview. On preview, the command prints No access logs exist for environment 'preview' until you turn delivery on with aws amplifygen3 logs-config edit --access-log-cwl-sampling-rate 100 --env preview.
Empty results from row-level security
When no row-level security (RLS) policy that you configured matches the caller, you see one of the following results:
-
A read returns
[], butaws amplifygen3 db queryshows rows. No SELECT policy matches the caller’s role.db queryconnects as an admin role that RLS doesn’t apply to. -
An update or delete succeeds but changes nothing. To check, add
.select()to the call. Ifdatais an empty array, no row changed. An update changes only the rows that the caller can select, so add a SELECT policy and an UPDATE policy in a new migration. -
An insert gets
409WRITE_CONFLICT. No INSERT policy matches the caller, or the new row fails thewith checkexpression of the policy.
Warning: Don’t turn off RLS or switch to service_role to fix an empty result. Either one exposes every row in the table.
For more information about writing and testing policies, see Secure your data.
Retries
The client SDK sends a failed request again before it returns the error. It retries a request that gets 429, for any method. It retries a 502, 503, 504 or network error only for GET and HEAD requests, because a write might already have run. The backend adds a Retry-After header to a 5xx response only when the request didn’t run. The SDK retries those responses for any method.
The SDK sends a request with the publishable key or a user token up to 4 times, and a signed service_role request up to 3 times. To turn retries off, create the client with db: { retry: false }.
Error codes
The following table lists data API error codes and how to fix them:
code
|
HTTP status | Meaning | Fix |
|---|---|---|---|
|
|
404 |
The table doesn’t exist, its migration isn’t applied, or you created it outside a migration |
Apply the migration, or reload the schema with |
|
|
404 |
The function that |
Apply the migration that creates it. For a function that you created with |
|
|
404 |
The path has a version, such as |
Remove the version, as in |
|
|
405 |
A method other than |
Call the function with |
|
|
300 |
More than one foreign key joins the two tables. The |
Name the foreign key column or constraint after |
|
|
400 |
The request asked for a planned or estimated count |
Use |
|
|
400 |
The query embeds a table that has no foreign key to the table that it reads |
Add the foreign key in a migration, or remove the embed |
|
|
400 |
A |
Send only visible ASCII characters in |
|
|
406 |
|
Filter so that one row matches, or use |
|
|
400 |
The query names a column that doesn’t exist |
Fix the name, and regenerate your TypeScript types |
|
|
401 or 403 |
|
If that role should have access, grant it in a new migration. For a table, view or sequence, the |
|
|
404 |
|
Make the column |
|
|
409 |
The write violates a unique key, a foreign key, an exclusion constraint, or an RLS |
Read the cause in the database log: |
|
|
500 |
The statement ran past the time limit of its role |
Add indexes for the query and for the RLS policies that you configured on its tables, and page through large results |
|
|
503 or 504 |
The data API couldn’t get a database connection, such as while the database restarts |
Retry after the |
|
|
503 |
A deadlock. Nothing was committed, and the response has |
Send the request again. To avoid deadlocks, lock rows in the same order in each transaction |
|
|
504 |
The backend didn’t answer within 60 seconds. The request might have been applied |
Check whether a write was applied before you send it again |
|
|
503 |
The backend is busy |
Send the request again after the |
|
|
429 |
Your requests exceeded the request rate of the branch or account, or the capacity of the branch |
Back off, batch writes and cache reads |
|
|
401 |
The backend refused the publishable key or the token. The message names the refused credential, except for a token whose session ended with sign-out, a ban, or the user’s removal, which gets |
For each message and its fix, see API keys and roles |
|
|
Usually 403 |
A firewall rule (AWS WAF) on the CDN blocked the request before it reached the backend, often because of text that looks like script or markup. The access log has no record of it |
Find the rule that matched in the firewall logs |
|
|
403 |
A signed |
Grant |
|
|
413 |
The body is over the limit for its path: 10 MiB on |
Split the body into smaller requests, such as batches of inserts |
A request with no User-Agent header gets a plain-text 403 with an empty code, and details holds the reason.
An expired token gets 401
invalid_credential with the message The bearer token is not valid for this backend. A token also stops working within 30 seconds after its session ends, and gets the message invalid credential. The local backend doesn’t check sessions. When the token comes from the client’s own session, the SDK reads the session again and retries once with a new token. If the session is gone, later requests run as anon.
Requests blocked by CORS
A deployed backend answers browser requests only from origins that it trusts: the environment’s Amplify URLs, its custom domains and its recent preview URLs. From any other origin, the browser blocks the response, and the SDK gets a network error with no status and no code. To fix it, add the origin to trustedOrigins under backend.auth in amplify.jsonc, and deploy. For more information, see Auth configuration.