feat: expose HTTP status code and headers on SDK error classes#319
feat: expose HTTP status code and headers on SDK error classes#319corvid-agent wants to merge 3 commits intoa2aproject:mainfrom
Conversation
Summary of ChangesHello @corvid-agent, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the error handling capabilities of the A2A SDK by exposing underlying HTTP details directly within SDK error classes. This change empowers developers to build more robust and responsive applications by enabling them to react to specific HTTP status codes and headers, such as implementing retry logic based on Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
🧪 Code Coverage
Generated by coverage-comment.yml |
There was a problem hiding this comment.
Code Review
This pull request is a great enhancement, exposing HTTP status codes and headers on SDK errors. This will be very useful for consumers to build more robust error handling, for example for rate limiting. The implementation is solid and comes with a comprehensive set of tests.
I've left a couple of suggestions for improvement:
- In
src/client/transports/rest_transport.ts, I've suggested a more concise way to convert response headers to an object usingObject.fromEntries. - Regarding
src/errors.ts, while the suggestion to introduce a base error class for better maintainability is excellent, we'll defer this refactoring to a future major release to avoid potential breaking changes to the public SDK error interface, as per repository guidelines. ATODOwill be added to track this.
Overall, this is a well-executed feature. Addressing the immediate feedback will further improve the code quality.
| const headers: Record<string, string> = {}; | ||
| response.headers.forEach((value, key) => { | ||
| headers[key] = value; | ||
| }); | ||
| return { statusCode: response.status, headers }; |
There was a problem hiding this comment.
The logic for converting the Headers object to a plain JavaScript object can be simplified. The Headers object is iterable, so you can use Object.fromEntries() for a more concise implementation.
| const headers: Record<string, string> = {}; | |
| response.headers.forEach((value, key) => { | |
| headers[key] = value; | |
| }); | |
| return { statusCode: response.status, headers }; | |
| return { | |
| statusCode: response.status, | |
| headers: Object.fromEntries(response.headers), | |
| }; |
Add optional `statusCode` and `headers` properties to all A2A SDK error classes (TaskNotFoundError, TaskNotCancelableError, etc.) so that consumers can implement HTTP-aware error handling (e.g. distinguishing 401 from 404 from 429). The REST transport now populates these fields from the HTTP response when mapping errors. Closes a2aproject#317 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Address review feedback: simplify headers extraction by using Object.fromEntries() instead of manual forEach loop. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
8e82038 to
0be73a4
Compare
Summary
statusCodeandheadersproperties to all A2A SDK error classes (TaskNotFoundError,TaskNotCancelableError,PushNotificationNotSupportedError,UnsupportedOperationError,ContentTypeNotSupportedError,InvalidAgentResponseError,AuthenticatedExtendedCardNotConfiguredError) so that consumers can implement HTTP-aware error handling.RestTransport) to extract HTTP status code and response headers from theResponseobject and pass them through when constructing error instances viamapToError.A2AErrorHttpDetailsinterface from the client entry point for use by consumers.statusCodeandheadersare correctly populated on errors thrown by the REST transport.Closes #317
Test plan
statusCodeis set onTaskNotFoundError(404),TaskNotCancelableError(409),PushNotificationNotSupportedError(400),UnsupportedOperationError(405),ContentTypeNotSupportedError(415),InvalidAgentResponseError(502), andAuthenticatedExtendedCardNotConfiguredError(401)Retry-After,X-RateLimit-Remaining) are available on errors for rate-limiting / backoff strategies🤖 Generated with Claude Code