forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.base.ts
41 lines (37 loc) · 1 KB
/
exception.base.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
import { ObjectLiteral } from '../types';
import { Exceptions } from './exception.types';
export interface SerializedException {
name: string;
message: string;
stack?: string;
metadata?: ObjectLiteral;
}
/**
* Base class for custom exceptions.
*
* @abstract
* @class ExceptionBase
* @extends {Error}
*/
export abstract class ExceptionBase extends Error {
/**
* @param {string} message
* @param {ObjectLiteral} [metadata={}]
* **BE CAREFUL** not to include sensitive info in 'metadata' to prevent leaks since
* all exception's data will end up in application's log files. Only include non-sensitive
* info that may help with debugging.
*/
constructor(readonly message: string, readonly metadata?: ObjectLiteral) {
super(message);
Error.captureStackTrace(this, this.constructor);
}
abstract name: Exceptions;
toJSON(): SerializedException {
return {
name: this.name,
message: this.message,
stack: this.stack,
metadata: this.metadata,
};
}
}