Skip to content

Commit fac3401

Browse files
committed
Moving Error section away from "Query D1".
1 parent e1d47f0 commit fac3401

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/content/docs/d1/observability/debug-d1.mdx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ D1 allows you to capture exceptions and log errors returned when querying a data
1010

1111
## Handle errors
1212

13-
The D1 [client API](/d1/build-with-d1/d1-client-api/) returns detailed [error messages](/d1/build-with-d1/d1-client-api/#errors) within an `Error` object.
13+
The D1 [Workers Binding API](/d1/worker-api/) returns detailed error messages within an `Error` object.
1414

1515
To ensure you are capturing the full error message, log or return `e.message` as follows:
1616

@@ -29,6 +29,50 @@ try {
2929
*/
3030
```
3131

32+
### Errors
33+
34+
The [`stmt.`](/d1/worker-api/prepared-statements/) and [`db.`](/d1/worker-api/d1-database/) methods throw an [Error object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) whenever an error occurs.
35+
36+
:::note
37+
Prior to [`wrangler` 3.1.1](https://github.com/cloudflare/workers-sdk/releases/tag/wrangler%403.1.1), D1 JavaScript errors used the [cause property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) for detailed error messages.
38+
39+
To inspect these errors when using older versions of `wrangler`, you should log `error?.cause?.message`.
40+
:::
41+
42+
To capture exceptions, log the `Error.message` value. For example, the code below has a query with an invalid keyword - `INSERTZ` instead of `INSERT`:
43+
44+
```js
45+
try {
46+
// This is an intentional mispelling
47+
await db.exec("INSERTZ INTO my_table (name, employees) VALUES ()");
48+
} catch (e: any) {
49+
console.error({
50+
message: e.message
51+
});
52+
}
53+
```
54+
55+
The code above throws the following error message:
56+
57+
```json
58+
{
59+
"message": "D1_EXEC_ERROR: Error in line 1: INSERTZ INTO my_table (name, employees) VALUES (): sql error: near \"INSERTZ\": syntax error in INSERTZ INTO my_table (name, employees) VALUES () at offset 0"
60+
}
61+
```
62+
63+
### Error list
64+
65+
D1 returns the following error constants, in addition to the extended (detailed) error message:
66+
67+
| Message | Cause |
68+
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
69+
| `D1_ERROR` | Generic error. |
70+
| `D1_TYPE_ERROR` | Returned when there is a mismatch in the type between a column and a value. A common cause is supplying an `undefined` variable (unsupported) instead of `null`. |
71+
| `D1_COLUMN_NOTFOUND` | Column not found. |
72+
| `D1_DUMP_ERROR` | Database dump error. |
73+
| `D1_EXEC_ERROR` | Exec error in line x: y error. |
74+
75+
3276
## View logs
3377

3478
View a stream of live logs from your Worker by using [`wrangler tail`](/workers/observability/logs/real-time-logs#view-logs-using-wrangler-tail) or via the [Cloudflare dashboard](/workers/observability/logs/real-time-logs#view-logs-from-the-dashboard).
@@ -43,7 +87,7 @@ You should include as much of the following in any bug report:
4387

4488
* The ID of your database. Use `wrangler d1 list` to match a database name to its ID.
4589
* The query (or queries) you ran when you encountered an issue. Ensure you redact any personally identifying information (PII).
46-
* The Worker code that makes the query, including any calls to `bind()` using the [client API](/d1/build-with-d1/d1-client-api/).
90+
* The Worker code that makes the query, including any calls to `bind()` using the [Workers Binding API](/d1/worker-api/).
4791
* The full error text, including the content of [`error.cause.message`](#handle-errors).
4892

4993
## Related resources

0 commit comments

Comments
 (0)