Debugging your application
In a real-world application, errors may happen! So, how can we handle them within our structure? Yes, you guessed right, with a plugin!
In production, the logs files are our debugger. So, first of all, it is important to write good and secure logs. Let’s create a new plugins/error-handler.js plugin file:
const fp = require('fastify-plugin')
module.exports = fp(function (fastify, opts, next) {
  fastify.setErrorHandler((err, req, reply) => {
    if (reply.statusCode >= 500) {
      req.log.error({ req, res: reply, err: err },
      err?.message)
      reply.send(`Fatal error. Contact the support team. Id
      ${req.id}`)
      return
    }
    req.log.info({ req, res: reply, err: err },
    ...