mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 03:14:03 +03:00
0e9d4e6792
Refs #4001 - grunt-jscs@0.8.1 which provides ES6 support.
52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
/* global ic */
|
|
|
|
var ajax = window.ajax = function () {
|
|
return ic.ajax.request.apply(null, arguments);
|
|
};
|
|
|
|
// Used in API request fail handlers to parse a standard api error
|
|
// response json for the message to display
|
|
function getRequestErrorMessage(request, performConcat) {
|
|
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 {
|
|
// Try to parse out the error, or default to 'Unknown'
|
|
if (request.responseJSON.errors && Ember.isArray(request.responseJSON.errors)) {
|
|
message = request.responseJSON.errors.map(function (errorItem) {
|
|
return errorItem.message;
|
|
});
|
|
} else {
|
|
message = request.responseJSON.error || 'Unknown Error';
|
|
}
|
|
} catch (e) {
|
|
msgDetail = request.status ? request.status + ' - ' + request.statusText : 'Server was not available';
|
|
message = 'The server returned an error (' + msgDetail + ').';
|
|
}
|
|
}
|
|
|
|
if (performConcat && Ember.isArray(message)) {
|
|
message = message.join('<br />');
|
|
}
|
|
|
|
// return an array of errors by default
|
|
if (!performConcat && typeof message === 'string') {
|
|
message = [message];
|
|
}
|
|
|
|
return message;
|
|
}
|
|
|
|
export {getRequestErrorMessage, ajax};
|
|
export default ajax;
|