ocpp-rpc
Version:
A client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP protocols (e.g. OCPP1.6-J and OCPP2.0.1).
53 lines (46 loc) • 2.01 kB
JavaScript
const errors = require('./errors');
const packageJson = require('../package.json');
const rpcErrorLUT = {
'GenericError' : errors.RPCGenericError,
'NotImplemented' : errors.RPCNotImplementedError,
'NotSupported' : errors.RPCNotSupportedError,
'InternalError' : errors.RPCInternalError,
'ProtocolError' : errors.RPCProtocolError,
'SecurityError' : errors.RPCSecurityError,
'FormationViolation' : errors.RPCFormationViolationError,
'FormatViolation' : errors.RPCFormatViolationError,
'PropertyConstraintViolation' : errors.RPCPropertyConstraintViolationError,
'OccurenceConstraintViolation' : errors.RPCOccurenceConstraintViolationError,
'OccurrenceConstraintViolation' : errors.RPCOccurrenceConstraintViolationError,
'TypeConstraintViolation' : errors.RPCTypeConstraintViolationError,
'MessageTypeNotSupported' : errors.RPCMessageTypeNotSupportedError,
'RpcFrameworkError' : errors.RPCFrameworkError,
};
function getPackageIdent() {
return `${packageJson.name}/${packageJson.version} (${process.platform})`;
}
function getErrorPlainObject(err) {
try {
// (nasty hack)
// attempt to serialise into JSON to ensure the error is, in fact, serialisable
return JSON.parse(JSON.stringify(err, Object.getOwnPropertyNames(err)));
} catch (e) {
// cannot serialise into JSON.
// return just stack and message instead
return {
stack: err.stack,
message: err.message,
};
}
}
function createRPCError(type, message, details) {
const E = rpcErrorLUT[type] ?? errors.RPCGenericError;
const err = new E(message ?? '');
err.details = details ?? {};
return err;
}
module.exports = {
getErrorPlainObject,
createRPCError,
getPackageIdent,
};