fastify / fastify-error

MIT License
102 stars 20 forks source link

Implement RFC7807 #64

Open Uzlopak opened 2 years ago

Uzlopak commented 2 years ago

Prerequisites

🚀 Feature Proposal

We could implement a toRFC7807-method, which creates RFC7807-compatible JSON Response to FastifyError and use it in fastify 4 by default.

So we could create a new Error like this


  const NewError = createError('CODE', 'hey %s, I like your %s %s')
  const err = new NewError('alice', 'attitude', 'see you')

and then the resulting JSON would be:

{
    type: "FastifyError",
    title: "FastifyError", 
    status: 500, // <-- statusCode
    detail: 'hey alice, I like your attitude', // <-- message 
    instance: '', // <-- here could be the route
    code: 'CODE',
    details: { }, // here could be the ajv error codes.
}

Motivation

No response

Example

No response

mcollina commented 2 years ago

This would be interesting. It might be too late for Fastify v4 but would you like to assemble a PR anyway?

Uzlopak commented 2 years ago

Is fastify-error the right place? Or should it be done in a different place?

mcollina commented 2 years ago

It should be done both here and in Fastify

Uzlopak commented 11 months ago

Reopening for #119

mcollina commented 11 months ago

How about we ads a new factory for RFC7807 errors?

jsumners commented 11 months ago

How about we ads a new factory for RFC7807 errors?

Seems like a good solution.