butlerd
Version:
Node.js library for butlerd, the butler daemon
146 lines (145 loc) • 4.71 kB
JavaScript
;
// this file can be imported without pulling in node.js's "net"
// module etc., so it can be used in a browser context for example.
Object.defineProperty(exports, "__esModule", { value: true });
var StandardErrorCode;
(function (StandardErrorCode) {
StandardErrorCode[StandardErrorCode["ParseError"] = -32700] = "ParseError";
StandardErrorCode[StandardErrorCode["InvalidRequest"] = -32600] = "InvalidRequest";
StandardErrorCode[StandardErrorCode["MethodNotFound"] = -32601] = "MethodNotFound";
StandardErrorCode[StandardErrorCode["InvalidParams"] = -32602] = "InvalidParams";
StandardErrorCode[StandardErrorCode["InternalError"] = -32603] = "InternalError";
})(StandardErrorCode = exports.StandardErrorCode || (exports.StandardErrorCode = {}));
var RequestType;
(function (RequestType) {
RequestType[RequestType["Request"] = 0] = "Request";
RequestType[RequestType["Notification"] = 1] = "Notification";
})(RequestType = exports.RequestType || (exports.RequestType = {}));
exports.createRequest = (method) => {
return Object.assign((params) => (gen) => ({
jsonrpc: "2.0",
method,
id: gen.generateID(),
params,
}), { __method: method });
};
exports.createNotification = (method) => {
return Object.assign((params) => ({
jsonrpc: "2.0",
method,
params,
}), { __method: method });
};
exports.createResult = () => (id, result, error) => {
if (error) {
return {
jsonrpc: "2.0",
error,
id,
};
}
else {
return {
jsonrpc: "2.0",
result,
id,
};
}
};
function formatRpcError(rpcError) {
if (rpcError.code === StandardErrorCode.InternalError) {
// don't prefix internal errors, for readability.
// if a `RequestError` is caught, it can still be
// detected by checking `.rpcError`
return rpcError.message;
}
return `JSON-RPC error ${rpcError.code}: ${rpcError.message}`;
}
/**
* A JavaScript Error that encapsulates a JSON-RPC 2.0 Error.
* @see asRequestError
* @see getErrorStack
* @see getRpcErrorData
*/
class RequestError extends Error {
constructor(rpcError) {
super(formatRpcError(rpcError));
this.rpcError = rpcError;
}
static fromInternalCode(code) {
return new RequestError({
message: internalCodeToString(code),
code,
data: {
stack: new Error().stack,
},
});
}
}
exports.RequestError = RequestError;
var InternalCode;
(function (InternalCode) {
InternalCode[InternalCode["ConversationCancelled"] = -1000] = "ConversationCancelled";
InternalCode[InternalCode["ConnectionTimedOut"] = -1100] = "ConnectionTimedOut";
InternalCode[InternalCode["SocketClosed"] = -1200] = "SocketClosed";
})(InternalCode = exports.InternalCode || (exports.InternalCode = {}));
/**
* Return a string representation of an internal error code.
*/
function internalCodeToString(code) {
switch (code) {
case InternalCode.ConversationCancelled:
return "JSON-RPC conversation cancelled";
case InternalCode.ConnectionTimedOut:
return "JSON-RPC connection timed out";
case InternalCode.SocketClosed:
return "JSON-RPC socket closed by remote peer";
}
}
exports.internalCodeToString = internalCodeToString;
/**
* Get a RequestError's stack Golang trace or JavaScript
* stack trace, if any, or message if not.
*/
function getErrorStack(e) {
if (!e) {
return "Unknown error";
}
let errorStack = e.stack || e.message;
const re = asRequestError(e);
if (re) {
const ed = getRpcErrorData(e);
if (ed && ed.stack) {
// use golang stack if available
errorStack = ed.stack;
}
else if (re.message) {
// or just message
errorStack = re.message;
}
}
return errorStack;
}
exports.getErrorStack = getErrorStack;
/**
* Cast an Error to `RequestError`, if it looks like one,
* otherwise return null
*/
function asRequestError(e) {
const re = e;
if (re.rpcError) {
return e;
}
}
exports.asRequestError = asRequestError;
/**
* If this error is a JSON-RPC 2.0 error, return its additional error data,
* if any
*/
function getRpcErrorData(e) {
const re = asRequestError(e);
if (re && re.rpcError) {
return re.rpcError.data;
}
}
exports.getRpcErrorData = getRpcErrorData;