Skip to content
This repository was archived by the owner on Jul 9, 2026. It is now read-only.
This repository was archived by the owner on Jul 9, 2026. It is now read-only.

ErrorBase doesn't use proper instance and shows invalid stack traces #24

Description

@doichev-kostia

Description

Because of the following line in the HttpError in routing-controllers

Object.setPrototypeOf(this, HttpError.prototype);

The inheritance of that error causes issues with stack traces and instance logic in JS

Snippet

From the following snippet you can see that the ErrorBase is not an instance of the ErrorBase and the stack trace is wrong

import { ErrorBase, NotFound, StatusCode } from "contracts";

const e1 = new ErrorBase(StatusCode.badRequest, "badRequest", "Bad request");
console.log({e1InstanceOfError: e1 instanceof Error, e1InstanceOfErrorBase: e1 instanceof ErrorBase});

const e2 = new NotFound("notFound", "Not found");

console.log({
	e2InstanceOfError: e2 instanceof Error,
	e2InstanceOfErrorBase: e2 instanceof ErrorBase,
	e2InstanceOfNotFound: e2 instanceof NotFound,
});

function a() {
	throw new NotFound("notFound", "Not found");
}

function b() {
	a();
}

function c() {
	b();
}

try {
	c();
} catch (error) {
	if (error instanceof NotFound) {
		console.log("error is NotFound");
	}
	if (error instanceof ErrorBase) {
		console.log("error is ErrorBase");
	}
	if (error instanceof Error) {
		console.log("error is Error");
	}

	console.log({error});
	console.error(error);
}

Output:

{ e1InstanceOfError: true, e1InstanceOfErrorBase: false }
{
  e2InstanceOfError: true,
  e2InstanceOfErrorBase: false,
  e2InstanceOfNotFound: false
}
error is Error
{
  error: HttpError
      at HttpError (/Users/panenco/work/papi/node_modules/routing-controllers/cjs/http-error/HttpError.js:17:22)
      at ErrorBase (/Users/panenco/work/papi/src/contracts/errors/errorBase.error.ts:16:5)
      at NotFound (/Users/panenco/work/papi/src/contracts/errors/notFound.error.ts:10:5)
      at a (/Users/panenco/work/papi/src/test.ts:15:8)
      at b (/Users/panenco/work/papi/src/test.ts:19:2)
      at c (/Users/panenco/work/papi/src/test.ts:23:2)
      at <anonymous> (/Users/panenco/work/papi/src/test.ts:27:2)
      at Object.<anonymous> (/Users/panenco/work/papi/src/test.ts:41:1)
      at Module._compile (node:internal/modules/cjs/loader:1256:14)
      at Object.F (/Users/panenco/Library/pnpm/global/5/.pnpm/@esbuild-kit+cjs-loader@2.4.2/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:941) {
    httpCode: 404,
    message: 'Not found',
    code: 404,
    reason: 'notFound',
    payload: undefined
  }
}
HttpError
    at HttpError (/Users/panenco/work/papi/node_modules/routing-controllers/cjs/http-error/HttpError.js:17:22)
    at ErrorBase (/Users/panenco/work/papi/src/contracts/errors/errorBase.error.ts:16:5)
    at NotFound (/Users/panenco/work/papi/src/contracts/errors/notFound.error.ts:10:5)
    at a (/Users/panenco/work/papi/src/test.ts:15:8)
    at b (/Users/panenco/work/papi/src/test.ts:19:2)
    at c (/Users/panenco/work/papi/src/test.ts:23:2)
    at <anonymous> (/Users/panenco/work/papi/src/test.ts:27:2)
    at Object.<anonymous> (/Users/panenco/work/papi/src/test.ts:41:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.F (/Users/panenco/Library/pnpm/global/5/.pnpm/@esbuild-kit+cjs-loader@2.4.2/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:941) {
  httpCode: 404,
  message: 'Not found',
  code: 404,
  reason: 'notFound',
  payload: undefined
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions