mirror of https://github.com/eleith/emailjs.git
smtp/error: convert makeSMTPError to static method of exported SMTPError class
This commit is contained in:
parent
7b758cc054
commit
d42f95e71c
|
@ -15,31 +15,43 @@ export const SMTPErrorStates = {
|
||||||
CONNECTIONAUTH: 10,
|
CONNECTIONAUTH: 10,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
class SMTPError extends Error {
|
export class SMTPError extends Error {
|
||||||
public code: number | null = null;
|
public code: number | null = null;
|
||||||
public smtp: unknown = null;
|
public smtp: unknown = null;
|
||||||
public previous: Error | null = null;
|
public previous: Error | null = null;
|
||||||
|
|
||||||
constructor(message: string) {
|
/**
|
||||||
|
* @protected
|
||||||
|
* @param {string} message error message
|
||||||
|
*/
|
||||||
|
protected constructor(message: string) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export function makeSMTPError(
|
/**
|
||||||
message: string,
|
*
|
||||||
code: number,
|
* @param {string} message error message
|
||||||
error?: Error | null,
|
* @param {number} code smtp error state
|
||||||
smtp?: unknown
|
* @param {Error | null} error previous error
|
||||||
) {
|
* @param {unknown} smtp arbitrary data
|
||||||
const msg = error?.message ? `${message} (${error.message})` : message;
|
* @returns {SMTPError} error
|
||||||
const err = new SMTPError(msg);
|
*/
|
||||||
|
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.code = code;
|
||||||
err.smtp = smtp;
|
err.smtp = smtp;
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
err.previous = error;
|
err.previous = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { makeSMTPError, SMTPErrorStates } from './error';
|
import { SMTPError, SMTPErrorStates } from './error';
|
||||||
import type { Socket } from 'net';
|
import type { Socket } from 'net';
|
||||||
import type { TLSSocket } from 'tls';
|
import type { TLSSocket } from 'tls';
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ export class SMTPResponse {
|
||||||
const error = (err: Error) => {
|
const error = (err: Error) => {
|
||||||
stream.emit(
|
stream.emit(
|
||||||
'response',
|
'response',
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'connection encountered an error',
|
'connection encountered an error',
|
||||||
SMTPErrorStates.ERROR,
|
SMTPErrorStates.ERROR,
|
||||||
err
|
err
|
||||||
|
@ -54,7 +54,7 @@ export class SMTPResponse {
|
||||||
stream.end();
|
stream.end();
|
||||||
stream.emit(
|
stream.emit(
|
||||||
'response',
|
'response',
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'timedout while connecting to smtp server',
|
'timedout while connecting to smtp server',
|
||||||
SMTPErrorStates.TIMEDOUT,
|
SMTPErrorStates.TIMEDOUT,
|
||||||
err
|
err
|
||||||
|
@ -72,7 +72,7 @@ export class SMTPResponse {
|
||||||
const close = (err: Error) => {
|
const close = (err: Error) => {
|
||||||
stream.emit(
|
stream.emit(
|
||||||
'response',
|
'response',
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'connection has closed',
|
'connection has closed',
|
||||||
SMTPErrorStates.CONNECTIONCLOSED,
|
SMTPErrorStates.CONNECTIONCLOSED,
|
||||||
err
|
err
|
||||||
|
@ -83,7 +83,7 @@ export class SMTPResponse {
|
||||||
const end = (err: Error) => {
|
const end = (err: Error) => {
|
||||||
stream.emit(
|
stream.emit(
|
||||||
'response',
|
'response',
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'connection has ended',
|
'connection has ended',
|
||||||
SMTPErrorStates.CONNECTIONENDED,
|
SMTPErrorStates.CONNECTIONENDED,
|
||||||
err
|
err
|
||||||
|
|
16
smtp/smtp.ts
16
smtp/smtp.ts
|
@ -4,8 +4,8 @@ import { hostname } from 'os';
|
||||||
import { connect, createSecureContext, TLSSocket } from 'tls';
|
import { connect, createSecureContext, TLSSocket } from 'tls';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
|
|
||||||
|
import { SMTPError, SMTPErrorStates } from './error';
|
||||||
import { SMTPResponse } from './response';
|
import { SMTPResponse } from './response';
|
||||||
import { makeSMTPError, SMTPErrorStates } from './error';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @readonly
|
* @readonly
|
||||||
|
@ -251,7 +251,7 @@ export class SMTPConnection extends EventEmitter {
|
||||||
this.close(true);
|
this.close(true);
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'could not establish an ssl connection',
|
'could not establish an ssl connection',
|
||||||
SMTPErrorStates.CONNECTIONAUTH
|
SMTPErrorStates.CONNECTIONAUTH
|
||||||
)
|
)
|
||||||
|
@ -274,7 +274,7 @@ export class SMTPConnection extends EventEmitter {
|
||||||
this.log(err);
|
this.log(err);
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'could not connect',
|
'could not connect',
|
||||||
SMTPErrorStates.COULDNOTCONNECT,
|
SMTPErrorStates.COULDNOTCONNECT,
|
||||||
err
|
err
|
||||||
|
@ -304,7 +304,7 @@ export class SMTPConnection extends EventEmitter {
|
||||||
this.quit(() => {
|
this.quit(() => {
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'bad response on connection',
|
'bad response on connection',
|
||||||
SMTPErrorStates.BADRESPONSE,
|
SMTPErrorStates.BADRESPONSE,
|
||||||
err,
|
err,
|
||||||
|
@ -360,7 +360,7 @@ export class SMTPConnection extends EventEmitter {
|
||||||
this.close(true);
|
this.close(true);
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'no connection has been established',
|
'no connection has been established',
|
||||||
SMTPErrorStates.NOCONNECTION
|
SMTPErrorStates.NOCONNECTION
|
||||||
)
|
)
|
||||||
|
@ -402,7 +402,7 @@ export class SMTPConnection extends EventEmitter {
|
||||||
}'${suffix}`;
|
}'${suffix}`;
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
errorMessage,
|
errorMessage,
|
||||||
SMTPErrorStates.BADRESPONSE,
|
SMTPErrorStates.BADRESPONSE,
|
||||||
null,
|
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
|
this.close(); // if auth is bad, close the connection, it won't get better by itself
|
||||||
caller(
|
caller(
|
||||||
callback,
|
callback,
|
||||||
makeSMTPError(
|
SMTPError.create(
|
||||||
'authorization.failed',
|
'authorization.failed',
|
||||||
SMTPErrorStates.AUTHFAILED,
|
SMTPErrorStates.AUTHFAILED,
|
||||||
err,
|
err,
|
||||||
|
@ -855,7 +855,7 @@ export class SMTPConnection extends EventEmitter {
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
const msg = 'no form of authorization supported';
|
const msg = 'no form of authorization supported';
|
||||||
const err = makeSMTPError(
|
const err = SMTPError.create(
|
||||||
msg,
|
msg,
|
||||||
SMTPErrorStates.AUTHNOTSUPPORTED,
|
SMTPErrorStates.AUTHNOTSUPPORTED,
|
||||||
null,
|
null,
|
||||||
|
|
Loading…
Reference in New Issue