cloudflare / cloudflare/workerd

Feature request: add offset to SQLite's error mesages

Open
#1,391 3 comments 3 reactions 0 assignees View on GitHub
feature request
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

## The issue

While looking into https://github.com/cloudflare/workers-sdk/issues/4343 I noticed a difference between how workerd/d1 reports errors, and how SQLite shell reports errors:

```
wrangler d1 execute DBNAME --local --command='CREATE TABLE accounts (
id tinytext NOT NULL,
username varchar(32) NOT NULL,
disabled tinyint(1) NOT NULL DEFAULT 0,
email varchar(255) NOT NULL,
password tinytext NOT NULL,
oplvl tinyint(4) NOT NULL DEFAULT 0,
created timestamp NOT NULL DEFAULT current_timestamp(),
lastchange timestamp NOT NULL DEFAULT current_timestamp(),
token tinytext NOT NULL DEFAULT '',
session tinytext NOT NULL DEFAULT ''
);'

X [ERROR] near "(": syntax error
```
vs
```
sqlite> CREATE TABLE accounts (
id tinytext NOT NULL,
username varchar(32) NOT NULL,
disabled tinyint(1) NOT NULL DEFAULT 0,
email varchar(255) NOT NULL,
password tinytext NOT NULL,
oplvl tinyint(4) NOT NULL DEFAULT 0,
created timestamp NOT NULL DEFAULT current_timestamp(),
lastchange timestamp NOT NULL DEFAULT current_timestamp(),
token tinytext NOT NULL DEFAULT '',
session tinytext NOT NULL DEFAULT ''
);
Parse error: near "(": syntax error
eated timestamp NOT NULL DEFAULT current_timestamp(), lastchange timestamp N
error here ---^
```

## Some context

It turns out this is because the sqlite shell implements nice errors here: https://github.com/sqlite/sqlite/blob/master/src/shell.c.in#L3097-L3101, while regular sqlite doesn't return the exact location of the error.

## Potential fix

Back in SQLite Release 3.38.4, we got access to the `sqlite3_error_offset()` interface:

> Added the [sqlite3_error_offset()](https://www.sqlite.org/c3ref/errcode.html) interface, which can sometimes help to localize an SQL error to a specific character in the input SQL text, so that applications can provide better error messages.

Using it, we could make the error message returned from workerd more like:

```
X [ERROR] near "(" at offset 17: syntax error
```

rather than:
```
X [ERROR] near "(": syntax error
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.