Skip to content

Commit

Permalink
[D1] lift error.cause into the error message (#3414)
Browse files Browse the repository at this point in the history
* [D1] lift error.cause into the error message

* Create itchy-experts-change.md

* fix: log the actual variable
  • Loading branch information
rozenmd authored Jun 7, 2023
1 parent a9c55bd commit 6b1870a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .changeset/itchy-experts-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: in D1, lift error.cause into the error message

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.
60 changes: 36 additions & 24 deletions packages/wrangler/templates/d1-beta-facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ var D1Database = class {
if (response.status !== 200) {
try {
const err = await response.json();
throw new Error("D1_DUMP_ERROR", {
throw new Error(`D1_DUMP_ERROR: ${err.error}`, {
cause: new Error(err.error),
});
} catch (e) {
throw new Error("D1_DUMP_ERROR", {
throw new Error(`D1_DUMP_ERROR: Status + ${response.status}`, {
cause: new Error("Status " + response.status),
});
}
Expand All @@ -49,16 +49,21 @@ var D1Database = class {
})
.indexOf(1);
if (error !== -1) {
throw new Error("D1_EXEC_ERROR", {
cause: new Error(
"Error in line " +
(error + 1) +
": " +
lines[error] +
": " +
exec[error].error
),
});
throw new Error(
`D1_EXEC_ERROR: Error in line ${error + 1}: ${lines[error]}: ${
exec[error].error
}`,
{
cause: new Error(
"Error in line " +
(error + 1) +
": " +
lines[error] +
": " +
exec[error].error
),
}
);
} else {
return {
count: exec.length,
Expand Down Expand Up @@ -90,14 +95,16 @@ var D1Database = class {
const answer = await response.json();
if (answer.error && dothrow) {
const err = answer;
throw new Error("D1_ERROR", { cause: new Error(err.error) });
throw new Error(`D1_ERROR: ${err.error}`, {
cause: new Error(err.error),
});
} else {
return Array.isArray(answer)
? answer.map((r) => mapD1Result(r))
: mapD1Result(answer);
}
} catch (e) {
throw new Error("D1_ERROR", {
throw new Error(`D1_ERROR: ${e.cause || "Something went wrong"}`, {
cause: new Error(e.cause || "Something went wrong"),
});
}
Expand Down Expand Up @@ -135,15 +142,20 @@ var D1PreparedStatement = class {
break;
}
default:
throw new Error("D1_TYPE_ERROR", {
cause: new Error(
"Type '" +
typeof values[r] +
"' not supported for value '" +
values[r] +
"'"
),
});
throw new Error(
`D1_TYPE_ERROR: Type '${typeof values[
r
]}' not supported for value '${values[r]}'`,
{
cause: new Error(
"Type '" +
typeof values[r] +
"' not supported for value '" +
values[r] +
"'"
),
}
);
}
}
return new D1PreparedStatement(this.database, this.statement, values);
Expand All @@ -155,7 +167,7 @@ var D1PreparedStatement = class {
const results = info.results;
if (colName !== void 0) {
if (results.length > 0 && results[0][colName] === void 0) {
throw new Error("D1_COLUMN_NOTFOUND", {
throw new Error(`D1_COLUMN_NOTFOUND: Column not found (${colName})`, {
cause: new Error("Column not found"),
});
}
Expand Down

0 comments on commit 6b1870a

Please sign in to comment.