2014-03-02 18:30:35 +04:00
|
|
|
/* global ic */
|
2014-05-24 07:25:20 +04:00
|
|
|
|
|
|
|
var ajax = window.ajax = function () {
|
2014-03-03 00:12:06 +04:00
|
|
|
return ic.ajax.request.apply(null, arguments);
|
|
|
|
};
|
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
|
2014-10-25 01:09:50 +04:00
|
|
|
function getRequestErrorMessage(request, performConcat) {
|
2014-05-24 07:25:20 +04:00
|
|
|
var message,
|
|
|
|
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'
|
2014-05-24 07:25:20 +04:00
|
|
|
if (request.responseJSON.errors && Ember.isArray(request.responseJSON.errors)) {
|
|
|
|
message = request.responseJSON.errors.map(function (errorItem) {
|
|
|
|
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) {
|
2014-06-02 00:53:16 +04: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 07:43:25 +04:00
|
|
|
if (performConcat && Ember.isArray(message)) {
|
|
|
|
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
|
|
|
}
|
2014-05-24 07:25:20 +04:00
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
export {getRequestErrorMessage, ajax};
|
2014-05-24 07:25:20 +04:00
|
|
|
export default ajax;
|