Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Unsupported Media Type Error #2899

Closed
matthyk opened this issue Mar 5, 2021 · 8 comments
Closed

Custom Unsupported Media Type Error #2899

matthyk opened this issue Mar 5, 2021 · 8 comments

Comments

@matthyk
Copy link
Contributor

matthyk commented Mar 5, 2021

馃殌 Feature Proposal

Currently, if no matching content type parser is available, a predefined Unsupported Media Type error is thrown. There should be the possibility that the user can define this error object himself.

Motivation

This would have 2 advantages:

  1. If users have a default format for errors that looks different from Fastify's current one, then they could use that format on the Unsupported Media Type.
  2. Users could define different errors in different plugins.

Example

This could be implemented like the addContentTypeParser API.

fastify.setUnsupportedMediaTypeError({
    "statusCode": 415,
    "error": "My Unsupported Media Type",
    "message": "This media type is currently not supported."
})

or like this, which would give the user even more possibilities:

fastify.setUnsupportedMediaTypeError((req, contentType, done) => {
    const myError = {
        requestedContentType: contentType,
        "statusCode": 415,
        "error": "My Unsupported Media Type",
        requestId: req.id
    }
    done(null, myError)
})
@matthyk
Copy link
Contributor Author

matthyk commented Mar 5, 2021

Are you open for a PR?

@RafaelGSS
Copy link
Member

Nowadays, you can't check it in the errorHandler?

@matthyk
Copy link
Contributor Author

matthyk commented Mar 6, 2021

Yes it is possible to check in the errorHandler but I think that is not that clean. And you do not have the possibility to define different error shapes in different plugins.

@mcollina
Copy link
Member

mcollina commented Mar 6, 2021

Instead of focusing on making this customizable, we should focus on allowing the error handler to call the one of the plugin above it. In this way we could even have route-specific error handlers.

@matthyk
Copy link
Contributor Author

matthyk commented Mar 10, 2021

After looking into the code again, I realized that I made a mistake. it is NOT possible to catch the error in the errorHandler. The content type parser runs reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType)) directly. As I understand it, @mcollina proposal could not solve the problem then either.

@mcollina
Copy link
Member

reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType)) will call the error handler.

@alainrk
Copy link
Contributor

alainrk commented Jul 27, 2021

Instead of focusing on making this customizable, we should focus on allowing the error handler to call the one of the plugin above it. In this way we could even have route-specific error handlers.

@mcollina Doesn't it work like that right now already?
I'm new to using fastify, but isn't it enough to just don't call the setErrorHandler() to have the error propagated to the closest above plugin calling the setErrorHandler().

I tried to write a test for it, that passes, but maybe I'm missing something.

test('custom root and custom plugin error handling with nested plugin throwing', t => {
    t.plan(3)

    const fastify = Fastify()

    fastify.register((instance, options, done) => {

      instance.setErrorHandler((err, req, reply) => {
        throw new Error('/a error')
      })

      instance.register((subinstance, options, done) => {
        subinstance.get('/c', function (req, reply) {
          throw new Error('/a/b/c error')
        })
        done()
      }, { prefix: '/b' })

      done()
    }, { prefix: '/a' })

    fastify.setErrorHandler((err, req, reply) => {
      throw new Error('/ error')
    })

    fastify.inject({
      url: '/a/b/c',
      method: 'GET'
    }, (err, res) => {
      t.error(err)
      t.equal(res.statusCode, 500)
      const payload = JSON.parse(res.payload)
      t.same(payload.message,'/a error')
    })
  })

@Eomm
Copy link
Member

Eomm commented Jan 28, 2022

Done in #3261

It will be released in fastify v4

@Eomm Eomm closed this as completed Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants