I’m quite new to Nuxt and I’ve been moved on a Nuxt3 project recently.
I want to have every of the server api endpoint to handle their business logic in a try-catch
and in case of error format an error log (I’ve successfully introduced winston).
Given this desiderata I stumbled upon Nuxt3 own server/utils
example that does exactly what I need, the only problem I know nothing about typescript and the docs doesn’t show how to use the provided example.
I converted the example to plain JS using an online converter and got this:
export const defineWrappedResponseHandler = handler =>
defineEventHandler(async event => {
try {
// do something before the route handler
const response = await handler(event)
// do something after the route handler
return { response }
} catch (err) {
// Error handling
return { err }
}
})
but still: how do I use this code into my apis?
I put the converted code into server/utils/customEventHandler.js
and tried with
export default customEventHandler(
defineEventHandler(async (event) => {
// do buiness logic and return data
})
);
but calling the endpoint (from the browser) I get a 500 response with
{
"url": "/api/account/user/login",
"statusCode": 500,
"statusMessage": "",
"message": "Invalid lazy handler result. It should be a function:",
"stack": ""
}