apps/web: add utils/error

This commit is contained in:
vchikalkin 2023-11-14 14:03:38 +03:00
parent 859cf7b2b9
commit bb29d91b17
3 changed files with 55 additions and 36 deletions

View File

@ -2,6 +2,7 @@
import * as apiIUS from '@/api/ius/query';
import { Conditions } from '@/components/Conditions';
import type { PageProps } from '@/types/page';
import { withError } from '@/utils/error';
import { makeCreateUrl } from '@/utils/url';
import type { Metadata } from 'next';
import { Background } from 'ui';
@ -22,12 +23,16 @@ export async function generateMetadata({ params, searchParams }: PageProps): Pro
}
export default async function Page({ params, searchParams }: PageProps) {
const createUrl = makeCreateUrl(`/${params.slug}`, searchParams);
const conditions = await apiIUS.getConditions(createUrl);
return withError({
render: async () => {
const createUrl = makeCreateUrl(`/${params.slug}`, searchParams);
const conditions = await apiIUS.getConditions(createUrl);
return (
<Background className="justify-center">
<Conditions html={conditions} />
</Background>
);
return (
<Background className="justify-center">
<Conditions html={conditions} />
</Background>
);
},
});
}

View File

@ -1,11 +1,10 @@
import * as apiIUS from '@/api/ius/query';
import type * as t from '@/api/ius/types';
import * as Form from '@/components/Form';
import type { PageProps } from '@/types/page';
import { withError } from '@/utils/error';
import { makeCreateUrl } from '@/utils/url';
import type { Metadata } from 'next';
import { Background, Divider, HttpError } from 'ui';
import type { WretchError } from 'wretch/types';
import { Background, Divider } from 'ui';
export async function generateMetadata({ params, searchParams }: PageProps): Promise<Metadata> {
const createUrl = makeCreateUrl(`/${params.slug}`, searchParams);
@ -23,33 +22,24 @@ export async function generateMetadata({ params, searchParams }: PageProps): Pro
}
export default async function Page({ params, searchParams }: PageProps) {
try {
const createUrl = makeCreateUrl(`/${params.slug}`, searchParams);
return withError({
render: async () => {
const createUrl = makeCreateUrl(`/${params.slug}`, searchParams);
const data = await apiIUS.getData(createUrl);
const metaData = await apiIUS.getMetaData(createUrl);
const { title } = await apiIUS.getConfig(createUrl);
const data = await apiIUS.getData(createUrl);
const metaData = await apiIUS.getMetaData(createUrl);
const { title } = await apiIUS.getConfig(createUrl);
const props = { data, metaData, title };
const props = { data, metaData, title };
return (
<Background>
<Form.Header {...props} url={'/ius' + createUrl('/conditions')} />
<Form.Elements {...props} />
<Divider />
<Form.Buttons />
</Background>
);
} catch (error) {
const _error = error as WretchError;
const json = _error.json as t.HttpError;
return (
<HttpError
code={_error.status.toString()}
title={json?.title || _error.name}
description={json?.errors?.join('') || _error.message || _error.text}
/>
);
}
return (
<Background>
<Form.Header {...props} url={'/ius' + createUrl('/conditions')} />
<Form.Elements {...props} />
<Divider />
<Form.Buttons />
</Background>
);
},
});
}

24
apps/web/utils/error.tsx Normal file
View File

@ -0,0 +1,24 @@
import type * as t from '@/api/ius/types';
import { HttpError } from 'ui';
import type { WretchError } from 'wretch/types';
type Props = {
render: () => Promise<JSX.Element>;
};
export async function withError({ render }: Props) {
try {
return await render();
} catch (error) {
const _error = error as WretchError;
const json = _error.json as t.HttpError;
return (
<HttpError
code={_error.status.toString()}
title={json?.title || _error.name}
description={json?.errors?.join('') || _error.message || _error.text}
/>
);
}
}