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

feat: error boundary in layout to handle application errors / crashes #1108

Merged
1 change: 1 addition & 0 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"isomorphic-dompurify": "^2.14.0",
"marked": "^4.0.14",
"openapi-sampler": "^1.2.1",
"react-error-boundary": "^4.1.2",
"use-resize-observer": "^9.1.0"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { ReactNode } from 'react';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
import { ErrorObject } from '../../types';
import { Error } from '../Error/Error';

interface Props {
children: ReactNode;
}

function fallbackRender({ error }: FallbackProps) {
const ErrorObject: ErrorObject = {
title: 'Something went wrong',
type: 'application-error',
validationErrors: [
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
title: error?.message,
},
],
};
return <Error error={ErrorObject} />;
}

const AsyncApiErrorBoundary = ({ children }: Props) => {
return (
<ErrorBoundary fallbackRender={fallbackRender}>{children}</ErrorBoundary>
);
};

export default AsyncApiErrorBoundary;
35 changes: 19 additions & 16 deletions library/src/containers/AsyncApi/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Error } from '../Error/Error';
import { ConfigInterface } from '../../config';
import { SpecificationContext, ConfigContext } from '../../contexts';
import { ErrorObject } from '../../types';
import AsyncApiErrorBoundary from '../ApplicationErrorHandler/ErrorBoundary';

interface Props {
asyncapi: AsyncAPIDocumentInterface;
Expand Down Expand Up @@ -48,24 +49,26 @@ const AsyncApiLayout: React.FunctionComponent<Props> = ({
<ConfigContext.Provider value={config}>
<SpecificationContext.Provider value={asyncapi}>
<section className="aui-root">
<div
className={`${observerClassName} relative md:flex bg-white leading-normal`}
id={config.schemaID ?? undefined}
ref={ref}
>
{configShow.sidebar && <Sidebar />}
<div className="panel--center relative py-8 flex-1">
<div className="relative z-10">
{configShow.errors && error && <Error error={error} />}
{configShow.info && <Info />}
{configShow.servers && <Servers />}
{configShow.operations && <Operations />}
{configShow.messages && <Messages />}
{configShow.schemas && <Schemas />}
<AsyncApiErrorBoundary>
<div
className={`${observerClassName} relative md:flex bg-white leading-normal`}
id={config.schemaID ?? undefined}
ref={ref}
>
{configShow.sidebar && <Sidebar />}
<div className="panel--center relative py-8 flex-1">
<div className="relative z-10">
{configShow.errors && error && <Error error={error} />}
catosaurusrex2003 marked this conversation as resolved.
Show resolved Hide resolved
{configShow.info && <Info />}
{configShow.servers && <Servers />}
{configShow.operations && <Operations />}
{configShow.messages && <Messages />}
{configShow.schemas && <Schemas />}
</div>
<div className="panel--right absolute top-0 right-0 h-full bg-gray-800" />
</div>
<div className="panel--right absolute top-0 right-0 h-full bg-gray-800" />
</div>
</div>
</AsyncApiErrorBoundary>
</section>
</SpecificationContext.Provider>
</ConfigContext.Provider>
Expand Down
5 changes: 4 additions & 1 deletion library/src/containers/Error/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const renderErrors = (errors: ValidationError[]): React.ReactNode => {
}
return (
<div key={index} className="flex gap-2">
<span>{`line ${singleError?.location?.startLine + singleError?.location?.startOffset}:`}</span>
{(singleError?.location?.startLine ??
singleError?.location?.startOffset) && (
<span>{`line ${singleError?.location?.startLine + singleError?.location?.startOffset}:`}</span>
)}
<code className="whitespace-pre-wrap break-all ml-2">
{singleError.title}
</code>
Expand Down
2 changes: 1 addition & 1 deletion library/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface MessageExample {

export interface ValidationError {
title: string;
location: {
location?: {
jsonPointer: string;
startLine: number;
startColumn: number;
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading