Skip to content

Commit 7cff80b

Browse files
authored
Refactor duplicate error handling into fetchErrorMessage helper (#7644)
## What Changed Extracts duplicate error handling logic for 404/500 status codes in `VerifyThenLoadAPIContent` into a reusable `fetchErrorMessage` helper function. Addresses review feedback from #7642. ## Type - [ ] ✨ Feature - [ ] 🐛 Bug fix - [x] ♻️ Refactor - [ ] 🏗️ Build/Infrastructure - [ ] 🔒 Security ## Testing No functional change. The helper function encapsulates the same AJAX call pattern previously duplicated in both 404 and 500 handlers. Before (repeated in both handlers): ```javascript 404: function () { try { $.ajax({ method: "GET", url: url, async: false, dataType: "json", success: function (data) { var msg = data && data.message ? data.message : error; window.CRM.DisplayErrorMessage(url, { message: msg }); }, error: function () { window.CRM.DisplayErrorMessage(url, { message: error }); }, }); } catch (e) { window.CRM.DisplayErrorMessage(url, { message: error }); } } ``` After: ```javascript 404: function () { fetchErrorMessage(url, error, function (msg) { window.CRM.DisplayErrorMessage(url, { message: msg }); }); } ``` ## Screenshots N/A - No UI changes ## Security Check - [ ] Introduces new input validation - [ ] Modifies authentication/authorization - [ ] Affects data privacy/GDPR ### Code Quality - [x] Database: Propel ORM only, no raw SQL - [x] No deprecated attributes (align, valign, nowrap, border, cellpadding, cellspacing, bgcolor) - [x] Bootstrap CSS classes used - [x] All CSS bundled via webpack ## Pre-Merge - [x] Tested locally - [x] No new warnings - [x] Build passes - [x] Backward compatible (or migration documented) <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/ChurchCRM/CRM/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.
2 parents 3bab219 + 6df7f15 commit 7cff80b

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

src/skin/js/CRMJSOM.js

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ window.CRM.VerifyThenLoadAPIContent = function (url) {
5353
var error = i18next.t(
5454
"There was a problem retrieving the requested object",
5555
);
56+
57+
// Helper function to fetch error message from JSON response
58+
function fetchErrorMessage(targetUrl, fallbackError, callback) {
59+
try {
60+
$.ajax({
61+
method: "GET",
62+
url: targetUrl,
63+
async: false,
64+
dataType: "json",
65+
success: function (data) {
66+
var msg = data && data.message ? data.message : fallbackError;
67+
callback(msg);
68+
},
69+
error: function () {
70+
callback(fallbackError);
71+
},
72+
});
73+
} catch (e) {
74+
callback(fallbackError);
75+
}
76+
}
77+
5678
$.ajax({
5779
method: "HEAD",
5880
url: url,
@@ -62,44 +84,14 @@ window.CRM.VerifyThenLoadAPIContent = function (url) {
6284
window.open(url);
6385
},
6486
404: function () {
65-
// Try to fetch the body to get a more helpful message (the route may return JSON)
66-
try {
67-
$.ajax({
68-
method: "GET",
69-
url: url,
70-
async: false,
71-
dataType: "json",
72-
success: function (data) {
73-
var msg = data && data.message ? data.message : error;
74-
window.CRM.DisplayErrorMessage(url, { message: msg });
75-
},
76-
error: function () {
77-
window.CRM.DisplayErrorMessage(url, { message: error });
78-
},
79-
});
80-
} catch (e) {
81-
window.CRM.DisplayErrorMessage(url, { message: error });
82-
}
87+
fetchErrorMessage(url, error, function (msg) {
88+
window.CRM.DisplayErrorMessage(url, { message: msg });
89+
});
8390
},
8491
500: function () {
85-
// For 500, try to fetch JSON body for a clearer message
86-
try {
87-
$.ajax({
88-
method: "GET",
89-
url: url,
90-
async: false,
91-
dataType: "json",
92-
success: function (data) {
93-
var msg = data && data.message ? data.message : error;
94-
window.CRM.DisplayErrorMessage(url, { message: msg });
95-
},
96-
error: function () {
97-
window.CRM.DisplayErrorMessage(url, { message: error });
98-
},
99-
});
100-
} catch (e) {
101-
window.CRM.DisplayErrorMessage(url, { message: error });
102-
}
92+
fetchErrorMessage(url, error, function (msg) {
93+
window.CRM.DisplayErrorMessage(url, { message: msg });
94+
});
10395
},
10496
},
10597
});

0 commit comments

Comments
 (0)