summary refs log tree commit diff
path: root/src/util/error.js
blob: 9816b6509bfb221d8c7ddbde7c5fb39c0a6046ad (plain) (blame)
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
export class SafeNSoundError extends Error {
    /**
     * @param options {SafeNSoundError}
     */
    constructor(options) {
        super();
        if (typeof options === 'string') {
            this.errCode = options;
            super.message = this.getDefaultMessage();
        } else if (typeof options === 'object') {
            for (const key in options) {
                this[key] = options[key];
            }
        } else {
            this.errCode = 'UNKNOWN_ERROR';
            this.message =
                'An unknown error occurred (invalid SafeNSoundError constructor options)';
        }
    }

    getDefaultMessage() {
        switch (this.type) {
            case 'USER_NOT_FOUND':
                return 'User not found';
            case 'INVALID_CREDENTIALS':
                return 'Invalid credentials';
            case 'EMAIL_ALREADY_EXISTS':
                return 'Email already exists';
            case 'PASSWORD_TOO_WEAK':
                return 'Password is too weak';
            case 'TOKEN_EXPIRED':
                return 'Token has expired';
            case 'UNAUTHORIZED':
                return 'Unauthorized access';
            case 'UNKNOWN_ERROR':
            default:
                return 'An unknown error occurred';
        }
    }
}