Skip to content

Commit da667e1

Browse files
committed
エラー発生方法を変更して修正
1 parent e7cb471 commit da667e1

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

03.asynchronous/async_await/existing_error.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ try {
1212
);
1313
console.log("テーブルが作成されました。");
1414

15-
const title = "Node.js入門";
1615
try {
1716
const result = await run(db, "INSERT INTO books (title) VALUES (?)", [
18-
title,
17+
"Node.js入門",
1918
]);
2019
console.log(`行が追加されました。id: ${result.lastID}`);
21-
await run(db, "INSERT INTO books (title) VALUES (?)", [title]);
20+
await run(db, "INSERT INTO books (nonexistent_column) VALUES (?)", [
21+
"Node.js入門",
22+
]);
2223
} catch (err) {
23-
if (
24-
title === "Node.js入門" &&
25-
String(err).includes(
26-
"SQLITE_CONSTRAINT: UNIQUE constraint failed: books.title",
27-
)
28-
) {
29-
console.error(`エラーが発生しました: ${String(err)}`);
24+
const columnError = err.message.match(
25+
/SQLITE_ERROR: table books has no column named (\w+)/,
26+
);
27+
if (columnError) {
28+
console.error(`エラーが発生しました: ${columnError[0]}`);
3029
} else {
3130
throw err;
3231
}

03.asynchronous/callback/existing_error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const db = new sqlite3.Database(":memory:", () => {
1717
console.log(`行が追加されました。id: ${this.lastID}`);
1818

1919
db.run(
20-
"INSERT INTO books (title) VALUES (?)",
20+
"INSERT INTO books (nonexistent_column) VALUES (?)",
2121
["Node.js入門"],
2222
(err) => {
2323
if (err) {

03.asynchronous/promise/existing_error.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ createDatabase(":memory:")
2121
})
2222
.then((result) => {
2323
console.log(`行が追加されました。id: ${result.lastID}`);
24-
return run(db, "INSERT INTO books (title) VALUES (?)", ["Node.js入門"]);
24+
return run(db, "INSERT INTO books (nonexistent_column) VALUES (?)", [
25+
"Node.js入門",
26+
]);
2527
})
2628
.catch((err) => {
2729
console.error(`エラーが発生しました: ${err.message}`);

0 commit comments

Comments
 (0)