ArunPrakashG / wordpress_client

A powerful and easy-to-use WordPress REST API client for Dart & Flutter.
MIT License
33 stars 12 forks source link

WordpressSuccessResponse when "rest_post_invalid_page_number" #76

Open matejmosko opened 1 month ago

matejmosko commented 1 month ago

When I call for json, I sometimes stumble on a problem when I get out of the pages range. I set page variable dynamically for paging capability in my app. However when I increment the page number for your requests to more than the maximum count, I end up with endless loading.

When the response is rest_post_invalid_page_number, wordpress_client accepts this as success and tries to map the values into a list of posts. This results in this exception:

Unhandled Exception: type '_Map<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' in type cast
E/flutter (20045): #0      IRequestExecutor.list.<anonymous closure> (package:wordpress_client/src/request_executor_base.dart:325:37)
E/flutter (20045): #1      WordpressRawResponse.map (package:wordpress_client/src/responses/wordpress_raw_response.dart:207:21)
E/flutter (20045): #2      IRequestExecutor.list (package:wordpress_client/src/request_executor_base.dart:322:24)

and the future client.posts.list(request) gets no response therefore it endlessly waits for the answer (and my loading is spinning endlessly as well)

Is there a way to get the Failure answer for the future even if the rawResponse is successfuly obtained, but it's invalid?

ArunPrakashG commented 1 month ago

Hi @matejmosko Could you please share a snippet to reproduce this? I just tried recreating this and i was getting expected outcome in all of the cases.

{"code":"rest_post_invalid_page_number","message":"The page number requested is larger than the number of pages available.","data":{"status":400}}

Posts Count: {code: rest_post_invalid_page_number, message: The page number requested is larger than the number of pages available., data: {status: 400}}

Exited.

I am getting the error response and the script exits as expected.

Additionally, the status code is 400, this should not be mapped to a success response in any circumstances because it is fundamentally wrong. I can look into this further with few more inputs on how to reproduce this.

Other options include, updating the library to the latest version. Also, you can get the total page count on each request headers. There is a getter for the same:

final response = await client.posts.list(
    ListPostRequest(
      page: 999999,
      order: Order.asc,
    ),
  );

  response.map<void>(
    onSuccess: (success) {
      final pagesCount = success.totalPagesCount;
    },
    onFailure: (error) {
      print(error.error);
    },
  );
ArunPrakashG commented 1 month ago

@matejmosko Hey, any update here?

matejmosko commented 1 month ago

I am a bit busy now. I will have a look at it in a week. I am sorry

Dňa po 26. 8. 2024, 16:50 Arun Prakash @.***> napísal(a):

@matejmosko https://github.com/matejmosko Hey, any update here?

— Reply to this email directly, view it on GitHub https://github.com/ArunPrakashG/wordpress_client/issues/76#issuecomment-2310404899, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOA64YD7SMCWEXFHKJVNCDZTM6CHAVCNFSM6AAAAABNAP7SOOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJQGQYDIOBZHE . You are receiving this because you were mentioned.Message ID: @.***>

ArunPrakashG commented 1 month ago

Hey @matejmosko, it's not totally fine. I was just checked in. have a great day.