-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON parsing fails to handle errors. #4576
Comments
Hi @manchicken, The error you are getting is a generic JSON parsing error, it's not something specific to the SDK, and it will not have information about the root cause of the error.
In order to root cause the actual response that was received you could use a middleware step to capture and log the raw response that caused the serialization error. That could be a transient error, or it could be a lambda timeout, or a wide variety of different errors. If this is a transient error, there is not much the SDK team can do to help. Since the AWS SDK is a thin layer on top of a standard http client, the SDK does not have any debug capabilities when it comes to network errors, you will need to implement a proper retry strategy, and investigate your application and networking stack to mitigate those errors. Additionally, you did not provide any of your actual code so I can only speculate on what the problem could be. Providing your actual code, or a code snippet that can reproduce the issue would be helpful. Thanks, |
What you said is exactly the problem: I can't tell what the actual error is because the SDK is failing to capture the actual error which appears to have been returned in some format other than JSON. This is absolutely a failure of the SDK to handle its error. I cannot consistently reproduce the API error, so I can't give you test code. All I can do is show you where the SDK is failing to handle errors properly, thus obscuring the actual error. It's entirely possible that the error is a rate limit or some other manner of error that would be my responsibility to manage, but since the SDK is obscuring the actual error I am unable to write code to address that root issue. |
@manchicken an error thrown from the deserialization step (you will see To see the underlying error, you must intentionally log I think that this is hard to discover for users, so I have submitted a PR to add a hint to the stack trace. In this change, the hint about the
|
That won't help for this, I don't think. Doesn't they line I linked precede the middleware? |
The error's stack trace includes the deserializer middleware, which means it goes through there. The In general the protocol functions are called by the serializer and deserializer middleware functions. |
Here is an example using the operation you were using: const { SSMClient, GetParameterHistoryCommand } = require("@aws-sdk/client-ssm");
const { HttpResponse } = require("@aws-sdk/protocol-http");
const client = new SSMClient({
requestHandler: new (class {
async handle() {
return {
response: new HttpResponse({
statusCode: 503,
headers: {},
body: Buffer.from("some unparseable response"),
}),
};
}
})(),
});
(async () => {
const list = await client.send(new GetParameterHistoryCommand({})).catch((_) => _);
console.log(list);
console.log(list.$response);
})(); output:
|
How am I to know which http error status to trap? This seems like an awful lot of complexity you're asking me to add to my code in avoidance of your SDK trapping API errors. The SDK should trap its own errors, not rely on the caller to inject code for error trapping. This JSON parsing error is clearly the responsibility of this SDK, which should properly handle exception cases introduced by the APIs that they call. This may be a short-term workaround for some, but it won't work for everybody, especially when you factor in the volume of requests made by this SDK. In an application which makes a bunch of calls, this middleware hack would do more to obscure the error than to reveal it. |
When you You can, for example, retry on all 500 errors with const result = await client.send(new GetParameterHistoryCommand({})).catch((err) => {
console.error(err.$response);
if (err.$response.statusCode >= 500) {
// retry
}
}); |
It seems wrong to close this even though the SDK is clearly failing to reasonably handle exceptions in JSON parsing. While work-arounds have been discussed, nobody has addressed the actual bug. The bug has not been fixed. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread. |
Hi @manchicken , Each AWS service adheres to a defined wire protocol (XML, JSON, CBOR, etc.), and the SDK expects responses in these formats. When a response deviates from the expected format, the SDK will throw a deserialization error as part of its standard operation. This behavior is by design, as the SDK is built to process responses strictly according to the protocol specification. To assist with diagnosing issues where the response format is unexpected, you need to examine the raw network response before it undergoes deserialization. This can be done with the functionality @kuhe introduced that also allows you to do your own error handling, or through logging middleware or the logger interface provided on the client. This approach is consistent across all modern Smithy-based SDKs. The purpose here is not to modify the SDK to suppress or handle these errors since they are not modeled and do not conform to the protocol set by the server, but rather to provide you tools to identify and address the underlying cause—whether it is an anomaly in server behavior or a networking issue. Since you now should be able to examine the underlying raw response, you could share it with us. If it is a malformed server response, I can upstream it and investigate why this was sent that way in the first place (this shouldn't have happened) and if its an actual server side issue we will attempt to fix it. Thanks, |
Checkboxes for prior research
Describe the bug
This line should be handling the error that
JSON.parse()
throws whenever there is a payload returned which isn't JSON: https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-ssm/src/protocols/Aws_json1_1.ts#L20833It would be great if this code had some sort of useful debug information that popped out as well. I'm getting this not-at-all helpful error:
It does eventually get to a single stack frame which covers my code, but all that does is tell me that I called your function.
JSON.parse()
throws a syntax error if you don't give it valid JSON, and this code never handles that error, leaving my code to choke on an AWS response that I can't even see.SDK version number
@aws-sdk/client-ssm version 3.299.0
Which JavaScript Runtime is this issue in?
Node.js
Details of the browser/Node.js/ReactNative version
Lambda running node 18
Reproduction Steps
I'm not at all certain how to replicate this. It seems like it most often chokes on transient API errors.
Observed Behavior
Unhelpful error message regarding JSON syntax that doesn't come from my program, and has little-to-no context on how to troubleshoot.
Expected Behavior
A useful error that helps me understand what went wrong. I'd hope that this message would at least contain:
Possible Solution
Handle the
JSON.parse()
exceptions, and then produce a useful error message with some useful context.Additional Information/Context
I know that it's possible to add middleware to the client which can dump the full contents of the request and response, but this is super noisy and makes it really difficult to troubleshoot transient errors as it really increases the size of the haystack we're looking in.
The text was updated successfully, but these errors were encountered: