Skip to content

Use HttpResponseException in annotation-processor #45053

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

samvaity
Copy link
Member

Description

Closes #43728

If an SDK is being regenerated based on a new swagger spec, a link to the pull request containing these swagger spec changes has been included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@azure-sdk
Copy link
Collaborator

azure-sdk commented Apr 22, 2025

API Change Check

APIView identified API level changes in this PR and created the following API reviews

io.clientcore:core

@samvaity samvaity force-pushed the exception-update branch 2 times, most recently from f401cea to ea9e072 Compare April 29, 2025 20:10
@samvaity samvaity requested a review from alzimmermsft April 29, 2025 22:13
@samvaity samvaity force-pushed the exception-update branch from f6c9d1a to 4802a3e Compare May 7, 2025 23:17
@samvaity samvaity marked this pull request as ready for review May 9, 2025 20:37
@samvaity samvaity force-pushed the exception-update branch from ec6bd11 to c81501f Compare May 9, 2025 22:35
@samvaity samvaity force-pushed the exception-update branch from c81501f to 621b47a Compare May 9, 2025 23:35
@azure-sdk
Copy link
Collaborator

API change check

APIView has identified API level changes in this PR and created following API reviews.

io.clientcore:core

if (value == null || value.toBytes().length == 0) {
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
} else {
ParameterizedType returnType = null;
Copy link
Member

Choose a reason for hiding this comment

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

I'm guessing this is a placeholder until we begin processing the exceptionBodyClass in UnexpectedResponseExceptionDetail?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, let me just include it in this PR. Will do a follow up commit.

} else {
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
}
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this logic is correct with what has changed between annotation processor and RestProxy. The decoded value should be the declared exception type, ex ErrorValue.

} else if (data == null || data.toBytes().length == 0) {
exceptionMessage.append("(empty body)");
} else {
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
Copy link
Member

Choose a reason for hiding this comment

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

By the time this method is call data would have been consumed to create decodedValue. Attempting to retrieve content again here might cause unexpected exceptions such as a stream closed exception if the response was using streaming.

Comment on lines +61 to +69
if (value == null || value.toBytes().length == 0) {
networkResponse.close();
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, null, null);
} else {
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class);
Object decoded = CoreUtils.decodeNetworkResponse(value, jsonSerializer, returnType);
networkResponse.close();
throw CoreUtils.instantiateUnexpectedException(responseCode, networkResponse, value, decoded);
}
Copy link
Member

Choose a reason for hiding this comment

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

Would we be better off creating a utility method in Core to handle this logic, rather than repeating in every generated API?

() -> createService(TestInterfaceClientImpl.TestInterfaceClientService.class).getBytes(getRequestUri()));

assertEquals("Status code 200, (1024-byte body)", e.getMessage());
assertTrue(e.getMessage().startsWith("Unexpected character"));
Copy link
Member

Choose a reason for hiding this comment

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

This test logic to me feels off. The message here seems to be a deserialization error when handling the exception type.

@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 201 })
@UnexpectedResponseExceptionDetail(statusCode = { 400 })
@UnexpectedResponseExceptionDetail(exceptionBodyClass = HttpBinJSON.class)
HttpBinJSON putWithUnexpectedResponseAndFallthroughExceptionType(@HostParam("uri") String uri,
Copy link
Member

Choose a reason for hiding this comment

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

Were these APIs removed or moved? They seem to test valid cases where different response codes correlate to different exception types.

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't support those different ExceptionTypes anymore right, but rather just a general HttpResponseException or CoreException?

Copy link
Member

Choose a reason for hiding this comment

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

I explained it poorly as type is too vague.

Correct, we don't support different exception types, such as NotModifiedException extends HttpResponseException.

I was more talking on how we still allow different exception body types to be configured, which isn't handled.

expectedResponseCheck = "responseCode == " + method.getExpectedStatusCodes().get(0) + ";";
if (method.getExpectedStatusCodes() == null || method.getExpectedStatusCodes().isEmpty()) {
// If expectedStatusCodes is empty, treat <400 as expected
expectedResponseCheck = "responseCode < 400";
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, historically only 2xx was treated as expected.

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.

Improve Error Handling : annotation-processor
3 participants