/
database-error.ts
47 lines (39 loc) · 1.08 KB
/
database-error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import BaseError, { CommonErrorProperties, ErrorOptions } from './base-error';
export interface DatabaseErrorParent
extends Error,
Pick<CommonErrorProperties, 'sql'> {
/** The parameters for the sql that triggered the error */
readonly parameters?: object;
}
export interface DatabaseErrorSubclassOptions extends ErrorOptions {
parent?: DatabaseErrorParent;
message?: string;
}
/**
* A base class for all database related errors.
*/
class DatabaseError
extends BaseError
implements DatabaseErrorParent, CommonErrorProperties
{
parent: Error;
original: Error;
sql: string;
parameters: object;
/**
* @param parent The database specific error which triggered this one
* @param options
*/
constructor(parent: DatabaseErrorParent, options: ErrorOptions = {}) {
super(parent.message);
this.name = 'SequelizeDatabaseError';
this.parent = parent;
this.original = parent;
this.sql = parent.sql;
this.parameters = parent.parameters ?? {};
if (options.stack) {
this.stack = options.stack;
}
}
}
export default DatabaseError;