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

Handle arrays of errors, not just strings #10500

Closed
dalen opened this issue Feb 7, 2025 · 1 comment
Closed

Handle arrays of errors, not just strings #10500

dalen opened this issue Feb 7, 2025 · 1 comment

Comments

@dalen
Copy link

dalen commented Feb 7, 2025

Is your feature request related to a problem? Please describe.

As backend I'm using an ASP.net Core server, and the default server validation messages it returns are arrays of string, not strings.

Currently, to make react admin handle them I need some code in the dataprovider to join it into strings. But it would be great if react admin could handle this out of the box. As it is the default behavior for all ASP.net backends, I imagine I'm not alone in having this issue.

Here's an example of the validation error JSON:

{
    "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "errors": {
        "stats.[0].stat": [
            "Duplicate Stat"
        ],
        "stats.[1].stat": [
            "Duplicate Stat"
        ]
    },
    "traceId": "00-027d52f35d25d285582d2265c9dc7e3e-fc67a0611aaa1057-00"
}

Describe the solution you'd like

Just doing a .join(', ') if the error is an array would be fine by me.

Describe alternatives you've considered

Currently I have code to catch the HttpError and modify it:

      try {
        const { json } = await httpClient(
          `${apiUrl}/api/${resourceUrl(resource)}`,
          {
            method: 'POST',
            body: JSON.stringify(params.data),
            ...httpOptions(),
          },
        );
        return {
          data: { ...params.data, id: json.id },
        };
      } catch (e) {
        if (!(e instanceof HttpError)) {
          throw e;
        }
        throw new HttpError(e.message, e.status, {
          ...e.body,
          errors: Object.fromEntries(
            Object.entries(e.body.errors as { [key: string]: string[] }).map(
              (error) => [error[0], error[1].join(', ')],
            ),
          ),
        });
      }

Additional context

Their error response is based on https://datatracker.ietf.org/doc/html/rfc7807 according to their docs at https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.problemdetails?view=aspnetcore-9.0

@djhi
Copy link
Collaborator

djhi commented Feb 11, 2025

This is the job of your dataProvider. We don't want to include code specific to a backend implementation. You could wrap the default httpClient to handle this instead of repeating the error handling in all your dataProvider methods.

@djhi djhi closed this as completed Feb 11, 2025
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

2 participants