Skip to content

Commit 6b1870a

Browse files
authored
[D1] lift error.cause into the error message (#3414)
* [D1] lift error.cause into the error message * Create itchy-experts-change.md * fix: log the actual variable
1 parent a9c55bd commit 6b1870a

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

.changeset/itchy-experts-change.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: in D1, lift error.cause into the error message
6+
7+
Prior to this PR, folks _had_ to console.log the `error.cause` property to understand why their D1 operations were failing. With this PR, `error.cause` will continue to work, but we'll also lift the cause into the error message.

packages/wrangler/templates/d1-beta-facade.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ var D1Database = class {
2020
if (response.status !== 200) {
2121
try {
2222
const err = await response.json();
23-
throw new Error("D1_DUMP_ERROR", {
23+
throw new Error(`D1_DUMP_ERROR: ${err.error}`, {
2424
cause: new Error(err.error),
2525
});
2626
} catch (e) {
27-
throw new Error("D1_DUMP_ERROR", {
27+
throw new Error(`D1_DUMP_ERROR: Status + ${response.status}`, {
2828
cause: new Error("Status " + response.status),
2929
});
3030
}
@@ -49,16 +49,21 @@ var D1Database = class {
4949
})
5050
.indexOf(1);
5151
if (error !== -1) {
52-
throw new Error("D1_EXEC_ERROR", {
53-
cause: new Error(
54-
"Error in line " +
55-
(error + 1) +
56-
": " +
57-
lines[error] +
58-
": " +
59-
exec[error].error
60-
),
61-
});
52+
throw new Error(
53+
`D1_EXEC_ERROR: Error in line ${error + 1}: ${lines[error]}: ${
54+
exec[error].error
55+
}`,
56+
{
57+
cause: new Error(
58+
"Error in line " +
59+
(error + 1) +
60+
": " +
61+
lines[error] +
62+
": " +
63+
exec[error].error
64+
),
65+
}
66+
);
6267
} else {
6368
return {
6469
count: exec.length,
@@ -90,14 +95,16 @@ var D1Database = class {
9095
const answer = await response.json();
9196
if (answer.error && dothrow) {
9297
const err = answer;
93-
throw new Error("D1_ERROR", { cause: new Error(err.error) });
98+
throw new Error(`D1_ERROR: ${err.error}`, {
99+
cause: new Error(err.error),
100+
});
94101
} else {
95102
return Array.isArray(answer)
96103
? answer.map((r) => mapD1Result(r))
97104
: mapD1Result(answer);
98105
}
99106
} catch (e) {
100-
throw new Error("D1_ERROR", {
107+
throw new Error(`D1_ERROR: ${e.cause || "Something went wrong"}`, {
101108
cause: new Error(e.cause || "Something went wrong"),
102109
});
103110
}
@@ -135,15 +142,20 @@ var D1PreparedStatement = class {
135142
break;
136143
}
137144
default:
138-
throw new Error("D1_TYPE_ERROR", {
139-
cause: new Error(
140-
"Type '" +
141-
typeof values[r] +
142-
"' not supported for value '" +
143-
values[r] +
144-
"'"
145-
),
146-
});
145+
throw new Error(
146+
`D1_TYPE_ERROR: Type '${typeof values[
147+
r
148+
]}' not supported for value '${values[r]}'`,
149+
{
150+
cause: new Error(
151+
"Type '" +
152+
typeof values[r] +
153+
"' not supported for value '" +
154+
values[r] +
155+
"'"
156+
),
157+
}
158+
);
147159
}
148160
}
149161
return new D1PreparedStatement(this.database, this.statement, values);
@@ -155,7 +167,7 @@ var D1PreparedStatement = class {
155167
const results = info.results;
156168
if (colName !== void 0) {
157169
if (results.length > 0 && results[0][colName] === void 0) {
158-
throw new Error("D1_COLUMN_NOTFOUND", {
170+
throw new Error(`D1_COLUMN_NOTFOUND: Column not found (${colName})`, {
159171
cause: new Error("Column not found"),
160172
});
161173
}

0 commit comments

Comments
 (0)