2018-04-12 22:18:11 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var defineProperty = require('./properties').defineProperty;
|
|
|
|
|
|
|
|
var codes = { };
|
|
|
|
|
|
|
|
[
|
|
|
|
// Unknown Error
|
|
|
|
'UNKNOWN_ERROR',
|
|
|
|
|
2018-04-14 01:21:48 +03:00
|
|
|
// Not implemented
|
|
|
|
'NOT_IMPLEMENTED',
|
|
|
|
|
2018-04-12 22:18:11 +03:00
|
|
|
// Missing new operator to an object
|
|
|
|
// - name: The name of the class
|
|
|
|
'MISSING_NEW',
|
|
|
|
|
2018-04-14 01:21:48 +03:00
|
|
|
|
2018-04-17 04:42:17 +03:00
|
|
|
// Call exception
|
|
|
|
'CALL_EXCEPTION',
|
|
|
|
|
|
|
|
|
|
|
|
// Response from a server was invalid
|
|
|
|
// - response: The body of the response
|
|
|
|
//'BAD_RESPONSE',
|
|
|
|
|
|
|
|
|
2018-04-14 01:21:48 +03:00
|
|
|
// Invalid argument (e.g. type) to a function:
|
2018-04-12 22:18:11 +03:00
|
|
|
// - arg: The argument name that was invalid
|
2018-04-17 04:42:17 +03:00
|
|
|
// - value: The value of the argument
|
|
|
|
// - type: The type of the argument
|
|
|
|
// - expected: What was expected
|
2018-04-14 01:21:48 +03:00
|
|
|
'INVALID_ARGUMENT',
|
|
|
|
|
|
|
|
// Missing argument to a function:
|
|
|
|
// - arg: The argument name that is required
|
2018-04-17 04:42:17 +03:00
|
|
|
// - count: The number of arguments received
|
|
|
|
// - expectedCount: The number of arguments expected
|
2018-04-14 01:21:48 +03:00
|
|
|
'MISSING_ARGUMENT',
|
|
|
|
|
|
|
|
// Too many arguments
|
2018-04-17 04:42:17 +03:00
|
|
|
// - count: The number of arguments received
|
|
|
|
// - expectedCount: The number of arguments expected
|
2018-04-14 01:21:48 +03:00
|
|
|
'UNEXPECTED_ARGUMENT',
|
|
|
|
|
|
|
|
|
|
|
|
// Unsupported operation
|
|
|
|
// - operation
|
|
|
|
'UNSUPPORTED_OPERATION',
|
|
|
|
|
2018-04-12 22:18:11 +03:00
|
|
|
|
|
|
|
].forEach(function(code) {
|
|
|
|
defineProperty(codes, code, code);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
defineProperty(codes, 'throwError', function(message, code, params) {
|
|
|
|
if (!code) { code = codes.UNKNOWN_ERROR; }
|
|
|
|
if (!params) { params = {}; }
|
|
|
|
|
|
|
|
var messageDetails = [];
|
|
|
|
Object.keys(params).forEach(function(key) {
|
2018-04-17 04:42:17 +03:00
|
|
|
try {
|
|
|
|
messageDetails.push(key + '=' + JSON.stringify(params[key]));
|
|
|
|
} catch (error) {
|
|
|
|
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
|
|
|
|
}
|
2018-04-12 22:18:11 +03:00
|
|
|
});
|
|
|
|
var reason = message;
|
|
|
|
if (messageDetails.length) {
|
|
|
|
message += ' (' + messageDetails.join(', ') + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
var error = new Error(message);
|
|
|
|
error.reason = reason;
|
|
|
|
error.code = code
|
|
|
|
|
|
|
|
Object.keys(params).forEach(function(key) {
|
|
|
|
error[key] = params[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
defineProperty(codes, 'checkNew', function(self, kind) {
|
|
|
|
if (!(self instanceof kind)) {
|
|
|
|
codes.throwError('missing new', codes.MISSING_NEW, { name: kind.name });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = codes;
|