Add basic error checking

This commit is contained in:
Amy 2021-11-15 23:46:25 -05:00
parent 3f3eee108c
commit 79c759075f
1 changed files with 19 additions and 7 deletions

View File

@ -3,18 +3,30 @@ import sqlite3 from "sqlite3";
const DATABASE_PATH = "catalogue.db";
export default async function getAllBooks(
export default function getAllBooks(
request: NextApiRequest,
response: NextApiResponse
) {
if (request.method !== "GET") {
response.status(405);
return;
}
const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY);
database.all(
"SELECT title, authors, isbn FROM books",
(error, rows: SimpleBook[]) => {
response.status(200).json(rows);
}
);
try {
database.all(
"SELECT title, authors, isbn FROM books",
(error: Error | null, rows: SimpleBook[]) => {
if (error) {
throw error;
}
response.status(200).json(rows);
}
);
} catch (error) {
response.status(500);
}
database.close();
}