cartodb-4.42/lib/assets/javascripts/builder/helpers/error-parser.js
2024-04-06 05:25:13 +00:00

30 lines
777 B
JavaScript
Executable File

var _ = require('underscore');
var DEFAULT_ERROR_MSG = '';
/**
* Return error message when backend request fails
* It tries to get responseText > errors and error arrays, if not gets `statusText`.
*/
module.exports = function (e) {
if (!e) { throw new Error('error is required'); }
try {
var responseText = e.responseText && e.responseText.trim() && JSON.parse(e.responseText);
var errorMessage = e.statusText || DEFAULT_ERROR_MSG;
if (responseText) {
var errors = _.compact(
_.map(['errors', 'error'], function (type) {
return responseText[type] && responseText[type].join(', ');
})
);
errorMessage = errors.join(', ');
}
return errorMessage;
} catch (err) {
return DEFAULT_ERROR_MSG;
}
};