Skip to content

Commit

Permalink
Ignore page not found error handling by Luigi (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDoberer authored May 3, 2024
1 parent 923c1a7 commit 3384998
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
3 changes: 3 additions & 0 deletions core/src/core-api/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class LuigiI18NManager {
* findInterpolations('Environment {num}', {num: 1})
*/
findInterpolations(value, interpolations) {
if (typeof value !== 'string' || !value.trim()) {
return value;
}
Object.keys(interpolations).forEach(item => {
value = value.replace(
new RegExp('{' + EscapingHelpers.escapeKeyForRegexp(item) + '}', 'gi'),
Expand Down
4 changes: 4 additions & 0 deletions core/src/services/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,11 @@ class RoutingClass {

async showPageNotFoundError(component, pathToRedirect, notFoundPath, isAnyPathMatched = false, config = {}) {
const redirectResult = RoutingHelpers.getPageNotFoundRedirectResult(notFoundPath, isAnyPathMatched);
if (redirectResult.ignoreLuigiErrorHandling) {
return;
}
const redirectPathFromNotFoundHandler = redirectResult.path;

if (redirectPathFromNotFoundHandler) {
if (redirectResult.keepURL) {
this.handleRouteChange(redirectPathFromNotFoundHandler, component, IframeHelpers.getIframeContainer(), config);
Expand Down
5 changes: 3 additions & 2 deletions core/src/utilities/helpers/routing-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,11 @@ class RoutingHelpersClass {
if (typeof pageNotFoundHandler === 'function') {
// custom 404 handler is provided, use it
const result = pageNotFoundHandler(notFoundPath, isAnyPathMatched);
if (result && result.redirectTo) {
if (result && (result.redirectTo || result.ignoreLuigiErrorHandling)) {
return {
path: result.redirectTo,
keepURL: result.keepURL
keepURL: result.keepURL,
ignoreLuigiErrorHandling: result.ignoreLuigiErrorHandling
};
}
}
Expand Down
15 changes: 15 additions & 0 deletions core/test/services/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,7 @@ describe('Routing', function() {
sinon.stub(RoutingHelpers, 'showRouteNotFoundAlert');
sinon.stub(LuigiI18N, 'getTranslation');
sinon.stub(component, 'showAlert');
sinon.stub(Routing, 'handleRouteChange');
});

it('navigate to redirect path', async () => {
Expand All @@ -1194,6 +1195,20 @@ describe('Routing', function() {

sinon.assert.calledWithExactly(Routing.navigateTo, pathToRedirect2);
});
it('do nothing if ignoreLuigiErrorHandling is implmented by the custom handler', () => {
const custom = {
handler: () => {
return {
ignoreLuigiErrorHandling: true
};
}
};
LuigiConfig.getConfigValue.returns(custom.handler);
Routing.showPageNotFoundError(component, pathToRedirect, notFoundPath);
sinon.assert.notCalled(Routing.handleRouteChange);
sinon.assert.notCalled(Routing.navigateTo);
sinon.assert.notCalled(RoutingHelpers.showRouteNotFoundAlert);
});
});
describe('dynamicNode', () => {
const nodeData = {
Expand Down
7 changes: 5 additions & 2 deletions core/test/utilities/helpers/routing-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,20 +772,23 @@ describe('Routing-helpers', () => {
it('with custom pageNotFoundHandler defined keepURL', async () => {
const customKeepURL = true;
const somePath = 'somePath';
const ignoreLuigiErrorHandling = undefined;
sinon
.stub(LuigiConfig, 'getConfigValue')
.withArgs('routing.pageNotFoundHandler')
.returns(() => {
return {
redirectTo: somePath,
keepURL: customKeepURL
keepURL: customKeepURL,
ignoreLuigiErrorHandling: ignoreLuigiErrorHandling
};
});
const expected = await RoutingHelpers.getPageNotFoundRedirectResult('');
assert.deepEqual(
{
path: somePath,
keepURL: customKeepURL
keepURL: customKeepURL,
ignoreLuigiErrorHandling: ignoreLuigiErrorHandling
},
expected
);
Expand Down
3 changes: 3 additions & 0 deletions docs/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class I18nProvider {
* findInterpolations('Environment {num}', {num: 1})
*/
findInterpolations(value, interpolations) {
if (typeof value !== 'string' || !value.trim()) {
return value;
}
Object.keys(interpolations).forEach(item => {
value = value.replace(
new RegExp('{' + EscapingHelpers.escapeKeyForRegexp(item) + '}', 'gi'),
Expand Down
3 changes: 2 additions & 1 deletion docs/navigation-parameters-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ You can configure the way Luigi tackles routing in your application in the `rout

### pageNotFoundHandler
- **type**: any
- **description**: defines custom behavior when a `404` error occurs. Luigi handles it by default. Leave its body empty if you have an external `404` handling. You can return an Object with **redirectTo** and **keepURL** as parameters. You can use the **redirectTo** parameter if you want Luigi to redirect to a specific navigation path after execution. Setting the **keepURL** parameter to `true` will keep the erroneous URL onto the browser's address bar.
- **description**: defines custom behavior when a `404` error occurs. Luigi handles it by default. Leave its body empty if you have an external `404` handling. You can return an Object with **redirectTo** and **keepURL** as parameters. You can use the **redirectTo** parameter if you want Luigi to redirect to a specific navigation path after execution. Setting the **keepURL** parameter to `true` will keep the erroneous URL onto the browser's address bar.
If you don't want Luigi to handle the page not found error, because you've implemented your own function for this purpose, you can return an Object with **ignoreLuigiErrorHandling** set to `true`.
- **attributes**:
- **wrongPath** (string): the path that the user tried navigating to.
- **wasAnyPathFitted** (bool): it is true if Luigi managed to fit a valid path which means **wrongPath** was only partially wrong. Otherwise it is false.
Expand Down

0 comments on commit 3384998

Please sign in to comment.