Skip to content

Commit d42f95e

Browse files
committed
smtp/error: convert makeSMTPError to static method of exported SMTPError class
1 parent 7b758cc commit d42f95e

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

smtp/error.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,43 @@ export const SMTPErrorStates = {
1515
CONNECTIONAUTH: 10,
1616
} as const;
1717

18-
class SMTPError extends Error {
18+
export class SMTPError extends Error {
1919
public code: number | null = null;
2020
public smtp: unknown = null;
2121
public previous: Error | null = null;
2222

23-
constructor(message: string) {
23+
/**
24+
* @protected
25+
* @param {string} message error message
26+
*/
27+
protected constructor(message: string) {
2428
super(message);
2529
}
26-
}
2730

28-
export function makeSMTPError(
29-
message: string,
30-
code: number,
31-
error?: Error | null,
32-
smtp?: unknown
33-
) {
34-
const msg = error?.message ? `${message} (${error.message})` : message;
35-
const err = new SMTPError(msg);
31+
/**
32+
*
33+
* @param {string} message error message
34+
* @param {number} code smtp error state
35+
* @param {Error | null} error previous error
36+
* @param {unknown} smtp arbitrary data
37+
* @returns {SMTPError} error
38+
*/
39+
public static create(
40+
message: string,
41+
code: number,
42+
error?: Error | null,
43+
smtp?: unknown
44+
) {
45+
const msg = error?.message ? `${message} (${error.message})` : message;
46+
const err = new SMTPError(msg);
3647

37-
err.code = code;
38-
err.smtp = smtp;
48+
err.code = code;
49+
err.smtp = smtp;
3950

40-
if (error) {
41-
err.previous = error;
42-
}
51+
if (error) {
52+
err.previous = error;
53+
}
4354

44-
return err;
55+
return err;
56+
}
4557
}

smtp/response.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeSMTPError, SMTPErrorStates } from './error';
1+
import { SMTPError, SMTPErrorStates } from './error';
22
import type { Socket } from 'net';
33
import type { TLSSocket } from 'tls';
44

@@ -42,7 +42,7 @@ export class SMTPResponse {
4242
const error = (err: Error) => {
4343
stream.emit(
4444
'response',
45-
makeSMTPError(
45+
SMTPError.create(
4646
'connection encountered an error',
4747
SMTPErrorStates.ERROR,
4848
err
@@ -54,7 +54,7 @@ export class SMTPResponse {
5454
stream.end();
5555
stream.emit(
5656
'response',
57-
makeSMTPError(
57+
SMTPError.create(
5858
'timedout while connecting to smtp server',
5959
SMTPErrorStates.TIMEDOUT,
6060
err
@@ -72,7 +72,7 @@ export class SMTPResponse {
7272
const close = (err: Error) => {
7373
stream.emit(
7474
'response',
75-
makeSMTPError(
75+
SMTPError.create(
7676
'connection has closed',
7777
SMTPErrorStates.CONNECTIONCLOSED,
7878
err
@@ -83,7 +83,7 @@ export class SMTPResponse {
8383
const end = (err: Error) => {
8484
stream.emit(
8585
'response',
86-
makeSMTPError(
86+
SMTPError.create(
8787
'connection has ended',
8888
SMTPErrorStates.CONNECTIONENDED,
8989
err

smtp/smtp.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { hostname } from 'os';
44
import { connect, createSecureContext, TLSSocket } from 'tls';
55
import { EventEmitter } from 'events';
66

7+
import { SMTPError, SMTPErrorStates } from './error';
78
import { SMTPResponse } from './response';
8-
import { makeSMTPError, SMTPErrorStates } from './error';
99

1010
/**
1111
* @readonly
@@ -251,7 +251,7 @@ export class SMTPConnection extends EventEmitter {
251251
this.close(true);
252252
caller(
253253
callback,
254-
makeSMTPError(
254+
SMTPError.create(
255255
'could not establish an ssl connection',
256256
SMTPErrorStates.CONNECTIONAUTH
257257
)
@@ -274,7 +274,7 @@ export class SMTPConnection extends EventEmitter {
274274
this.log(err);
275275
caller(
276276
callback,
277-
makeSMTPError(
277+
SMTPError.create(
278278
'could not connect',
279279
SMTPErrorStates.COULDNOTCONNECT,
280280
err
@@ -304,7 +304,7 @@ export class SMTPConnection extends EventEmitter {
304304
this.quit(() => {
305305
caller(
306306
callback,
307-
makeSMTPError(
307+
SMTPError.create(
308308
'bad response on connection',
309309
SMTPErrorStates.BADRESPONSE,
310310
err,
@@ -360,7 +360,7 @@ export class SMTPConnection extends EventEmitter {
360360
this.close(true);
361361
caller(
362362
callback,
363-
makeSMTPError(
363+
SMTPError.create(
364364
'no connection has been established',
365365
SMTPErrorStates.NOCONNECTION
366366
)
@@ -402,7 +402,7 @@ export class SMTPConnection extends EventEmitter {
402402
}'${suffix}`;
403403
caller(
404404
callback,
405-
makeSMTPError(
405+
SMTPError.create(
406406
errorMessage,
407407
SMTPErrorStates.BADRESPONSE,
408408
null,
@@ -763,7 +763,7 @@ export class SMTPConnection extends EventEmitter {
763763
this.close(); // if auth is bad, close the connection, it won't get better by itself
764764
caller(
765765
callback,
766-
makeSMTPError(
766+
SMTPError.create(
767767
'authorization.failed',
768768
SMTPErrorStates.AUTHFAILED,
769769
err,
@@ -855,7 +855,7 @@ export class SMTPConnection extends EventEmitter {
855855
break;
856856
default:
857857
const msg = 'no form of authorization supported';
858-
const err = makeSMTPError(
858+
const err = SMTPError.create(
859859
msg,
860860
SMTPErrorStates.AUTHNOTSUPPORTED,
861861
null,

0 commit comments

Comments
 (0)