UNPKG

@kddd/artinet-sdk

Version:

TypeScript SDK for the Agent2Agent (A2A) Protocol

82 lines 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FAILED_UPDATE = exports.PUSH_NOTIFICATION_NOT_SUPPORTED = exports.UNSUPPORTED_OPERATION = exports.TASK_NOT_CANCELABLE = exports.TASK_NOT_FOUND = exports.INTERNAL_ERROR = exports.INVALID_PARAMS = exports.INVALID_REQUEST = exports.METHOD_NOT_FOUND = exports.PARSE_ERROR = exports.SystemError = void 0; exports.errorHandler = errorHandler; const extended_schema_js_1 = require("../../types/extended-schema.js"); const log_js_1 = require("../../utils/logging/log.js"); class SystemError extends Error { code; data; constructor(message, code, data) { super(message); this.name = "RpcError"; this.message = message; this.code = code; this.data = data; } } exports.SystemError = SystemError; // Factory methods for common errors const PARSE_ERROR = (data) => new SystemError("Invalid JSON payload", extended_schema_js_1.ErrorCodeParseError, data); exports.PARSE_ERROR = PARSE_ERROR; const METHOD_NOT_FOUND = (data) => new SystemError("Method not found", extended_schema_js_1.ErrorCodeMethodNotFound, data); exports.METHOD_NOT_FOUND = METHOD_NOT_FOUND; const INVALID_REQUEST = (data) => new SystemError("Request payload validation error", extended_schema_js_1.ErrorCodeInvalidRequest, data); exports.INVALID_REQUEST = INVALID_REQUEST; const INVALID_PARAMS = (data) => new SystemError("Invalid parameters", extended_schema_js_1.ErrorCodeInvalidParams, data); exports.INVALID_PARAMS = INVALID_PARAMS; const INTERNAL_ERROR = (data) => new SystemError("Internal error", extended_schema_js_1.ErrorCodeInternalError, data); exports.INTERNAL_ERROR = INTERNAL_ERROR; const TASK_NOT_FOUND = (data) => new SystemError("Task not found", extended_schema_js_1.ErrorCodeTaskNotFound, data); exports.TASK_NOT_FOUND = TASK_NOT_FOUND; const TASK_NOT_CANCELABLE = (data) => new SystemError("Task cannot be canceled", extended_schema_js_1.ErrorCodeTaskNotCancelable, data); exports.TASK_NOT_CANCELABLE = TASK_NOT_CANCELABLE; const UNSUPPORTED_OPERATION = (data) => new SystemError("This operation is not supported", extended_schema_js_1.ErrorCodeUnsupportedOperation, data); exports.UNSUPPORTED_OPERATION = UNSUPPORTED_OPERATION; const PUSH_NOTIFICATION_NOT_SUPPORTED = (data) => new SystemError("Push Notification is not supported", extended_schema_js_1.ErrorCodePushNotificationNotSupported, data); exports.PUSH_NOTIFICATION_NOT_SUPPORTED = PUSH_NOTIFICATION_NOT_SUPPORTED; const FAILED_UPDATE = (message) => ({ state: "failed", message: { role: "agent", parts: [{ type: "text", text: message }], }, }); exports.FAILED_UPDATE = FAILED_UPDATE; /** * Express error handler middleware. */ function errorHandler(err, req, res, next) { if (res.headersSent) { return next(err); } (0, log_js_1.logError)("errorHandler", JSON.stringify(err), err); let reqId = null; try { if (req.body && typeof req.body === "object" && "id" in req.body) { reqId = req.body.id; } } catch (e) { (0, log_js_1.logError)("A2AServer", "Error extracting request ID", e); } let jsonRpcError; if (err instanceof SystemError) { jsonRpcError = { code: err.code, message: err.message, data: err.data }; } else { const internalError = (0, exports.INTERNAL_ERROR)(err.stack); jsonRpcError = { code: internalError.code, message: internalError.message, data: internalError.data, }; } const errorResponse = { jsonrpc: "2.0", id: reqId, error: jsonRpcError, }; res.status(200).json(errorResponse); } //# sourceMappingURL=errors.js.map