Skip to content

Commit d67cd0d

Browse files
Do not report "d1 execute" command file missing error to Sentry (#9163)
1 parent 798aa39 commit d67cd0d

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

.changeset/social-cups-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Do not report "d1 execute" command file missing error to Sentry

packages/wrangler/src/__tests__/d1/execute.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ describe("execute", () => {
137137
)
138138
);
139139
});
140+
141+
it("should throw a UserError if file does not exist", async () => {
142+
setIsTTY(false);
143+
writeWranglerConfig({
144+
d1_databases: [
145+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
146+
],
147+
});
148+
149+
await expect(runWrangler(`d1 execute db --file missing.sql --local --json`))
150+
.rejects.toThrowErrorMatchingInlineSnapshot(`
151+
[Error: {
152+
"error": {
153+
"text": "Unable to read SQL text file /"missing.sql/". Please check the file path and try again."
154+
}
155+
}]
156+
`);
157+
});
140158
});
141159

142160
function useSentry() {

packages/wrangler/src/d1/execute.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,17 @@ function shorten(query: string | undefined, length: number) {
599599
}
600600

601601
async function checkForSQLiteBinary(filename: string) {
602-
const fd = await fs.open(filename, "r");
603602
const buffer = Buffer.alloc(15);
604-
await fd.read(buffer, 0, 15);
603+
604+
try {
605+
const fd = await fs.open(filename, "r");
606+
await fd.read(buffer, 0, 15);
607+
} catch (e) {
608+
throw new UserError(
609+
`Unable to read SQL text file "${filename}". Please check the file path and try again.`
610+
);
611+
}
612+
605613
if (buffer.toString("utf8") === "SQLite format 3") {
606614
throw new UserError(
607615
"Provided file is a binary SQLite database file instead of an SQL text file. The execute command can only process SQL text files. Please export an SQL file from your SQLite database and try again."

0 commit comments

Comments
 (0)