Skip to content

Commit

Permalink
Improves error handling in database read operation
Browse files Browse the repository at this point in the history
Adds proper error handling for database fetch operations:
- Returns 404 status when resource not found
- Returns 500 status for internal server errors
- Implements async/await pattern for database calls

Makes the API more robust and provides clearer error messages to clients
  • Loading branch information
bin zhang committed Jan 19, 2025
1 parent 90b5578 commit ee69847
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/database/server/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,26 @@ export const handleReadSingle = async (req, res) => {

const id = req.params.id;
if (!isV0Id(id)) {
const result = serverDb.get(id);
return res.status(200).json({ ...result, id });
try {
const result = await serverDb.get(id);

if (!result) {
return res.status(404).json({
error: "Not Found",
message: `Resource with id ${id} not found`,
});
}

return res.status(200).json({ ...result, id });
} catch (error) {
console.error("Database fetch error:", error);
return res.status(500).json({
error: "Internal Server Error",
message: "Failed to fetch data",
});
}
}

const { isList } = extractAndDecodePrefix(id);

try {
Expand Down

0 comments on commit ee69847

Please sign in to comment.