Skip to content

Commit 46dee87

Browse files
committed
pat inject: Allow configurable error pages. Can be disabled by adding pat-inject-errorhandler.off to the URL's query string.
1 parent b4cc0c3 commit 46dee87

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
- pat tabs: Refactor based on ``ResizeObserver`` and fix problems calculating the with with transitions.
5555
- pat tabs: When clicking on the ``extra-tabs`` element, toggle between ``open`` and ``closed`` classes to allow opening/closing an extra-tabs menu via CSS.
5656
- pat autofocus: Do not autofocus in iframes. Fixes: #761.
57-
- pat inject: Allow configurable error pages.
57+
- pat inject: Allow configurable error pages. Can be disabled by adding ``pat-inject-errorhandler.off`` to the URL's query string.
5858

5959
### Technical
6060

src/pat/inject/documentation.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ The code looks for a meta tag with the name ``pat-inject-`` plus the HTTP status
335335

336336
Another example: You can present the user with a login page in case the session has expired (``401`` error).
337337

338+
You can disable this behavior for debugging by adding the following parameter to the query string:
339+
``pat-inject-errorhandler.off``.
340+
338341
### Options reference
339342

340343
You can customise the behaviour of injection through options in the `data-pat-inject` attribute.

src/pat/inject/inject.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,15 @@ const inject = {
677677
"It seems, the server is down. Please make a screenshot and contact support. Thank you!";
678678
}
679679

680+
const url_params = new URLSearchParams(window.location.search);
681+
680682
const fallback_url = document
681683
.querySelector(`meta[name=pat-inject-${status}]`)
682684
?.getAttribute("content", false);
683-
if (fallback_url) {
685+
if (
686+
fallback_url &&
687+
url_params.get("pat-inject-errorhandler.off") === null
688+
) {
684689
try {
685690
const fallback_response = await fetch(fallback_url, {
686691
method: "GET",

src/pat/inject/inject.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,5 +973,50 @@ describe("pat-inject", function () {
973973

974974
done();
975975
});
976+
977+
it("Doesnt get error page from meta tags if query string present", async (done) => {
978+
delete global.window.location;
979+
global.window.location = {
980+
search: "?something=nothing&pat-inject-errorhandler.off",
981+
};
982+
983+
global.fetch = jest.fn().mockImplementation(
984+
mockFetch(`
985+
<!DOCTYPE html>
986+
<html>
987+
<head>
988+
<title>404</title>
989+
</head>
990+
<body>
991+
<h1>oh-nose!</h1>
992+
</body>
993+
</html>
994+
`)
995+
);
996+
997+
// apparently <head> is empty if we do not set it.
998+
document.head.innerHTML = `
999+
<meta name="pat-inject-404" content="/404.html" />
1000+
`;
1001+
1002+
pattern.init($a);
1003+
1004+
// Invoke error case
1005+
pattern._onInjectError($a, [], {
1006+
jqxhr: { status: 404 },
1007+
});
1008+
await utils.timeout(1); // wait a tick for async to settle.
1009+
1010+
expect(document.body.querySelector("#lab")).toBeTruthy();
1011+
// In this case, the normal error reporting is used
1012+
expect(
1013+
document.body.hasAttribute("data-error-message")
1014+
).toBeTruthy();
1015+
1016+
global.fetch.mockClear();
1017+
delete global.fetch;
1018+
1019+
done();
1020+
});
9761021
});
9771022
});

0 commit comments

Comments
 (0)