You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/d1/observability/debug-d1.mdx
+46-2Lines changed: 46 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ D1 allows you to capture exceptions and log errors returned when querying a data
10
10
11
11
## Handle errors
12
12
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.
14
14
15
15
To ensure you are capturing the full error message, log or return `e.message` as follows:
16
16
@@ -29,6 +29,50 @@ try {
29
29
*/
30
30
```
31
31
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
+
awaitdb.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:
|`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
+
32
76
## View logs
33
77
34
78
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:
43
87
44
88
* The ID of your database. Use `wrangler d1 list` to match a database name to its ID.
45
89
* 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/).
47
91
* The full error text, including the content of [`error.cause.message`](#handle-errors).
0 commit comments