Skip to content
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

Is it possible to change the type depending on the status code? #273

Open
manabu0926 opened this issue Oct 26, 2024 · 1 comment
Open

Is it possible to change the type depending on the status code? #273

manabu0926 opened this issue Oct 26, 2024 · 1 comment

Comments

@manabu0926
Copy link

manabu0926 commented Oct 26, 2024

Use case

when mutate request return error, I don't know how to handle errors.

I'm assuming that I want to process success in then, and process failure in onError.
At that time, I would like to get the error message defined in the response.

Proposal

post:
  tags:
    - DriverApi::Talks
  summary: post Message
  operationId: postTalk
  security:
    - bearerAuth: []
  requestBody:
    required: true
    content:
      application/json:
        schema:
          $ref: "#/components/schemas/talk_create_params"
  responses:
    "201":
      description: "Created"
      content:
        application/json:
          schema:
            $ref: "../../../components/schemas/responses.yml#/created"
    "422":
      description: "Unprocessable Entity Error"
      content:
        application/json:
          schema:
            $ref: "../../../components/schemas/responses.yml#/unprocessable_entity_error" ← define errorMessage(string).

want to write, example,

final api = DriverApiTalkClient(dio);
final body = TalkCreateParams(message: 'test');
await api
    .postTalk(body: body)
    .then((response) => showSnackbar(response.message))
    .onError((_, __, response) {
      if(response.statusCode == 400) {
        showSnackbar(response.errorMessage);
      } else if(response.statusCode == 422) {
        otherHandleError();
      }
});

Sorry if this has already been done. I would be happy if you could teach me how to write it!
I think this will solve the redundant writing style that I was dissatisfied with with the previous openapi-generator, so I would be happy if you would consider it!

@manabu0926
Copy link
Author

manabu0926 commented Oct 27, 2024

e.g.
want to generate,
〇〇_client.g.dart

final _result = await _dio.fetch<Map<String, dynamic>>(_options);
if (_result.statusCode != 200) {  ///← not success statusCode
  throw DioException(
    requestOptions: _options,
    response: _result,
  );
}

call function,

await api.postTalk.then(
  (response) => state.value = response.data.todayShift,
  onError: (Object error) => {
    if (error is DioException)
      {
        print(error.response?.statusCode),
        print(error.response?.data['message']),
      },
  },
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant