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

Fix/errors #25

Merged
merged 5 commits into from
Sep 18, 2023
Merged

Fix/errors #25

merged 5 commits into from
Sep 18, 2023

Conversation

doichev-kostia
Copy link
Member

Proposed changes

According to #24, I fixed the inheritance and the stack traces problem

1. HttpError was providing invalid instance information for inherited errors due to `Object.setPrototypeOf(this, HttpError.prototype);`
2. Capture the stack trace properly
@doichev-kostia
Copy link
Member Author

Test file

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: true }
{
  e2InstanceOfError: true,
  e2InstanceOfErrorBase: true,
  e2InstanceOfNotFound: true
}
error is NotFound
error is ErrorBase
error is Error
{
  error: NotFound: Not found
      at a (/Users/panenco/work/papi/src/test.ts:15:9)
      at b (/Users/panenco/work/papi/src/test.ts:19:3)
      at c (/Users/panenco/work/papi/src/test.ts:23:3)
      at <anonymous> (/Users/panenco/work/papi/src/test.ts:27:3)
      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)
      at Module.load (node:internal/modules/cjs/loader:1119:32)
      at Module._load (node:internal/modules/cjs/loader:960:12)
      at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29) {
    code: 404,
    reason: 'notFound',
    payload: undefined
  }
}
NotFound: Not found
    at a (/Users/panenco/work/papi/src/test.ts:15:9)
    at b (/Users/panenco/work/papi/src/test.ts:19:3)
    at c (/Users/panenco/work/papi/src/test.ts:23:3)
    at <anonymous> (/Users/panenco/work/papi/src/test.ts:27:3)
    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)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29) {
  code: 404,
  reason: 'notFound',
  payload: undefined
}

@github-actions
Copy link
Contributor


=============================== Coverage summary ===============================
Statements   : 93.58% ( 321/343 )
Branches     : 74.84% ( 122/163 )
Functions    : 88.23% ( 75/85 )
Lines        : 93.89% ( 277/295 )
================================================================================

@doichev-kostia doichev-kostia merged commit 3e3ece6 into main Sep 18, 2023
1 check passed
@doichev-kostia doichev-kostia deleted the fix/errors branch September 18, 2023 12:02
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

Successfully merging this pull request may close these issues.

2 participants