From cd0aa009f6b0c9448e55effbfa04c2d291efb684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20R=C3=A4ntil=C3=A4?= Date: Tue, 23 Mar 2021 00:06:14 +0100 Subject: [PATCH] fix(errors): made sure extend errors are named and inherited properly --- lib/errors.spec.ts | 22 ++++++++++++++++++++++ lib/errors.ts | 11 ++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 lib/errors.spec.ts diff --git a/lib/errors.spec.ts b/lib/errors.spec.ts new file mode 100644 index 0000000..3056a82 --- /dev/null +++ b/lib/errors.spec.ts @@ -0,0 +1,22 @@ +import { DuplicateError, DuplicateConstraintError } from './errors' + +describe( 'errors', ( ) => +{ + describe( 'DuplicateError', ( ) => + { + it( 'should have proper name', ( ) => + { + const err = new DuplicateError( "err" ); + expect( err.name ).toBe( "DuplicateError" ); + } ); + } ); + + describe( 'DuplicateConstraintError', ( ) => + { + it( 'should have proper name', ( ) => + { + const err = new DuplicateConstraintError( "err" ); + expect( err.name ).toBe( "DuplicateConstraintError" ); + } ); + } ); +} ); diff --git a/lib/errors.ts b/lib/errors.ts index 9ec3bd6..ef0d0b4 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -1,11 +1,20 @@ export class DuplicateError extends Error -{ } +{ + constructor( message?: string ) + { + super( message ); + Object.setPrototypeOf( this, DuplicateError.prototype ); + this.name = this.constructor.name; + } +} export class DuplicateConstraintError extends DuplicateError { constructor( field: string ) { super( `Constraint ${field} already set.` ); + Object.setPrototypeOf( this, DuplicateConstraintError.prototype ); + this.name = this.constructor.name; } }