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

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

Open
doichev-kostia opened this issue Sep 18, 2023 · 0 comments
Assignees

Comments

@doichev-kostia
Copy link
Member

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
}
@doichev-kostia doichev-kostia self-assigned this Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant