Hi,
Is it possible to add in future release a custom and simplified error handling within a custom rest client?
I read #180, but personally I think it's a very heavy task for such a simple requirement. Wouldn't be worth it to simplify custom errors handling ? I Think it's a common problem
I've the same need. Here's my finding:
crudResponse.js will render message in HttpError object or 'aor.notification.http_error'case CRUD_GET_LIST_FAILURE:
case CRUD_GET_MANY_FAILURE:
case CRUD_GET_MANY_REFERENCE_FAILURE:
case CRUD_CREATE_FAILURE:
case CRUD_UPDATE_FAILURE:
case CRUD_DELETE_FAILURE: {
console.error(error);
const errorMessage = typeof error === 'string'
? error
: (error.message || 'aor.notification.http_error');
return yield put(showNotification(errorMessage, 'warning'));
}
HttpError object is generated by fetchUtil.jsif (status < 200 || status >= 300) {
return Promise.reject(new HttpError((json && json.message) || statusText, status));
}
restClientconst { url, options } = convertRESTRequestToHTTP(type, resource, params);
return httpClient(url, options)
.then(response =>
convertHTTPResponseToREST(response, type, resource, params)
)
.catch(error => {
// error is HttpError object
console.log(error, error.message, error.status);
return Promise.reject(error); // rethrow it
});
However the body of my error response is { _erorr: "..." } so it is not assigned to HttpError.message.
HttpError only stores "Internal Server Error", 500, which is not particularly useful.
I call for passing json as 3rd parameter to HttpError so we can do custom remapping.
// fetchUtil.js
if (status < 200 || status >= 300) {
return Promise.reject(new HttpError((json && json.message) || statusText, status, json || body));
}
// custom restClient
.catch(error => {
// error is HttpError object
console.log(error, error.message, error.status, error.body);
return Promise.reject({ message: error.body._error });
});
@fzaninotto what do you think?
There is no standard in REST land about passing error messages. I suggest you use your own fetch function instead of the one from fetchUtils in your case.
But I agree that passing the json to the HttpError constructor makes it easier, so I'm +1 for that change. Feel free to send us a PR abut it.
should this be closed by https://github.com/marmelab/admin-on-rest/commit/57dbd86128037096a445f152f4f339113c0aada7#diff-af187ae2de6c59a4ec26814f04fddacd ?
Absolutely, thanks for pointing!
Oh great, it's what I needed :).
Most helpful comment
I've the same need. Here's my finding:
crudResponse.jswill rendermessageinHttpErrorobject or'aor.notification.http_error'HttpErrorobject is generated byfetchUtil.jsrestClientHowever the body of my error response is
{ _erorr: "..." }so it is not assigned toHttpError.message.HttpErroronly stores "Internal Server Error", 500, which is not particularly useful.I call for passing
jsonas 3rd parameter toHttpErrorso we can do custom remapping.@fzaninotto what do you think?