Azure / autorest.java

Extension for AutoRest (https://github.com/Azure/autorest) that generates Java code
MIT License
33 stars 82 forks source link

Unbranded CodeGen should generate error models instead of reusing the model from core library #2991

Open lirenhe opened 4 days ago

lirenhe commented 4 days ago

epic https://github.com/microsoft/typespec/issues/5203

Need to generate the error model from the following definition:

@error
model ApiError {
  /** A machine readable error code */
  code: string;

  /** A human readable message */
  // https://github.com/microsoft/OpenAPI/blob/main/extensions/x-ms-primary-error-message.md
  @OpenAPI.extension("x-ms-primary-error-message", true)
  message: string;
}

Please also verify we could map the error by status code correctly.

model Standard4XXResponse extends ApiError {
  @minValue(400)
  @maxValue(499)
  @statusCode
  statusCode: int32;
}
weidongxu-microsoft commented 4 days ago

Java impl code would look like this (there is no range status code in clientcore, hence the full list -- except 404 which is among expected)

        @HttpRequestInformation(method = HttpMethod.DELETE, path = "/items/{id}", expectedStatusCodes = { 204, 404 })
        @UnexpectedResponseExceptionDetail(
            statusCode = {
                500,
...
                599 },
            exceptionBodyClass = Standard5XXResponse.class)
        @UnexpectedResponseExceptionDetail(
            statusCode = {
                400,
                401,
                402,
                403,
                405,
                406,
...
                499 },
            exceptionBodyClass = Standard4XXResponse.class)
        @UnexpectedResponseExceptionDetail
        Response<Void> deleteSync(@HostParam("endpoint") String endpoint, @PathParam("id") long id,
            @HeaderParam("Accept") String accept, RequestOptions requestOptions);

PS: if there is an @error ApiError model without status code in the op, the last line would be e.g.

        @UnexpectedResponseExceptionDetail(exceptionBodyClass = ApiError.class)
weidongxu-microsoft commented 2 days ago

Use be

        try {
            client.invalid();
        } catch (HttpResponseException e) {
            InvalidAuth error = (InvalidAuth) e.getValue();
        }