Is it safe to (can I) use `HttpError` from 'admin-on-rest/lib/util/HttpError'?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Is it safe to (can I) use `HttpError` from 'admin-on-rest/lib/util/HttpError'?



My API provider is sending error messages in the response.body as a ReadableStream as plain text and I need to get admin-on-rest to display those messages to the end user.


response.body



I'm planning to do so like this:


import HttpError from 'admin-on-rest/lib/util/HttpError'
import _ from 'lodash';

function handleError(response)
if (_.isFunction(response.text))
const statusText = response.statusText;
const status = response.status;
return response.text().then(text => );
else throw new Error(response.statusText);



And in my custom REST Client


//...
if (response.status < 200 || response.status >= 300)
return handleError(response);

//...



But I feel that accessing to HttpError directly through 'admin-on-rest/lib/util/HttpError' intead of 'admin-on-rest' is insecure because if the internal implementation changes (or there is a refactor of the internal classes of the framework, etc) my implementation will fail.


HttpError


'admin-on-rest/lib/util/HttpError'


'admin-on-rest'



I cannot do import HttpError from 'admin-on-rest/lib/util/HttpError' because it doesn't work that way. I get __WEBPACK_IMPORTED_MODULE_0_admin_on_rest___default.a is not a constructor


import HttpError from 'admin-on-rest/lib/util/HttpError'


__WEBPACK_IMPORTED_MODULE_0_admin_on_rest___default.a is not a constructor


HttpError


import HttpError from 'admin-on-rest/lib/util/HttpError'




1 Answer
1



A simple and safe(er) alternative can be just not to use HttpError at all.


HttpError



Instead do Promise.reject with a simple object as argument, which contains a message and status properties like this:


Promise.reject


message


status


//...
return response.text().then(text =>
return Promise.reject(message: text );
);
//...






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard