fastify / fastify-sensible

Defaults for Fastify that everyone can agree on
MIT License
467 stars 37 forks source link

httpErrors.HttpError type export should be a class, not an interface #167

Open tmcw opened 2 months ago

tmcw commented 2 months ago

Prerequisites

Fastify version

4.28.1

Plugin version

5.6.0

Node.js version

v20.16.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

14.6.1 (23G93)

Description

This module exports a class at httpErrors.HttpError, so I should be able to write an errorHandler in fastify like this:

    errorHandler(error, _request, reply) {
      if (error instanceof httpErrors.HttpError) {
        return error;
      }
      return reply.internalServerError();
    }

However, this doesn't type-check: httpErrors.HttpError is an interface in the type exports:

https://github.com/fastify/fastify-sensible/blob/822ae7a895e7e8296429ea38e2cf4060aaf19144/lib/httpError.d.ts#L1-L9

And you can't instanceof an interface, because it's a type, not a value. Most likely, HttpError should be a class.

Link to code that reproduces the bug

No response

Expected Behavior

No response

Uzlopak commented 2 months ago

Can you provide a PR?

tmcw commented 2 months ago

Yep, I'm trying to, it's turning out to be tricky to implement.

lallenfrancisl commented 1 week ago

I have created a PR @Uzlopak Please review