wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
48
49
50
51
52
53
54
55
56
57
/**
 * @readonly
 * @enum
 */
export const SMTPErrorStates = {
    COULDNOTCONNECT: 1,
    BADRESPONSE: 2,
    AUTHFAILED: 3,
    TIMEDOUT: 4,
    ERROR: 5,
    NOCONNECTION: 6,
    AUTHNOTSUPPORTED: 7,
    CONNECTIONCLOSED: 8,
    CONNECTIONENDED: 9,
    CONNECTIONAUTH: 10,
} as const;
 
export class SMTPError extends Error {
    public code: number | null = null;
    public smtp: unknown = null;
    public previous: Error | null = null;
 
    /**
     * @protected
     * @param {string} message error message
     */
    protected constructor(message: string) {
        super(message);
    }
 
    /**
     *
     * @param {string} message error message
     * @param {number} code smtp error state
     * @param {Error | null} error previous error
     * @param {unknown} smtp arbitrary data
     * @returns {SMTPError} error
     */
    public static create(
        message: string,
        code: number,
        error?: Error | null,
        smtp?: unknown
    ) {
        const msg = error?.message ? `${message} (${error.message})` : message;
        const err = new SMTPError(msg);
 
        err.code = code;
        err.smtp = smtp;
 
        if (error) {
            err.previous = error;
        }
 
        return err;
    }
}