Skip to content

Commit 252975b

Browse files
committed
Add error handling utilities for not found errors in notes
1 parent e6e046d commit 252975b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

utils/error.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
function createNotFoundError(message) {
4+
const error = new Error(message)
5+
error.name = 'NotFoundError'
6+
error.statusCode = 404
7+
return error
8+
}
9+
10+
async function handleReadNoteError(error, request, reply) {
11+
if (error.name === 'NotFoundError') {
12+
const { id } = request.params
13+
request.log.info(`Note not found for ID: ${id} and User ID: ${request.user.id}`)
14+
await reply.status(404).send({
15+
statusCode: 404,
16+
error: 'Not Found',
17+
message: error.message,
18+
requestId: request.id,
19+
})
20+
} else {
21+
request.log.error('Error reading note:', error)
22+
await reply.send(error)
23+
}
24+
}
25+
26+
module.exports = { createNotFoundError, handleReadNoteError }

0 commit comments

Comments
 (0)