74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
|
'use strict';
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
// Unknown Error
|
||
|
exports.UNKNOWN_ERROR = 'UNKNOWN_ERROR';
|
||
|
// Not implemented
|
||
|
exports.NOT_IMPLEMENTED = 'NOT_IMPLEMENTED';
|
||
|
// Missing new operator to an object
|
||
|
// - name: The name of the class
|
||
|
exports.MISSING_NEW = 'MISSING_NEW';
|
||
|
// Call exception
|
||
|
exports.CALL_EXCEPTION = 'CALL_EXCEPTION';
|
||
|
// Response from a server was invalid
|
||
|
// - response: The body of the response
|
||
|
//'BAD_RESPONSE',
|
||
|
// Invalid argument (e.g. type) to a function:
|
||
|
// - arg: The argument name that was invalid
|
||
|
// - value: The value of the argument
|
||
|
// - type: The type of the argument
|
||
|
// - expected: What was expected
|
||
|
exports.INVALID_ARGUMENT = 'INVALID_ARGUMENT';
|
||
|
// Missing argument to a function:
|
||
|
// - arg: The argument name that is required
|
||
|
// - count: The number of arguments received
|
||
|
// - expectedCount: The number of arguments expected
|
||
|
exports.MISSING_ARGUMENT = 'MISSING_ARGUMENT';
|
||
|
// Too many arguments
|
||
|
// - count: The number of arguments received
|
||
|
// - expectedCount: The number of arguments expected
|
||
|
exports.UNEXPECTED_ARGUMENT = 'UNEXPECTED_ARGUMENT';
|
||
|
// Numeric Fault
|
||
|
// - operation: the operation being executed
|
||
|
// - fault: the reason this faulted
|
||
|
exports.NUMERIC_FAULT = 'NUMERIC_FAULT';
|
||
|
// Unsupported operation
|
||
|
// - operation
|
||
|
exports.UNSUPPORTED_OPERATION = 'UNSUPPORTED_OPERATION';
|
||
|
// @TODO: Enum
|
||
|
function throwError(message, code, params) {
|
||
|
if (!code) {
|
||
|
code = exports.UNKNOWN_ERROR;
|
||
|
}
|
||
|
if (!params) {
|
||
|
params = {};
|
||
|
}
|
||
|
var messageDetails = [];
|
||
|
Object.keys(params).forEach(function (key) {
|
||
|
try {
|
||
|
messageDetails.push(key + '=' + JSON.stringify(params[key]));
|
||
|
}
|
||
|
catch (error) {
|
||
|
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
|
||
|
}
|
||
|
});
|
||
|
var reason = message;
|
||
|
if (messageDetails.length) {
|
||
|
message += ' (' + messageDetails.join(', ') + ')';
|
||
|
}
|
||
|
// @TODO: Any??
|
||
|
var error = new Error(message);
|
||
|
error.reason = reason;
|
||
|
error.code = code;
|
||
|
Object.keys(params).forEach(function (key) {
|
||
|
error[key] = params[key];
|
||
|
});
|
||
|
throw error;
|
||
|
}
|
||
|
exports.throwError = throwError;
|
||
|
function checkNew(self, kind) {
|
||
|
if (!(self instanceof kind)) {
|
||
|
throwError('missing new', exports.MISSING_NEW, { name: kind.name });
|
||
|
}
|
||
|
}
|
||
|
exports.checkNew = checkNew;
|