Error handling

Controller error handler

server/api/tasks/controller.ts
import { defineController } from './$relay'
import { createTask } from '$/service/tasks'
export default defineController(() => ({
post: async ({ body }) => {
try {
const task = await createTask(body.label)
return { status: 201, body: task }
} catch (e) {
return { status: 500, body: 'Something broke!' }
}
}
}))

The default error handler

Fastify onError

server/service/app.ts
import Fastify from 'fastify'
import server from './$server'
const fastify = Fastify()
server(fastify, { basePath: '/api/v1' })
fastify.addHook('onError', (req, reply, err) => {
console.error(err.stack)
})
fastify.listen(3000)