UNPKG

node-jet

Version:

Jet Realtime Message Bus for the Web. Daemon and Peer implementation.

108 lines (107 loc) 3.76 kB
const errorUrlBase = 'https://github.com/lipp/node-jet/blob/master/doc/peer.markdown'; export const NO_ERROR_CODE = 1000; export const PARSE_ERROR_CODE = -32600; export const INVALID_REQUEST = -32600; export const METHOD_NOT_FOUND = -32601; export const INVALID_PARAMS_CODE = -32602; export const INTERNAL_ERROR_CODE = -32603; export const INVALID_PATH = -32604; export const RESPONSE_TIMEOUT_CODE = -32001; export const CONNECTION_ERROR_CODE = -32002; export class JSONRPCError extends Error { code; message; data; constructor(code, name, message, details = '') { super(); this.code = code; this.name = 'jet.' + name; this.message = message; this.data = { name: 'jet.' + name, url: errorUrlBase + '#jet' + name.toLowerCase(), details }; } toString = () => `code: ${this.code} \nname: ${this.name} \n${this.message} \n${this.data}`; } export class ParseError extends JSONRPCError { constructor(details = '') { super(PARSE_ERROR_CODE, 'ParseError', 'Message could not be parsed', details); } } export class InvalidParamError extends JSONRPCError { constructor(name, message, details = '') { super(INVALID_PARAMS_CODE, name, message, details); } } export class NotFound extends InvalidParamError { constructor(details) { super('NotFound', 'No State/Method matching the specified path', details); } } export class InvvalidCredentials extends InvalidParamError { constructor(details) { super('invalid params', 'invalid credentials', details); } } export class NotAuthorized extends InvalidParamError { constructor(details) { super('invalid params', 'Missing authorization', details); } } export class InvalidArgument extends InvalidParamError { constructor(details) { super('InvalidArgument', 'The provided argument(s) have been refused by the State/Method', details); } } export class Occupied extends InvalidParamError { constructor(details) { super('Occupied', 'A State/Method with the same path has already been added', details); } } export class FetchOnly extends InvalidParamError { constructor(details) { super('FetchOnly', 'The State cannot be modified', details); } } export class methodNotFoundError extends JSONRPCError { constructor(details = '') { super(METHOD_NOT_FOUND, 'MethodNotFound', 'Method not found', details); } } export class invalidMethod extends JSONRPCError { constructor(details) { super(INVALID_REQUEST, 'invalidMethod', 'The path does not support this method', details); } } export class invalidState extends JSONRPCError { constructor(details) { super(INVALID_PATH, 'invalidState', 'The path is not supported', details); } } export class invalidRequest extends JSONRPCError { constructor(name = 'invalidRequest', message = 'Invalid Request', details = '') { super(INVALID_REQUEST, name, message, details); } } export class notAllowed extends invalidRequest { constructor(details) { super('NotAllowed', 'Not allowed', details); } } export class ConnectionClosed extends JSONRPCError { constructor(details) { super(CONNECTION_ERROR_CODE, 'ConnectionClosed', 'The connection is closed', details); } } export class ConnectionInUse extends JSONRPCError { constructor(err) { super(CONNECTION_ERROR_CODE, 'ConnectionInUse', 'Could not establish connection', err); } } export class PeerTimeout extends JSONRPCError { constructor(details) { super(RESPONSE_TIMEOUT_CODE, 'PeerTimeout', 'The peer processing the request did not respond within the specified timeout', details); } }