@dexwox-labs/a2a-client
Version:
TypeScript client implementation for Google's Agent-to-Agent (A2A) protocol - includes HTTP/WebSocket communication, circuit breakers and error handling
67 lines • 3.11 kB
JavaScript
;
/**
* @module ErrorHandler
* @description Utilities for handling and normalizing errors in the A2A SDK
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.A2ATimeoutError = exports.A2AValidationError = exports.A2ANetworkError = exports.A2AServerError = exports.A2AClientError = exports.A2AError = void 0;
exports.normalizeError = normalizeError;
const error_1 = require("./error");
Object.defineProperty(exports, "A2AError", { enumerable: true, get: function () { return error_1.A2AError; } });
Object.defineProperty(exports, "A2AClientError", { enumerable: true, get: function () { return error_1.A2AClientError; } });
Object.defineProperty(exports, "A2AServerError", { enumerable: true, get: function () { return error_1.A2AServerError; } });
Object.defineProperty(exports, "A2ANetworkError", { enumerable: true, get: function () { return error_1.A2ANetworkError; } });
Object.defineProperty(exports, "A2AValidationError", { enumerable: true, get: function () { return error_1.A2AValidationError; } });
Object.defineProperty(exports, "A2ATimeoutError", { enumerable: true, get: function () { return error_1.A2ATimeoutError; } });
/**
* Normalizes any error into an A2AError
*
* This utility function converts any error or exception into a standardized
* A2AError object. It handles various error types including:
* - Existing A2AError instances (returned as-is)
* - Standard JavaScript Error objects
* - Server response objects with status codes
* - Any other unknown error types
*
* @param err - The error to normalize, can be of any type
* @returns A standardized A2AError instance
*
* @example
* ```typescript
* try {
* // Some operation that might fail
* await fetch('https://a2a-server.example.com');
* } catch (error) {
* // Normalize the error to a standard format
* const normalizedError = normalizeError(error);
*
* // Now we can handle it consistently
* console.error(`Error (${normalizedError.code}): ${normalizedError.message}`);
*
* // And we can check for specific error types
* if (normalizedError instanceof A2ANetworkError) {
* // Handle network errors specifically
* }
* }
* ```
*/
function normalizeError(err) {
// If it's already an A2AError, return it as-is
if (err instanceof error_1.A2AError) {
return err;
}
// Handle standard JavaScript Error objects
if (err instanceof Error) {
return new error_1.A2AClientError('UNKNOWN_ERROR', err.message, { stack: err.stack });
}
// Handle server response objects with status codes
if (typeof err === 'object' && err !== null) {
const errorObj = err;
if (errorObj.status && typeof errorObj.status === 'number') {
return new error_1.A2AServerError(errorObj.status, String(errorObj.code || 'SERVER_ERROR'), String(errorObj.message || 'Server error occurred'), errorObj);
}
}
// Handle any other unknown error types
return new error_1.A2AClientError('UNKNOWN_ERROR', 'Unknown error occurred', { originalError: err });
}
//# sourceMappingURL=error-handler.js.map