smtp/error: convert makeSMTPError to static method of exported SMTPError class

This commit is contained in:
Zack Schuster 2020-05-27 05:31:36 -07:00
parent 7b758cc054
commit d42f95e71c
3 changed files with 42 additions and 30 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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,