

# 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
<a name="_the_error_object"></a>

Check `error` after each call:

```ts
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:

```bash
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
<a name="_empty_results_from_row_level_security"></a>

When no row-level security (RLS) policy that you configured matches the caller, you see one of the following results:
+  **A read returns `[]`, but `aws amplifygen3 db query` shows rows.** No SELECT policy matches the caller’s role. `db query` connects 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. If `data` is 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 `409` `WRITE_CONFLICT`.** No INSERT policy matches the caller, or the new row fails the `with check` expression of the policy.

**Warning**  
 **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](backend-rls.md).

## Retries
<a name="_retries"></a>

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
<a name="_error_codes"></a>

The following table lists data API error codes and how to fix them:


|  `code`  | HTTP status | Meaning | Fix | 
| --- | --- | --- | --- | 
|  `PGRST205`  | 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 `aws amplifygen3 db query "notify pgrst, 'reload schema'" --env <env>`  | 
|  `PGRST202`  | 404 | The function that `rpc` calls doesn’t exist, or the data API hasn’t loaded it | Apply the migration that creates it. For a function that you created with `aws amplifygen3 db query`, reload the schema. For more information, see [Call database functions](backend-data-api-rpc.md)  | 
|  `PGRST125`  | 404 | The path has a version, such as `/rest/v1/notes`  | Remove the version, as in `/rest/notes`  | 
|  `PGRST101`  | 405 | A method other than `GET`, `HEAD`, `POST`, or `OPTIONS` on `/rest/rpc/<name>`  | Call the function with `POST`, or with `GET` for a function that only reads | 
|  `PGRST201`  | 300 | More than one foreign key joins the two tables. The `details` field lists them | Name the foreign key column or constraint after `!`. For more information, see [Read data](backend-data-api-query.md)  | 
|  `COUNT_NOT_SUPPORTED`  | 400 | The request asked for a planned or estimated count | Use `count: 'exact'`  | 
|  `PGRST200`  | 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 | 
|  `PREFER_NOT_ASCII`  | 400 | A `Prefer` header value has characters outside visible ASCII | Send only visible ASCII characters in `Prefer`  | 
|  `PGRST116`  | 406 |  `.single()` matched zero rows or several | Filter so that one row matches, or use `.maybeSingle()`  | 
|  `42703`  | 400 | The query names a column that doesn’t exist | Fix the name, and regenerate your TypeScript types | 
|  `42501`  | 401 or 403 |  `permission denied for …​`: the caller’s role has no grant (401 signed out, 403 signed in) | If that role should have access, grant it in a new migration. For a table, view or sequence, the `hint` names the missing grant. For a function, the `hint` is empty: grant `execute`, such as `grant execute on function public.<name>(<argument types>) to authenticated;`  | 
|  `42883`  | 404 |  `operator does not exist: uuid = text`: a function compares a `uuid` column with `auth.uid()`, which returns `text`  | Make the column `text`, or cast one side so that the types match | 
|  `WRITE_CONFLICT`  | 409 | The write violates a unique key, a foreign key, an exclusion constraint, or an RLS `with check` expression. The response names no constraint or policy. Only a `service_role` request gets the original SQLSTATE, such as `23505`  | Read the cause in the database log: `aws amplifygen3 logs get --env <env> --log-type database --filter ERROR`  | 
|  `57014`  | 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 | 
|  `PGRST000`, `PGRST001`, `PGRST003`, `DB_UNAVAILABLE`  | 503 or 504 | The data API couldn’t get a database connection, such as while the database restarts | Retry after the `Retry-After` time. The SDK has already retried it, up to 4 times with the publishable key or a user token, or 3 times when signed | 
|  `40P01`  | 503 | A deadlock. Nothing was committed, and the response has `Retry-After: 1`  | Send the request again. To avoid deadlocks, lock rows in the same order in each transaction | 
|  `GATEWAY_TIMEOUT`  | 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 | 
|  `SERVICE_UNAVAILABLE`  | 503 | The backend is busy | Send the request again after the `Retry-After` time | 
|  `THROTTLED`  | 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 | 
|  `invalid_credential`  | 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 `invalid credential`  | For each message and its fix, see [API keys and roles](backend-keys.md)  | 
|  `REQUEST_BLOCKED`  | 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 | 
|  `FORBIDDEN`  | 403 | A signed `service_role` request from an identity that has no `amplifygen3:Connect` permission on the branch | Grant `amplifygen3:Connect` on the branch’s ARN, from `aws amplifygen3 backend info`  | 
|  `PAYLOAD_TOO_LARGE`  | 413 | The body is over the limit for its path: 10 MiB on `/rest` and 1 MiB on `/auth`, or 1 MiB for a signed `service_role` request | 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 name="_requests_blocked_by_cors"></a>

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](backend-auth-config.md).