Debug
D1 allows you to capture exceptions and log errors returned when querying a database. To debug D1, you will use the same tools available when debugging Workers.
Handle errors
The D1 client API returns detailed error messages on the cause
property within an Error
object.
To ensure you are capturing the full error message, make sure to log or return e.cause.message
, as follows:
try {await db.exec("INSERTZ INTO my_table (name, employees) VALUES ()");} catch (e: any) {console.log({message: e.message,cause: e.cause.message,});}/*{"message": "D1_EXEC_ERROR","cause": "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"}*/
View logs
View a stream of live logs from your Worker by using wrangler tail
or via the Cloudflare dashboard.
Report issues
- To report bugs or request features, go to the Cloudflare Community Forums.
- To give feedback, go to the D1 Discord channel.
- If you are having issues with Wrangler, report issues in the Wrangler GitHub repository."
You should include as much of the following in any bug report:
- The ID of your database: use
wrangler d1 list
to match a database name to its ID. - The query, or queries, that you ran when you encountered an issue. Ensure you redact any personally identifying information (PII).
- The Worker code that makes the query, including any calls to
.bind()
using the client API. - The full error text, including the content of
error.cause.message
.
Related resources
- Learn how to debug Workers.
- Understand how to access logs generated from your Worker and D1.
- Use
wrangler dev
to run your Worker and D1 locally and debug issues before deploying.