fastify / fastify-sensible

Defaults for Fastify that everyone can agree on
MIT License
462 stars 34 forks source link

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

Open tmcw opened 1 week ago

tmcw commented 1 week 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 1 week ago

Can you provide a PR?

tmcw commented 1 week ago

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