1/** 2 * @module jira/ajs/ajax/ajax-util 3 */ 4define('jira/ajs/ajax/ajax-util', ['jira/util/formatter', 'underscore', 'exports'], function (formatter, _, exports) { 5 "use strict"; 6 7 var messageFromErrorCollection = function messageFromErrorCollection(data) { 8 if (data) { 9 if (_.isArray(data.errorMessages) && !_.isEmpty(data.errorMessages)) { 10 return data.errorMessages.join(' '); 11 } else if (_.isObject(data.errors) && !_.isEmpty(data.errors)) { 12 return _.values(data.errors).join(' '); 13 } 14 } 15 return null; 16 }; 17 var getMessageFromXHRBody = function getMessageFromXHRBody(xhr) { 18 var data; 19 try { 20 data = xhr.responseText && JSON.parse(xhr.responseText); 21 return messageFromErrorCollection(data); 22 } catch (e) { 23 //fall through because its not JSON. 24 } 25 return null; 26 }; 27 var getMessageFromXHRStatus = function getMessageFromXHRStatus(xhr) { 28 if (xhr.status === 401) { 29 return formatter.I18n.getText("common.ajax.unauthorised.alert"); 30 } else if (xhr.responseText) { 31 return formatter.I18n.getText("common.ajax.servererror"); 32 } else { 33 return formatter.I18n.getText("common.ajax.commserror"); 34 } 35 }; 36 var WEBSUDO_REGEX = /websudo/i; 37 38 /** 39 * Returns a description on why the passed XHR failed. 40 * 41 * @param {XMLHttpRequest} xhr to check. 42 * @returns {string} a description on why the passed XHR failed. Will always return something even if it must 43 * be a generic message. 44 */ 45 exports.getErrorMessageFromXHR = function (xhr) { 46 return getMessageFromXHRBody(xhr) || getMessageFromXHRStatus(xhr); 47 }; 48 49 /** 50 * Determine if the passed xhr failed because of WebSudo. 51 * 52 * @param {XMLHttpRequest} xhr to test 53 * @returns {boolean} true if the passed XHR failed because of WebSudo or false otherwise. 54 */ 55 exports.isWebSudoFailure = function (xhr) { 56 return xhr && xhr.status === 401 && WEBSUDO_REGEX.test(xhr.responseText); 57 }; 58}); 59AJS.namespace("JIRA.Ajax", null, require('jira/ajs/ajax/ajax-util'));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.