Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw 404 error from the client side #138

Closed
DB-Alex opened this issue Sep 12, 2019 · 7 comments
Closed

Throw 404 error from the client side #138

DB-Alex opened this issue Sep 12, 2019 · 7 comments

Comments

@DB-Alex
Copy link

DB-Alex commented Sep 12, 2019

Hi,

Is it possible to throw a 404 error from the client side?

I was trying something like this:

throw new NotFoundError(request.originalUrl);

But the NotFoundBoundary does not come up.

@jamesknelson
Copy link
Collaborator

It should be possible to import NotFoundBoundary from Navi and throw that, as it's exported. If you do this from within a component, then the boundary should be triggered.

Where are you throwing the error from?

@DB-Alex
Copy link
Author

DB-Alex commented Sep 13, 2019

I have a multi language app so in my router I have:

'/how-it-works': withLocale({
            'en': lazy(() => import('pages/HowItWorksPage')),
            'nl': redirect('/werkwijze/'),
        }),

Now when someone tries the access this route with locale de there is no route so it should throw a 404.

const withLocale = (matchers) => {
    return map((request, context) => {
        if (matchers[context.locale]) {
            return matchers[context.locale];
        }

        // Throw 404 when nothing found for current locale
        throw new NotFoundError(request.originalUrl);
    });
};

The NotFoundBoundary is already in my I just need it to be triggered so it shows the 404 page.

@jamesknelson
Copy link
Collaborator

Hmm... I would have thought this would triggered the 404 page already.

Is the component with the NotFoundBoundary rendered in the root view?

@DB-Alex
Copy link
Author

DB-Alex commented Sep 17, 2019

The NotFoundBoundary is in my App component and in routes.js I have:

export default compose(
    withView((request, context) =>
        <App
            currentUser={context.currentUser}
            appLocale={context.locale}
        />,
    ),

@jamesknelson
Copy link
Collaborator

Okay. And just to check, the <App /> component has a <View /> inside it?

Is there any chance you could get a minimal reproduction on working on CodeSandbox or similar?

@DB-Alex
Copy link
Author

DB-Alex commented Sep 18, 2019

This is inside app render:

<div className={classes['root']}>
                                        <BottomMenu />
                                        <MobileWrapper>
                                            <Header />
                                            <Main theme={theme}>
                                                <NotFoundBoundary render={NotFound}>
                                                    <View />
                                                </NotFoundBoundary>
                                            </Main>
                                            <Footer theme={theme} />
                                            <LiveChat />
                                        </MobileWrapper>
                                    </div>

@DB-Alex
Copy link
Author

DB-Alex commented Sep 26, 2019

I fixed it:

<NotFoundBoundary render={NotFound}>

Needed to be:

<NotFoundBoundary
                                                    render={() => (
                                                        <NotFound />
                                                    )}
                                                >

@DB-Alex DB-Alex closed this as completed Sep 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants