add more meaningful 404 error screens

This commit is contained in:
Keith Grant
2019-08-07 16:04:39 -07:00
parent 256fc74676
commit fe8df27811
7 changed files with 139 additions and 37 deletions

View File

@@ -9,6 +9,7 @@ import {
EmptyStateBody, EmptyStateBody,
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import { ExclamationTriangleIcon } from '@patternfly/react-icons'; import { ExclamationTriangleIcon } from '@patternfly/react-icons';
import { RootAPI } from '@api';
import ErrorDetail from '@components/ErrorDetail'; import ErrorDetail from '@components/ErrorDetail';
import NotFoundError from './NotFoundError'; import NotFoundError from './NotFoundError';
@@ -16,23 +17,33 @@ const EmptyState = styled(PFEmptyState)`
width: var(--pf-c-empty-state--m-lg--MaxWidth); width: var(--pf-c-empty-state--m-lg--MaxWidth);
`; `;
async function logout() {
await RootAPI.logout();
window.location.replace('/#/login');
}
class ContentError extends React.Component { class ContentError extends React.Component {
render() { render() {
const { error, i18n } = this.props; const { error, children, i18n } = this.props;
if (error && error.response && error.response.status === 401) { if (error && error.response && error.response.status === 401) {
// TODO: check for session timeout & redirect to /login if (!error.response.headers['session-timeout']) {
logout();
return null;
}
} }
if (error && error.response && error.response.status === 404) { if (error && error.response && error.response.status === 404) {
return <NotFoundError error={error} />; return <NotFoundError error={error}>{children}</NotFoundError>;
} }
return ( return (
<EmptyState> <EmptyState>
<EmptyStateIcon icon={ExclamationTriangleIcon} /> <EmptyStateIcon icon={ExclamationTriangleIcon} />
<Title size="lg">{i18n._(t`Something went wrong...`)}</Title> <Title size="lg">{i18n._(t`Something went wrong...`)}</Title>
<EmptyStateBody> <EmptyStateBody>
{i18n._( {children ||
t`There was an error loading this content. Please reload the page.` i18n._(
)} t`There was an error loading this content. Please reload the page.`
)}
</EmptyStateBody> </EmptyStateBody>
{error && <ErrorDetail error={error} />} {error && <ErrorDetail error={error} />}
</EmptyState> </EmptyState>

View File

@@ -15,15 +15,13 @@ const EmptyState = styled(PFEmptyState)`
width: var(--pf-c-empty-state--m-lg--MaxWidth); width: var(--pf-c-empty-state--m-lg--MaxWidth);
`; `;
function NotFoundError ({ i18n, error }) { function NotFoundError({ i18n, error, children }) {
return ( return (
<EmptyState> <EmptyState>
<EmptyStateIcon icon={ExclamationTriangleIcon} /> <EmptyStateIcon icon={ExclamationTriangleIcon} />
<Title size="lg"> <Title size="lg">{i18n._(t`Not Found`)}</Title>
{i18n._(t`Not Found`)}
</Title>
<EmptyStateBody> <EmptyStateBody>
{i18n._(`The page you requested could not be found.`)} {children || i18n._(`The page you requested could not be found.`)}
</EmptyStateBody> </EmptyStateBody>
{error && <ErrorDetail error={error} />} {error && <ErrorDetail error={error} />}
</EmptyState> </EmptyState>

View File

@@ -1,5 +1,5 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Route, withRouter, Switch, Redirect } from 'react-router-dom'; import { Route, withRouter, Switch, Redirect, Link } from 'react-router-dom';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import styled from 'styled-components'; import styled from 'styled-components';
@@ -9,7 +9,7 @@ import {
PageSection, PageSection,
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import { JobsAPI } from '@api'; import { JobsAPI } from '@api';
import ContentError from '@components/ContentError'; import ContentError, { NotFoundError } from '@components/ContentError';
import CardCloseButton from '@components/CardCloseButton'; import CardCloseButton from '@components/CardCloseButton';
import RoutedTabs from '@components/RoutedTabs'; import RoutedTabs from '@components/RoutedTabs';
@@ -99,7 +99,14 @@ class Job extends Component {
return ( return (
<PageSection> <PageSection>
<Card className="awx-c-card"> <Card className="awx-c-card">
<ContentError error={contentError} /> <ContentError error={contentError}>
{contentError.response.status === 404 && (
<span>
{i18n._(`The page you requested could not be found.`)}{' '}
<Link to="/jobs">{i18n._(`View all Jobs.`)}</Link>
</span>
)}
</ContentError>
</Card> </Card>
</PageSection> </PageSection>
); );
@@ -139,6 +146,20 @@ class Job extends Component {
path="/jobs/:type/:id/output" path="/jobs/:type/:id/output"
render={() => <JobOutput type={match.params.type} job={job} />} render={() => <JobOutput type={match.params.type} job={job} />}
/>, />,
<Route
key="not-found"
path="*"
render={() => (
<NotFoundError>
{i18n._(`The page you requested could not be found.`)}{' '}
<Link
to={`/jobs/${match.params.type}/${match.params.id}/details`}
>
{i18n._(`View Job Details`)}
</Link>
</NotFoundError>
)}
/>,
]} ]}
</Switch> </Switch>
</Card> </Card>

View File

@@ -1,8 +1,12 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Redirect } from 'react-router-dom'; import { Redirect, Link } from 'react-router-dom';
import { PageSection, Card } from '@patternfly/react-core';
import { UnifiedJobsAPI } from '@api'; import { UnifiedJobsAPI } from '@api';
import ContentError, { NotFoundError } from '@components/ContentError';
import { JOB_TYPE_URL_SEGMENTS } from '../../constants'; import { JOB_TYPE_URL_SEGMENTS } from '../../constants';
const NOT_FOUND = 'not found';
class JobTypeRedirect extends Component { class JobTypeRedirect extends Component {
static defaultProps = { static defaultProps = {
view: 'details', view: 'details',
@@ -12,8 +16,9 @@ class JobTypeRedirect extends Component {
super(props); super(props);
this.state = { this.state = {
hasError: false, error: null,
job: null, job: null,
isLoading: true,
}; };
this.loadJob = this.loadJob.bind(this); this.loadJob = this.loadJob.bind(this);
} }
@@ -24,24 +29,44 @@ class JobTypeRedirect extends Component {
async loadJob() { async loadJob() {
const { id } = this.props; const { id } = this.props;
this.setState({ isLoading: true });
try { try {
const { data } = await UnifiedJobsAPI.read({ id }); const { data } = await UnifiedJobsAPI.read({ id });
const job = data.results[0];
this.setState({ this.setState({
job: data.results[0], job,
isLoading: false,
error: job ? null : NOT_FOUND,
});
} catch (error) {
this.setState({
error,
isLoading: false,
}); });
} catch (err) {
this.setState({ hasError: true });
} }
} }
render() { render() {
const { path, view } = this.props; const { path, view } = this.props;
const { hasError, job } = this.state; const { error, job, isLoading } = this.state;
if (hasError) { if (error) {
return <div>Error</div>; return (
<PageSection>
<Card className="awx-c-card">
{error === NOT_FOUND ? (
<NotFoundError>
The requested job could not be found. <Link to="/jobs">View all Jobs.</Link>
</NotFoundError>
) : (
<ContentError error={error} />
)}
</Card>
</PageSection>
);
} }
if (!job) { if (isLoading) {
// TODO show loading state
return <div>Loading...</div>; return <div>Loading...</div>;
} }
const type = JOB_TYPE_URL_SEGMENTS[job.type]; const type = JOB_TYPE_URL_SEGMENTS[job.type];

View File

@@ -1,7 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Switch, Route, withRouter, Redirect } from 'react-router-dom'; import { Switch, Route, withRouter, Redirect, Link } from 'react-router-dom';
import { import {
Card, Card,
CardHeader as PFCardHeader, CardHeader as PFCardHeader,
@@ -10,7 +10,7 @@ import {
import styled from 'styled-components'; import styled from 'styled-components';
import CardCloseButton from '@components/CardCloseButton'; import CardCloseButton from '@components/CardCloseButton';
import RoutedTabs from '@components/RoutedTabs'; import RoutedTabs from '@components/RoutedTabs';
import ContentError from '@components/ContentError'; import ContentError, { NotFoundError } from '@components/ContentError';
import { OrganizationAccess } from './OrganizationAccess'; import { OrganizationAccess } from './OrganizationAccess';
import OrganizationDetail from './OrganizationDetail'; import OrganizationDetail from './OrganizationDetail';
import OrganizationEdit from './OrganizationEdit'; import OrganizationEdit from './OrganizationEdit';
@@ -168,7 +168,16 @@ class Organization extends Component {
return ( return (
<PageSection> <PageSection>
<Card className="awx-c-card"> <Card className="awx-c-card">
<ContentError error={contentError} /> <ContentError error={contentError}>
{contentError.response.status === 404 && (
<span>
{i18n._(`Organization not found.`)}{' '}
<Link to="/organizations">
{i18n._(`View all Organizations.`)}
</Link>
</span>
)}
</ContentError>
</Card> </Card>
</PageSection> </PageSection>
); );
@@ -226,6 +235,21 @@ class Organization extends Component {
)} )}
/> />
)} )}
<Route
key="not-found"
path="*"
render={() => (
<NotFoundError>
{i18n._(`The page you requested could not be found.`)}{' '}
{match.params.id && (
<Link to={`/organizations/${match.params.id}/details`}>
{i18n._(`View Organization Details`)}
</Link>
)}
</NotFoundError>
)}
/>
,
</Switch> </Switch>
</Card> </Card>
</PageSection> </PageSection>

View File

@@ -2,9 +2,9 @@ import React, { Component } from 'react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { Card, CardHeader, PageSection } from '@patternfly/react-core'; import { Card, CardHeader, PageSection } from '@patternfly/react-core';
import { Switch, Route, Redirect, withRouter } from 'react-router-dom'; import { Switch, Route, Redirect, withRouter, Link } from 'react-router-dom';
import CardCloseButton from '@components/CardCloseButton'; import CardCloseButton from '@components/CardCloseButton';
import ContentError from '@components/ContentError'; import ContentError, { NotFoundError } from '@components/ContentError';
import RoutedTabs from '@components/RoutedTabs'; import RoutedTabs from '@components/RoutedTabs';
import JobTemplateDetail from './JobTemplateDetail'; import JobTemplateDetail from './JobTemplateDetail';
import { JobTemplatesAPI } from '@api'; import { JobTemplatesAPI } from '@api';
@@ -77,7 +77,14 @@ class Template extends Component {
return ( return (
<PageSection> <PageSection>
<Card className="awx-c-card"> <Card className="awx-c-card">
<ContentError error={contentError} /> <ContentError error={contentError}>
{contentError.response.status === 404 && (
<span>
{i18n._(`Template not found.`)}{' '}
<Link to="/templates">{i18n._(`View all Templates.`)}</Link>
</span>
)}
</ContentError>
</Card> </Card>
</PageSection> </PageSection>
); );
@@ -92,8 +99,9 @@ class Template extends Component {
to="/templates/:templateType/:id/details" to="/templates/:templateType/:id/details"
exact exact
/> />
{template && ( {template && [
<Route <Route
key="details"
path="/templates/:templateType/:id/details" path="/templates/:templateType/:id/details"
render={() => ( render={() => (
<JobTemplateDetail <JobTemplateDetail
@@ -102,14 +110,29 @@ class Template extends Component {
template={template} template={template}
/> />
)} )}
/> />,
)}
{template && (
<Route <Route
key="edit"
path="/templates/:templateType/:id/edit" path="/templates/:templateType/:id/edit"
render={() => <JobTemplateEdit template={template} />} render={() => <JobTemplateEdit template={template} />}
/> />,
)} <Route
key="not-found"
path="*"
render={() => (
<NotFoundError>
{i18n._(`The page you requested could not be found.`)}{' '}
{match.params.id && (
<Link
to={`/templates/${match.params.templateType}/${match.params.id}/details`}
>
{i18n._(`View Template Details`)}
</Link>
)}
</NotFoundError>
)}
/>,
]}
</Switch> </Switch>
</Card> </Card>
</PageSection> </PageSection>

View File

@@ -1,11 +1,11 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Route, withRouter, Switch } from 'react-router-dom'; import { Route, withRouter, Switch, Link } from 'react-router-dom';
import { Config } from '@contexts/Config'; import { Config } from '@contexts/Config';
import Breadcrumbs from '@components/Breadcrumbs/Breadcrumbs'; import Breadcrumbs from '@components/Breadcrumbs/Breadcrumbs';
import { NotFoundError } from '@components/ContentError';
import { TemplateList } from './TemplateList'; import { TemplateList } from './TemplateList';
import Template from './Template'; import Template from './Template';
import JobTemplateAdd from './JobTemplateAdd'; import JobTemplateAdd from './JobTemplateAdd';