2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2014-05-24 07:25:20 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {isArray} = Ember;
|
|
|
|
|
2014-05-24 07:25:20 +04:00
|
|
|
// Used in API request fail handlers to parse a standard api error
|
|
|
|
// response json for the message to display
|
2015-05-27 03:41:12 +03:00
|
|
|
export default function getRequestErrorMessage(request, performConcat) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let message,
|
2014-05-24 07:25:20 +04:00
|
|
|
msgDetail;
|
|
|
|
|
|
|
|
// Can't really continue without a request
|
|
|
|
if (!request) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems like a sensible default
|
|
|
|
message = request.statusText;
|
|
|
|
|
|
|
|
// If a non 200 response
|
|
|
|
if (request.status !== 200) {
|
|
|
|
try {
|
2014-06-02 00:53:16 +04:00
|
|
|
// Try to parse out the error, or default to 'Unknown'
|
2015-10-28 14:36:45 +03:00
|
|
|
if (request.responseJSON.errors && isArray(request.responseJSON.errors)) {
|
|
|
|
message = request.responseJSON.errors.map((errorItem) => {
|
2014-05-24 07:25:20 +04:00
|
|
|
return errorItem.message;
|
2014-06-30 07:43:25 +04:00
|
|
|
});
|
2014-05-24 07:25:20 +04:00
|
|
|
} else {
|
2014-06-02 00:53:16 +04:00
|
|
|
message = request.responseJSON.error || 'Unknown Error';
|
2014-05-24 07:25:20 +04:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2015-10-28 14:36:45 +03:00
|
|
|
msgDetail = request.status ? `${request.status} - ${request.statusText}` : 'Server was not available';
|
|
|
|
message = `The server returned an error (${msgDetail}).`;
|
2014-05-24 07:25:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (performConcat && isArray(message)) {
|
2014-06-30 07:43:25 +04:00
|
|
|
message = message.join('<br />');
|
|
|
|
}
|
|
|
|
|
|
|
|
// return an array of errors by default
|
|
|
|
if (!performConcat && typeof message === 'string') {
|
|
|
|
message = [message];
|
|
|
|
}
|
|
|
|
|
2014-05-24 07:25:20 +04:00
|
|
|
return message;
|
2014-10-25 01:09:50 +04:00
|
|
|
}
|