From d42f95e71cb2132f38debee327788bcc487ec8d7 Mon Sep 17 00:00:00 2001 From: Zack Schuster Date: Wed, 27 May 2020 05:31:36 -0700 Subject: [PATCH] smtp/error: convert makeSMTPError to static method of exported SMTPError class --- smtp/error.ts | 46 +++++++++++++++++++++++++++++----------------- smtp/response.ts | 10 +++++----- smtp/smtp.ts | 16 ++++++++-------- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/smtp/error.ts b/smtp/error.ts index a56c414..ad7ee45 100644 --- a/smtp/error.ts +++ b/smtp/error.ts @@ -15,31 +15,43 @@ export const SMTPErrorStates = { CONNECTIONAUTH: 10, } as const; -class SMTPError extends Error { +export class SMTPError extends Error { public code: number | null = null; public smtp: unknown = null; public previous: Error | null = null; - constructor(message: string) { + /** + * @protected + * @param {string} message error message + */ + protected constructor(message: string) { super(message); } -} -export function makeSMTPError( - message: string, - code: number, - error?: Error | null, - smtp?: unknown -) { - const msg = error?.message ? `${message} (${error.message})` : message; - const err = new SMTPError(msg); + /** + * + * @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; + err.code = code; + err.smtp = smtp; - if (error) { - err.previous = error; + if (error) { + err.previous = error; + } + + return err; } - - return err; } diff --git a/smtp/response.ts b/smtp/response.ts index 9bd229c..4f4539c 100644 --- a/smtp/response.ts +++ b/smtp/response.ts @@ -1,4 +1,4 @@ -import { makeSMTPError, SMTPErrorStates } from './error'; +import { SMTPError, SMTPErrorStates } from './error'; import type { Socket } from 'net'; import type { TLSSocket } from 'tls'; @@ -42,7 +42,7 @@ export class SMTPResponse { const error = (err: Error) => { stream.emit( 'response', - makeSMTPError( + SMTPError.create( 'connection encountered an error', SMTPErrorStates.ERROR, err @@ -54,7 +54,7 @@ export class SMTPResponse { stream.end(); stream.emit( 'response', - makeSMTPError( + SMTPError.create( 'timedout while connecting to smtp server', SMTPErrorStates.TIMEDOUT, err @@ -72,7 +72,7 @@ export class SMTPResponse { const close = (err: Error) => { stream.emit( 'response', - makeSMTPError( + SMTPError.create( 'connection has closed', SMTPErrorStates.CONNECTIONCLOSED, err @@ -83,7 +83,7 @@ export class SMTPResponse { const end = (err: Error) => { stream.emit( 'response', - makeSMTPError( + SMTPError.create( 'connection has ended', SMTPErrorStates.CONNECTIONENDED, err diff --git a/smtp/smtp.ts b/smtp/smtp.ts index ab1f3dd..6fafd40 100644 --- a/smtp/smtp.ts +++ b/smtp/smtp.ts @@ -4,8 +4,8 @@ import { hostname } from 'os'; import { connect, createSecureContext, TLSSocket } from 'tls'; import { EventEmitter } from 'events'; +import { SMTPError, SMTPErrorStates } from './error'; import { SMTPResponse } from './response'; -import { makeSMTPError, SMTPErrorStates } from './error'; /** * @readonly @@ -251,7 +251,7 @@ export class SMTPConnection extends EventEmitter { this.close(true); caller( callback, - makeSMTPError( + SMTPError.create( 'could not establish an ssl connection', SMTPErrorStates.CONNECTIONAUTH ) @@ -274,7 +274,7 @@ export class SMTPConnection extends EventEmitter { this.log(err); caller( callback, - makeSMTPError( + SMTPError.create( 'could not connect', SMTPErrorStates.COULDNOTCONNECT, err @@ -304,7 +304,7 @@ export class SMTPConnection extends EventEmitter { this.quit(() => { caller( callback, - makeSMTPError( + SMTPError.create( 'bad response on connection', SMTPErrorStates.BADRESPONSE, err, @@ -360,7 +360,7 @@ export class SMTPConnection extends EventEmitter { this.close(true); caller( callback, - makeSMTPError( + SMTPError.create( 'no connection has been established', SMTPErrorStates.NOCONNECTION ) @@ -402,7 +402,7 @@ export class SMTPConnection extends EventEmitter { }'${suffix}`; caller( callback, - makeSMTPError( + SMTPError.create( errorMessage, SMTPErrorStates.BADRESPONSE, null, @@ -763,7 +763,7 @@ export class SMTPConnection extends EventEmitter { this.close(); // if auth is bad, close the connection, it won't get better by itself caller( callback, - makeSMTPError( + SMTPError.create( 'authorization.failed', SMTPErrorStates.AUTHFAILED, err, @@ -855,7 +855,7 @@ export class SMTPConnection extends EventEmitter { break; default: const msg = 'no form of authorization supported'; - const err = makeSMTPError( + const err = SMTPError.create( msg, SMTPErrorStates.AUTHNOTSUPPORTED, null,