317 lines
9.0 KiB
JavaScript
317 lines
9.0 KiB
JavaScript
var decycle = require('./decycle');
|
|
|
|
var isStrict = (function () { return !this; })();
|
|
|
|
function AuthTokenExpiredError(message, expiry) {
|
|
this.name = 'AuthTokenExpiredError';
|
|
this.message = message;
|
|
this.expiry = expiry;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
AuthTokenExpiredError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function AuthTokenInvalidError(message) {
|
|
this.name = 'AuthTokenInvalidError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
AuthTokenInvalidError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function AuthTokenNotBeforeError(message, date) {
|
|
this.name = 'AuthTokenNotBeforeError';
|
|
this.message = message;
|
|
this.date = date;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
AuthTokenNotBeforeError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
// For any other auth token error.
|
|
function AuthTokenError(message) {
|
|
this.name = 'AuthTokenError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
AuthTokenError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function SilentMiddlewareBlockedError(message, type) {
|
|
this.name = 'SilentMiddlewareBlockedError';
|
|
this.message = message;
|
|
this.type = type;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
SilentMiddlewareBlockedError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function InvalidActionError(message) {
|
|
this.name = 'InvalidActionError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
InvalidActionError.prototype = Object.create(Error.prototype);
|
|
|
|
function InvalidArgumentsError(message) {
|
|
this.name = 'InvalidArgumentsError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
InvalidArgumentsError.prototype = Object.create(Error.prototype);
|
|
|
|
function InvalidOptionsError(message) {
|
|
this.name = 'InvalidOptionsError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
InvalidOptionsError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function InvalidMessageError(message) {
|
|
this.name = 'InvalidMessageError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
InvalidMessageError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function SocketProtocolError(message, code) {
|
|
this.name = 'SocketProtocolError';
|
|
this.message = message;
|
|
this.code = code;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
SocketProtocolError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function ServerProtocolError(message) {
|
|
this.name = 'ServerProtocolError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
ServerProtocolError.prototype = Object.create(Error.prototype);
|
|
|
|
function HTTPServerError(message) {
|
|
this.name = 'HTTPServerError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
HTTPServerError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function ResourceLimitError(message) {
|
|
this.name = 'ResourceLimitError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
ResourceLimitError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function TimeoutError(message) {
|
|
this.name = 'TimeoutError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
TimeoutError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function BadConnectionError(message, type) {
|
|
this.name = 'BadConnectionError';
|
|
this.message = message;
|
|
this.type = type;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
BadConnectionError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function BrokerError(message) {
|
|
this.name = 'BrokerError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
BrokerError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function ProcessExitError(message, code) {
|
|
this.name = 'ProcessExitError';
|
|
this.message = message;
|
|
this.code = code;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
ProcessExitError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
function UnknownError(message) {
|
|
this.name = 'UnknownError';
|
|
this.message = message;
|
|
if (Error.captureStackTrace && !isStrict) {
|
|
Error.captureStackTrace(this, arguments.callee);
|
|
} else {
|
|
this.stack = (new Error()).stack;
|
|
}
|
|
}
|
|
UnknownError.prototype = Object.create(Error.prototype);
|
|
|
|
|
|
// Expose all error types
|
|
|
|
module.exports = {
|
|
AuthTokenExpiredError: AuthTokenExpiredError,
|
|
AuthTokenInvalidError: AuthTokenInvalidError,
|
|
AuthTokenNotBeforeError: AuthTokenNotBeforeError,
|
|
AuthTokenError: AuthTokenError,
|
|
SilentMiddlewareBlockedError: SilentMiddlewareBlockedError,
|
|
InvalidActionError: InvalidActionError,
|
|
InvalidArgumentsError: InvalidArgumentsError,
|
|
InvalidOptionsError: InvalidOptionsError,
|
|
InvalidMessageError: InvalidMessageError,
|
|
SocketProtocolError: SocketProtocolError,
|
|
ServerProtocolError: ServerProtocolError,
|
|
HTTPServerError: HTTPServerError,
|
|
ResourceLimitError: ResourceLimitError,
|
|
TimeoutError: TimeoutError,
|
|
BadConnectionError: BadConnectionError,
|
|
BrokerError: BrokerError,
|
|
ProcessExitError: ProcessExitError,
|
|
UnknownError: UnknownError
|
|
};
|
|
|
|
module.exports.socketProtocolErrorStatuses = {
|
|
1001: 'Socket was disconnected',
|
|
1002: 'A WebSocket protocol error was encountered',
|
|
1003: 'Server terminated socket because it received invalid data',
|
|
1005: 'Socket closed without status code',
|
|
1006: 'Socket hung up',
|
|
1007: 'Message format was incorrect',
|
|
1008: 'Encountered a policy violation',
|
|
1009: 'Message was too big to process',
|
|
1010: 'Client ended the connection because the server did not comply with extension requirements',
|
|
1011: 'Server encountered an unexpected fatal condition',
|
|
4000: 'Server ping timed out',
|
|
4001: 'Client pong timed out',
|
|
4002: 'Server failed to sign auth token',
|
|
4003: 'Failed to complete handshake',
|
|
4004: 'Client failed to save auth token',
|
|
4005: 'Did not receive #handshake from client before timeout',
|
|
4006: 'Failed to bind socket to message broker',
|
|
4007: 'Client connection establishment timed out'
|
|
};
|
|
|
|
module.exports.socketProtocolIgnoreStatuses = {
|
|
1000: 'Socket closed normally',
|
|
1001: 'Socket hung up'
|
|
};
|
|
|
|
// Properties related to error domains cannot be serialized.
|
|
var unserializableErrorProperties = {
|
|
domain: 1,
|
|
domainEmitter: 1,
|
|
domainThrown: 1
|
|
};
|
|
|
|
module.exports.dehydrateError = function (error, includeStackTrace) {
|
|
var dehydratedError;
|
|
if (!error || typeof error == 'string') {
|
|
dehydratedError = error;
|
|
} else {
|
|
dehydratedError = {
|
|
message: error.message
|
|
};
|
|
if (includeStackTrace) {
|
|
dehydratedError.stack = error.stack;
|
|
}
|
|
for (var i in error) {
|
|
if (!unserializableErrorProperties[i]) {
|
|
dehydratedError[i] = error[i];
|
|
}
|
|
}
|
|
}
|
|
return decycle(dehydratedError);
|
|
};
|
|
|
|
module.exports.hydrateError = function (error) {
|
|
var hydratedError = null;
|
|
if (error != null) {
|
|
if (typeof error == 'string') {
|
|
hydratedError = error;
|
|
} else {
|
|
hydratedError = new Error(error.message);
|
|
for (var i in error) {
|
|
if (error.hasOwnProperty(i)) {
|
|
hydratedError[i] = error[i];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return hydratedError;
|
|
};
|