Skip to content

Commit

Permalink
Let ajax function return all data on not ok (#976)
Browse files Browse the repository at this point in the history
Let ajax functions return all data on not ok so we can examine the
error code if necessary.
  • Loading branch information
70ray authored Dec 10, 2023
1 parent 965a8d5 commit d7a1767
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
19 changes: 15 additions & 4 deletions SETUP/tests/jsTests/ajaxTests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global QUnit ajax */
/* global QUnit ajax AJAX_ERROR_CODES */
/* exported codeUrl */

let codeUrl = "https://www.dummy.org";
Expand Down Expand Up @@ -27,7 +27,7 @@ QUnit.module("Ajax test", function() {

return ajax("GET", "myUrl", {}, {}, fetchPromise)
.then(function() {}, function(data) {
assert.strictEqual(data, "Incorrect response type");
assert.deepEqual(data, {error: "Incorrect response type", code: AJAX_ERROR_CODES.INCORRECT_RESPONSE_TYPE});
});
});

Expand All @@ -40,7 +40,7 @@ QUnit.module("Ajax test", function() {

return ajax("GET", "myUrl", {}, {}, fetchPromise)
.then(function() {}, function(data) {
assert.strictEqual(data, "not found");
assert.strictEqual(data.error, "not found");
});
});

Expand All @@ -53,7 +53,18 @@ QUnit.module("Ajax test", function() {

return ajax("GET", "myUrl", {}, {}, fetchPromise)
.then(function() {}, function(data) {
assert.strictEqual(data, "Unknown error");
assert.deepEqual(data, {error: "Unknown error", code: AJAX_ERROR_CODES.UNKNOWN_ERROR});
});
});

QUnit.test("Network error", function (assert) {
function fetchPromise() {
return Promise.reject();
}

return ajax("GET", "myUrl", {}, {}, fetchPromise)
.then(function() {}, function(data) {
assert.deepEqual(data, {error: "Network error", code: AJAX_ERROR_CODES.NETWORK_ERROR});
});
});

Expand Down
25 changes: 18 additions & 7 deletions scripts/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/*global codeUrl */
/* exported ajax */
/* exported ajax ajaxAlert AJAX_ERROR_CODES */

const AJAX_ERROR_CODES = {
UNKNOWN_ERROR: 999,
INCORRECT_RESPONSE_TYPE: 998,
NETWORK_ERROR: 997,
};

function ajax(method, apiUrl, queryParams = {}, data = {}, fetchPromise = fetch) {
let url = new URL(codeUrl + "/api/index.php");
Expand All @@ -25,20 +31,25 @@ function ajax(method, apiUrl, queryParams = {}, data = {}, fetchPromise = fetch)
.then(function(response) {
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
reject("Incorrect response type");
reject({error: "Incorrect response type", code: AJAX_ERROR_CODES.INCORRECT_RESPONSE_TYPE});
} else if(response.ok) {
resolve(response.json());
} else {
response.json()
.then(function(data) {
let message = data.error;
if(!message) {
message = "Unknown error";
if(!data) {
data = {error: "Unknown error", code: AJAX_ERROR_CODES.UNKNOWN_ERROR};
}
reject(message);
reject(data);
});
}
})
.catch(reject);
.catch(function() {
reject({error: "Network error", code: AJAX_ERROR_CODES.NETWORK_ERROR});
});
});
}

function ajaxAlert(data) {
alert(data.error);
}
14 changes: 7 additions & 7 deletions scripts/page_browse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global $ proofIntData ajax makeImageWidget makeTextWidget viewSplitter */
/*global $ proofIntData ajax ajaxAlert makeImageWidget makeTextWidget viewSplitter */
/* exported pageBrowse */

function makePageControl(pages, selectedImageFileName, changePage) {
Expand Down Expand Up @@ -124,8 +124,8 @@ function pageBrowse(params, storageKey, replaceUrl, mentorMode = false, setShowF
});
resolve();
})
.catch(function(error) {
alert(error);
.catch(function(data) {
ajaxAlert(data);
reject();
});
}
Expand Down Expand Up @@ -167,7 +167,7 @@ function pageBrowse(params, storageKey, replaceUrl, mentorMode = false, setShowF
ajax("GET", `v1/projects/${projectId}/pages/${imageFileName}/pagerounds/${round}`)
.then(function(data) {
textWidget.setText(data.text);
}, alert);
}, ajaxAlert);
}
replaceUrl();
}
Expand Down Expand Up @@ -330,7 +330,7 @@ function pageBrowse(params, storageKey, replaceUrl, mentorMode = false, setShowF

function getPages() {
ajax("GET", `v1/projects/${projectId}/pages`)
.then(displayPages, alert);
.then(displayPages, ajaxAlert);
}

function showProjectInfo(projectData) {
Expand All @@ -348,8 +348,8 @@ function pageBrowse(params, storageKey, replaceUrl, mentorMode = false, setShowF

getProjectData = function() {
ajax("GET", `v1/projects/${projectId}`)
.then(showProjectInfo, function(error) {
alert(error);
.then(showProjectInfo, function(data) {
ajaxAlert(data);
selectAProject();
});
};
Expand Down

0 comments on commit d7a1767

Please sign in to comment.