Skip to content

Commit

Permalink
fix(errors): made sure extend errors are named and inherited properly
Browse files Browse the repository at this point in the history
  • Loading branch information
grantila committed Mar 22, 2021
1 parent 3e80084 commit cd0aa00
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -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" );
} );
} );
} );
11 changes: 10 additions & 1 deletion lib/errors.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit cd0aa00

Please sign in to comment.