@kddd/artinet-sdk
Version:
TypeScript SDK for the Agent2Agent (A2A) Protocol
43 lines • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseResponse = parseResponse;
const errors_js_1 = require("../../utils/common/errors.js");
const log_js_1 = require("../../utils/logging/log.js");
/**
* Parses a JSON-RPC response string and validates its structure.
* If the response contains an error, it is thrown as an A2AError.
* If the response contains neither a result nor an error, a validation error is thrown.
*
* @param data Response data as string
* @returns The parsed and validated response object
* @throws A2AError if the response contains an error or is invalid
*/
function parseResponse(data) {
if (!data) {
throw (0, errors_js_1.PARSE_ERROR)("Invalid response data");
}
try {
const parsed = JSON.parse(data);
if (parsed.error) {
throw new errors_js_1.SystemError(parsed.error.message, parsed.error.code, parsed.error.data);
}
if (typeof parsed !== "object" ||
parsed === null ||
parsed.jsonrpc !== "2.0") {
throw (0, errors_js_1.PARSE_ERROR)("invalid jsonrpc");
}
if (parsed.result === undefined) {
throw (0, errors_js_1.PARSE_ERROR)("result is undefined");
}
return parsed;
}
catch (error) {
if (error instanceof errors_js_1.SystemError) {
(0, log_js_1.logError)("parseResponse", "SystemError:", error.message);
throw error;
}
(0, log_js_1.logError)("parseResponse", "Error parsing response:", data);
throw (0, errors_js_1.PARSE_ERROR)(error);
}
}
//# sourceMappingURL=parser.js.map