api-platform / admin

A beautiful and fully-featured administration interface builder for hypermedia APIs
https://api-platform.com/docs/admin/
MIT License
481 stars 130 forks source link

Delete with 500 response does not call onError callback #579

Closed papiliond closed 1 month ago

papiliond commented 1 month ago

API Platform version(s) affected: 4.0.1 and below

Description
I am using the <DeleteButton /> provided by react-admin to delete a record. This API call makes my server return a 500 response, which should trigger an error notification on the frontend. Instead, I am redirected to the list page and the ra.notification.deleted is displayed.

The mutationMode is "pessimistic". I debugged the useDeleteWithConfirmController.tsx hook and i can confirm that the onSuccess method is called immediately after the deletion confirmation modal is confirmed. The onError is not called at all. Moreover, the convertHydraResponseToReactAdminResponse in hydra/dataProvider.js is called once, with a null response.

How to reproduce

  1. Make sure that calling DELETE on your api returns 500 status response. UPDATE: It happens with 4** errors, too.
  2. Go to an "Edit" page and hit "Delete".
  3. You will be redirected to the list page with a success notification at the bottom.

Additional Context
Console log from the convertHydraResponseToReactAdminResponse function in dataProvider.js:

'{"type":"DELETE","resource":"users","params":{"id":"/api/users/1","previousData":{"@context":"/api/contexts/User","@id":"/api/users/1","@type":"User","id":"/api/users/1","uuid":"3daa234b-e5fd-49e5-b2b3-e55f9d222b93","authenticatorId":5,"fullname":"Rita Roberts I","email":"admin@test.test","username":"admin-test-test","roles":["ROLE_ADMIN","ROLE_USER"],"password":"$2y$13$mx5JvkNodAbSrr6DYruA9eO7mQr8/Xq8jUk/N2pa1UANw7pCLz6iO","isEnabled":true,"team":["/api/teams/1"],"teamRoles":[{"team":"/api/teams/1","roleGroup":"/api/role_groups/2"}],"isDeletable":true,"createdBy":"n/a","createdAt":"2024-08-13T20:18:53+00:00","updatedAt":"2024-08-13T20:18:55+00:00","userIdentifier":"admin-test-test","originId":"1"}},"response":null}'

papiliond commented 1 month ago

Closing as it was an issue in our custom httpClient.

const fetchHydra = async (url, options = {}) => {
  try {
    const result = await baseFetchHydra(url, {
      ...options,
      headers: getHeaders
    });

    return result;
  } catch (error) {
    if (error?.response?.status === 503) {
      setTimeout(() => {
        window.location.reload();
      }, 0);
    } else {
     // Here I had to rethrow the error.
      throw error;
    }
  }

  return null;
};