From bb29d91b17e734df88cb058c6efe3164985482db Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Tue, 14 Nov 2023 14:03:38 +0300 Subject: [PATCH] apps/web: add utils/error --- apps/web/app/ius/[slug]/conditions/page.tsx | 19 +++++--- apps/web/app/ius/[slug]/page.tsx | 48 ++++++++------------- apps/web/utils/error.tsx | 24 +++++++++++ 3 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 apps/web/utils/error.tsx diff --git a/apps/web/app/ius/[slug]/conditions/page.tsx b/apps/web/app/ius/[slug]/conditions/page.tsx index e5622bf..f5498ff 100644 --- a/apps/web/app/ius/[slug]/conditions/page.tsx +++ b/apps/web/app/ius/[slug]/conditions/page.tsx @@ -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 ( - - - - ); + return ( + + + + ); + }, + }); } diff --git a/apps/web/app/ius/[slug]/page.tsx b/apps/web/app/ius/[slug]/page.tsx index 7c79372..9f1c9aa 100644 --- a/apps/web/app/ius/[slug]/page.tsx +++ b/apps/web/app/ius/[slug]/page.tsx @@ -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 { 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 ( - - - - - - - ); - } catch (error) { - const _error = error as WretchError; - const json = _error.json as t.HttpError; - - return ( - - ); - } + return ( + + + + + + + ); + }, + }); } diff --git a/apps/web/utils/error.tsx b/apps/web/utils/error.tsx new file mode 100644 index 0000000..c12f593 --- /dev/null +++ b/apps/web/utils/error.tsx @@ -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; +}; + +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 ( + + ); + } +}