UNPKG

@arifwidianto/rpc-agent

Version:

RPC Agent for both client and server, extends more methods easily

108 lines 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorService = exports.RpcError = void 0; const interfaces_1 = require("../interfaces"); class RpcError extends Error { code; details; timestamp; requestId; source; errorId; constructor({ message, code, details, requestId, source = "rpc-service", }) { super(message); this.name = "RpcError"; this.code = code; this.details = details; this.requestId = requestId; this.source = source; this.timestamp = new Date().toISOString(); this.errorId = `${source}-${this.timestamp}-${Math.random().toString(36).substr(2, 9)}`; Object.setPrototypeOf(this, RpcError.prototype); } toJSON() { return { code: this.code, message: this.message, details: this.details, timestamp: this.timestamp, errorId: this.errorId, source: this.source, ...(this.requestId !== undefined && { requestId: this.requestId }), }; } static fromJSON(json) { return new RpcError({ message: json.message, code: json.code, details: json.details, requestId: json.requestId, source: json.source, }); } } exports.RpcError = RpcError; class ErrorService { static createError(code, message, field, details, requestId, source) { const errorDetails = { ...(details || {}), ...(field && { field }), }; return new RpcError({ message, code, details: errorDetails, requestId, source, }); } static parseError(message = "Parse error", details) { return this.createError(interfaces_1.ErrorCode.PARSE_ERROR, message, undefined, details); } static invalidRequest(message = "Invalid request", details) { return this.createError(interfaces_1.ErrorCode.INVALID_REQUEST, message, undefined, details); } static methodNotFound(message = "Method not found", details) { return this.createError(interfaces_1.ErrorCode.METHOD_NOT_FOUND, message, undefined, details); } static invalidParams(message, field, details) { return this.createError(interfaces_1.ErrorCode.INVALID_PARAMS, message, field, details); } static internalError(message = "Internal error", details) { return this.createError(interfaces_1.ErrorCode.INTERNAL_ERROR, message, undefined, details); } static requestTimeout(message = "Request timeout", details) { return this.createError(interfaces_1.ErrorCode.TIMEOUT, message, undefined, details); } static extensionNotFound(message = "Extension not found", details) { return this.createError(interfaces_1.ErrorCode.EXTENSION_NOT_FOUND, message, undefined, details); } static serviceUnavailable(message = "Service unavailable", details) { return this.createError(interfaces_1.ErrorCode.SERVICE_UNAVAILABLE, message, undefined, details); } static unauthorized(message = "Unauthorized", details) { return this.createError(interfaces_1.ErrorCode.UNAUTHORIZED, message, undefined, details); } static forbidden(message = "Forbidden", details) { return this.createError(interfaces_1.ErrorCode.FORBIDDEN, message, undefined, details); } static rateLimited(message = "Rate limited", details) { return this.createError(interfaces_1.ErrorCode.RATE_LIMITED, message, undefined, details); } static isRpcError(error) { return error instanceof RpcError; } static wrapError(error) { if (this.isRpcError(error)) { return error; } const message = error instanceof Error ? error.message : String(error); const details = error instanceof Error ? { stack: error.stack } : undefined; return this.internalError(message, { ...details, originalError: String(error), }); } } exports.ErrorService = ErrorService; //# sourceMappingURL=error.service.js.map