The JSON-RPC 2 specification defined also the following specialized
errors:
// Parse error: invalid JSON was received by the peer.throw new InvalidJson()// Invalid request: the JSON sent is not a valid JSON-RPC 2 message.throw new InvalidRequest()// Method not found: the method does not exist or is not available.throw new MethodNotFound(methodName)// Invalid parameters.throw new InvalidParameters(data)
Custom errors can of course be created, they just have to inherit
JsonRpcError:
var MethodNotFound = require('json-rpc-protocol').MethodNotFoundformat.error(0, new MethodNotFound('add'))// → {// "jsonrpc": "2.0",// "id": 0,// "error": {// "code": -3601,// "message": "method not found: add",// "data": "add"// }// }
Note: the error to format must implement a toJsonRpcError function which returns an object or it
will be automatically replaced by an unknown error for security
reasons.
toJsonRpcError example:
toJsonRpcError () { return { code: 42, // must be an integer message: 'Hacking too much time!', // must be a string data: [ 'Hackerman' ] // optional }}
Development
# Install dependencies> npm install# Run the tests> npm test# Continuously compile> npm run dev# Continuously run the tests> npm run dev-test# Build for production (automatically called by npm install)> npm run build