Skip to content

@W-21053358 Handle 429 response from svc-api gracefully by adding NPE checks and relevant messages#40

Merged
bharatsuri97 merged 7 commits intoforcedotcom:masterfrom
SaranshSinha:npe-429-fix
Feb 23, 2026
Merged

@W-21053358 Handle 429 response from svc-api gracefully by adding NPE checks and relevant messages#40
bharatsuri97 merged 7 commits intoforcedotcom:masterfrom
SaranshSinha:npe-429-fix

Conversation

@SaranshSinha
Copy link
Copy Markdown
Contributor

…sage to the response

Added a map to associate HTTP status codes with error messages and updated error handling to utilize this map.
@salesforce-cla
Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @SaranshSinha to sign the Salesforce Inc. Contributor License Agreement.

Add relevant message handling for associated error codes.
Removed the status code to error message mapping and simplified error handling in the buildErrorFromClientResponseBodyString method.
if (contentType.contains("application/json")) {
HttpHeaders headers = inputMessage.getHeaders();
MediaType contentType = headers != null ? headers.getContentType() : null;
if (contentType != null && contentType.toString().contains("application/json")) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is case sensitive, check if you need case insensitive check (recommended).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check if you need to support formats like application/problem+json and application/vnd.api+json

Comment on lines 64 to 74
BodyExtractor<Mono<Error>, ReactiveHttpInputMessage> extractor = (inputMessage, context) -> {
String contentType = inputMessage.getHeaders().getContentType().toString();
if (contentType.contains("application/json")) {
HttpHeaders headers = inputMessage.getHeaders();
MediaType contentType = headers != null ? headers.getContentType() : null;
if (contentType != null && contentType.toString().contains("application/json")) {
return BodyExtractors.toMono(Error.class)
.extract(inputMessage, context);
} else {
return buildErrorFromClientResponseBodyString(inputMessage, context);
}
};
return extractor;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler suggestion:

Suggested change
return (inputMessage, context) -> {
final MediaType contentType = inputMessage.getHeaders().getContentType();
if (MediaType.APPLICATION_JSON.includes(contentType)) {
return BodyExtractors
.toMono(Error.class)
.extract(inputMessage, context);
}
return buildErrorFromClientResponseBodyString(inputMessage, context);
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the spec /einstein-bot-sdk-java/src/main/resources/v5_3_0_api_specs.yml, only application/json is returned for all requests. So I think the current case sensitive check should be more than sufficient.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the suggested code does the same

}

} No newline at end of file
@Test
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: formatting


ChatbotResponseException chatbotResponseException = validateAndGetCause(exceptionThrown,
ChatbotResponseException.class);
assertEquals(HttpStatus.TOO_MANY_REQUESTS.value(), chatbotResponseException.getStatus());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... Why is it a 429?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are stubbing this response in line 416 along with a null contentType. This basically mocks the current flow where the sdk breaks due to NPE. Now the test verifies that the NPE issue is fixed for such responses

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I'm not convinced on the 429 part. If the issue is with content type, it should have been a 415.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is with a "null" content-type. GRLS metrics confirmed that it was throwing 429 and we faced this NPE during that time interval

@bharatsuri97 bharatsuri97 merged commit d5166d2 into forcedotcom:master Feb 23, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants