Repository: fastify/fastify
File: test/encapsulated-error-handler.test.js (JavaScript)

Code before:
      // 3. the inner error handler catches the error, and throws a new error
      t.equal(err.message, 'from_endpoint')
      throw new Error('from_inner')
    })
    fastify.get('/encapsulated', async () => {
      // 2. the endpoint throws an error
      throw new Error('from_endpoint')
    })
  })

  fastify.setErrorHandler(async function b (err) {
    // 4. the outer error handler catches the error thrown by the inner error handler
    t.equal(err.message, 'from_inner')
    // 5. the outer error handler throws a new error
    throw new Error('from_outer')
  })

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // 6. the default error handler returns the error from the outer error handler
  t.equal(res.json().message, 'from_outer')
})

// See discussion in https://github.com/fastify/fastify/pull/5222#discussion_r1432573655
test('encapsulates a synchronous error handler', async t => {
  t.plan(4)

  const fastify = Fastify()
  fastify.register(async function (fastify) {
    fastify.setErrorHandler(function a (err) {
      // 3. the inner error handler catches the error, and throws a new error
      t.equal(err.message, 'from_endpoint')
      throw new Error('from_inner')
    })
    fastify.get('/encapsulated', async () => {
      // 2. the endpoint throws an error
      throw new Error('from_endpoint')
    })
  })

  fastify.setErrorHandler(async function b (err) {
    // 4. the outer error handler catches the error thrown by the inner error handler
    t.equal(err.message, 'from_inner')
    // 5. the outer error handler throws a new error
    throw new Error('from_outer')
  })

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // 6. the default error handler returns the error from the outer error handler
  t.equal(res.json().message, 'from_outer')

Review comment (suggestion):
```suggestion
  t.plan(3)
```

Why `t.plan` is `4` but not `3`?
I can see three `t.equal` only and none of the code path run twice.

Code after:
      // 3. the inner error handler catches the error, and throws a new error
      t.equal(err.message, 'from_endpoint')
      throw new Error('from_inner')
    })
    fastify.get('/encapsulated', async () => {
      // 2. the endpoint throws an error
      throw new Error('from_endpoint')
    })
  })

  fastify.setErrorHandler(async function b (err) {
    // 4. the outer error handler catches the error thrown by the inner error handler
    t.equal(err.message, 'from_inner')
    // 5. the outer error handler throws a new error
    throw new Error('from_outer')
  })

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // 6. the default error handler returns the error from the outer error handler
  t.equal(res.json().message, 'from_outer')
})

// See discussion in https://github.com/fastify/fastify/pull/5222#discussion_r1432573655
test('encapsulates a synchronous error handler', async t => {
  t.plan(3)

  const fastify = Fastify()
  fastify.register(async function (fastify) {
    fastify.setErrorHandler(function a (err) {
      // 3. the inner error handler catches the error, and throws a new error
      t.equal(err.message, 'from_endpoint')
      throw new Error('from_inner')
    })
    fastify.get('/encapsulated', async () => {
      // 2. the endpoint throws an error
      throw new Error('from_endpoint')
    })
  })

  fastify.setErrorHandler(async function b (err) {
    // 4. the outer error handler catches the error thrown by the inner error handler
    t.equal(err.message, 'from_inner')
    // 5. the outer error handler throws a new error
    throw new Error('from_outer')
  })

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // 6. the default error handler returns the error from the outer error handler
  t.equal(res.json().message, 'from_outer')