show failure message on failed async form requests

This commit is contained in:
Felix Hamann 2019-03-07 19:22:28 +01:00
parent 010718dfab
commit fb26d70c40

View File

@ -6,9 +6,12 @@
var ASYNC_FORM_RESPONSE_CLASS = 'async-form-response';
var ASYNC_FORM_LOADING_CLASS = 'async-form-loading';
var ASYNC_FORM_MIN_DELAY = 600;
var DEFAULT_FAILURE_MESSAGE = 'The response we received from the server did not match what we expected. Please let us know this happened via the help widget in the top navigation.';
window.utils.asyncForm = function(formElement, options) {
options = options || {};
var lastRequestTimestamp = 0;
function setup() {
@ -47,11 +50,21 @@
window.utils.httpClient.post(url, headers, body)
.then(function(response) {
return response.json();
if (response.headers.get("content-type").indexOf("application/json") !== -1) {// checking response header
return response.json();
} else {
throw new TypeError('Response from "' + url + '" has unexpected Content-Type. Expected: "application/json". Received: "' + (response.headers.get("content-type") || '(undefined)') + '"');
}
}).then(function(response) {
processResponse(response[0])
processResponse(response[0]);
}).catch(function(error) {
console.error('could not fetch or process response from ' + url, { error });
var failureMessage = DEFAULT_FAILURE_MESSAGE;
if (options.i18n && options.i18n.asyncFormFailure) {
failureMessage = options.i18n.asyncFormFailure;
}
processResponse({ content: failureMessage });
formElement.classList.remove(ASYNC_FORM_LOADING_CLASS);
});
}