fastify-sensible icon indicating copy to clipboard operation
fastify-sensible copied to clipboard

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

Open tmcw opened this issue 1 year ago • 2 comments
trafficstars

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

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

tmcw avatar Sep 09 '24 20:09 tmcw

Can you provide a PR?

Uzlopak avatar Sep 09 '24 20:09 Uzlopak

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

tmcw avatar Sep 10 '24 14:09 tmcw

I have created a PR @Uzlopak Please review

lallenfrancisl avatar Nov 04 '24 19:11 lallenfrancisl