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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC-7807 for errors #415

Open
franky47 opened this issue Dec 6, 2021 · 0 comments
Open

RFC-7807 for errors #415

franky47 opened this issue Dec 6, 2021 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@franky47
Copy link
Member

franky47 commented Dec 6, 2021

In combination with fastify.sensible, make it easy to send well-formatted errors to clients, with actionable details for better DX.

Specification: https://tools.ietf.org/html/rfc7807

Copy-pasta from another project:

// https://tools.ietf.org/html/rfc7807

import { STATUS_CODES } from 'node:http'
import { z } from 'zod'

export const rfc7807Schema = z.object({
  status: z
    .number()
    .int()
    .refine(code => code in STATUS_CODES),
  type: z.string().url().optional(), // Technically a URI, but let's not split hairs
  title: z.string(),
  detail: z.string().optional(),
  instance: z.string().url().optional()
})

export type RFC7807 = z.TypeOf<typeof rfc7807Schema>

export class RFC7807Error extends Error {
  readonly rfc7807: RFC7807

  constructor(args: RFC7807) {
    super(
      `${STATUS_CODES[args.status]}: ${args.title}${
        args.detail ? ` (${args.detail})` : ''
      }`
    )
    this.name = 'RFC7807Error'
    this.rfc7807 = {
      ...args,
      type: args.type ?? 'about:blank'
    }
  }
}
@franky47 franky47 added the enhancement New feature or request label Dec 6, 2021
@franky47 franky47 mentioned this issue Dec 20, 2021
5 tasks
@franky47 franky47 added this to the v4 milestone Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant