Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/page-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ Typically, error pages will be the same component as a Not Found or 404 page.

_**Note:** If using a React component for your error page, it can receive the error message thrown by a guard function via an `error` prop._

## Raw errors

By default, only the `message` property of the error thrown by a guard is sent to the Error page (or if no `message` property is available, the string `"Not found."`).

If you want the exact value thrown by your guards available on the `error` prop for your Error page you can set the `rawError` prop to `true`.

It can be set either:

- _globally_ as the `rawError` prop of a [`GuardProvider`](/docs/guard-provider.md)

- _individually_ as the `rawError` prop of a [`GuardedRoute`](/docs/guarded-route.md)

## Examples

With strings:
Expand Down
21 changes: 14 additions & 7 deletions package/src/Guard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React, { useCallback, useContext, useEffect, useMemo } from 'react';
import { __RouterContext as RouterContext } from 'react-router';
import { matchPath, Redirect, Route } from 'react-router-dom';
import { ErrorPageContext, FromRouteContext, GuardContext, LoadingPageContext } from './contexts';
import {
ErrorPageContext,
FromRouteContext,
GuardContext,
LoadingPageContext,
RawErrorContext,
} from './contexts';
import { usePrevious, useStateRef, useStateWhenMounted } from './hooks';
import renderPage from './renderPage';
import {
Expand Down Expand Up @@ -36,6 +42,7 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
const guards = useContext(GuardContext);
const LoadingPage = useContext(LoadingPageContext);
const ErrorPage = useContext(ErrorPageContext);
const useRawErrors = useContext(RawErrorContext);

const hasGuards = useMemo(() => !!(guards && guards.length > 0), [guards]);
const [validationsRequested, setValidationsRequested] = useStateRef<number>(0);
Expand Down Expand Up @@ -122,7 +129,7 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
* Validates the route using the guards. If an error occurs, it
* will toggle the route error state.
*/
const validateRoute = async (): Promise<void> => {
const validateRoute = async (useRawErrors: boolean | null | undefined): Promise<void> => {
const currentRequests = validationsRequested.current;

let pageProps: PageProps = {};
Expand All @@ -134,7 +141,7 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
pageProps = props;
routeRedirect = redirect;
} catch (error) {
routeError = error.message || 'Not found.';
routeError = useRawErrors ? error : error.message || 'Not found.';
}

if (currentRequests === getValidationsRequested()) {
Expand All @@ -146,8 +153,8 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
};

useEffect(() => {
validateRoute();
}, []);
validateRoute(useRawErrors);
}, [useRawErrors]);

useEffect(() => {
if (hasPathChanged) {
Expand All @@ -156,10 +163,10 @@ const Guard: React.FunctionComponent<GuardProps> = ({ children, component, meta,
setRouteRedirect(null);
setRouteValidated(!hasGuards);
if (hasGuards) {
validateRoute();
validateRoute(useRawErrors);
}
}
}, [hasPathChanged]);
}, [hasPathChanged, useRawErrors]);

if (hasPathChanged) {
if (hasGuards) {
Expand Down
14 changes: 12 additions & 2 deletions package/src/GuardProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React, { useContext } from 'react';
import { __RouterContext as RouterContext } from 'react-router';
import invariant from 'tiny-invariant';
import { ErrorPageContext, FromRouteContext, GuardContext, LoadingPageContext } from './contexts';
import {
ErrorPageContext,
FromRouteContext,
GuardContext,
LoadingPageContext,
RawErrorContext,
} from './contexts';
import { useGlobalGuards, usePrevious } from './hooks';
import { GuardProviderProps } from './types';

Expand All @@ -11,6 +17,7 @@ const GuardProvider: React.FunctionComponent<GuardProviderProps> = ({
ignoreGlobal,
loading,
error,
rawError,
}) => {
const routerContext = useContext(RouterContext);
invariant(!!routerContext, 'You should not use <GuardProvider> outside a <Router>');
Expand All @@ -20,12 +27,15 @@ const GuardProvider: React.FunctionComponent<GuardProviderProps> = ({

const loadingPage = useContext(LoadingPageContext);
const errorPage = useContext(ErrorPageContext);
const useRawErrors = useContext(RawErrorContext);

return (
<GuardContext.Provider value={providerGuards}>
<LoadingPageContext.Provider value={loading || loadingPage}>
<ErrorPageContext.Provider value={error || errorPage}>
<FromRouteContext.Provider value={from}>{children}</FromRouteContext.Provider>
<RawErrorContext.Provider value={rawError || useRawErrors}>
<FromRouteContext.Provider value={from}>{children}</FromRouteContext.Provider>
</RawErrorContext.Provider>
</ErrorPageContext.Provider>
</LoadingPageContext.Provider>
</GuardContext.Provider>
Expand Down
13 changes: 9 additions & 4 deletions package/src/GuardedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { Route } from 'react-router-dom';
import invariant from 'tiny-invariant';
import ContextWrapper from './ContextWrapper';
import Guard from './Guard';
import { ErrorPageContext, GuardContext, LoadingPageContext } from './contexts';
import { ErrorPageContext, GuardContext, LoadingPageContext, RawErrorContext } from './contexts';
import { useGlobalGuards } from './hooks';
import { GuardedRouteProps, PageComponent } from './types';

const GuardedRoute: React.FunctionComponent<GuardedRouteProps> = ({
children,
component,
error,
rawError,
guards,
ignoreGlobal,
loading,
Expand All @@ -32,9 +33,13 @@ const GuardedRoute: React.FunctionComponent<GuardedRouteProps> = ({
<GuardContext.Provider value={routeGuards}>
<ContextWrapper<PageComponent> context={LoadingPageContext} value={loading}>
<ContextWrapper<PageComponent> context={ErrorPageContext} value={error}>
<Guard name={path} component={component} meta={meta} render={render}>
{children}
</Guard>
<ContextWrapper<boolean | null | undefined>
context={RawErrorContext}
value={rawError}>
<Guard name={path} component={component} meta={meta} render={render}>
{children}
</Guard>
</ContextWrapper>
</ContextWrapper>
</ContextWrapper>
</GuardContext.Provider>
Expand Down
2 changes: 2 additions & 0 deletions package/src/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const FromRouteContext = createContext<RouteComponentProps | null>(null);
export const GuardContext = createContext<GuardFunction[] | null>(null);

export const LoadingPageContext = createContext<PageComponent>(null);

export const RawErrorContext = createContext<boolean | null | undefined>(false);
1 change: 1 addition & 0 deletions package/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface BaseGuardProps {
ignoreGlobal?: boolean;
loading?: PageComponent;
error?: PageComponent;
rawError?: boolean;
}

export type PropsWithMeta<T> = T & {
Expand Down