This commit is contained in:
@@ -11,7 +11,7 @@ interface PageRouteProps {
|
|||||||
} | undefined>
|
} | undefined>
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function parseBlogParams({ params: paramsPromise, searchParams: searchParamsPromise }: PageRouteProps) {
|
async function parseBlogParams({ params: paramsPromise, searchParams: searchParamsPromise }: PageRouteProps) {
|
||||||
const params = await paramsPromise ?? {};
|
const params = await paramsPromise ?? {};
|
||||||
const searchParams = await searchParamsPromise ?? {};
|
const searchParams = await searchParamsPromise ?? {};
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ export async function parseBlogParams({ params: paramsPromise, searchParams: sea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBlog(paramsResult: ReturnType<typeof parseBlogParams>) {
|
async function getBlog(paramsResult: ReturnType<typeof parseBlogParams>) {
|
||||||
const { errorMsg, id, p } = await paramsResult;
|
const { errorMsg, id, p } = await paramsResult;
|
||||||
if (errorMsg) {
|
if (errorMsg) {
|
||||||
return {
|
return {
|
||||||
@@ -78,7 +78,9 @@ export async function generateMetadata({ params, searchParams }: PageRouteProps)
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function Page({ params, searchParams }: PageRouteProps) {
|
export default async function Page({ params, searchParams }: PageRouteProps) {
|
||||||
let { errorMsg, id, p } = await parseBlogParams({ params, searchParams });
|
const res = await parseBlogParams({ params, searchParams });
|
||||||
|
const { id, p } = res;
|
||||||
|
let { errorMsg } = res;
|
||||||
|
|
||||||
const data = errorMsg ? null
|
const data = errorMsg ? null
|
||||||
: await BlogAPI.getBlog(`${id}`, p).catch(e => handleAPIError(e, ({ message }) => { errorMsg = message; return null }));
|
: await BlogAPI.getBlog(`${id}`, p).catch(e => handleAPIError(e, ({ message }) => { errorMsg = message; return null }));
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export function normalizeAPIError(error: unknown): never {
|
|||||||
throw new APIError((error instanceof Error ? `${error.message}` : '') || '未知错误', 400);
|
throw new APIError((error instanceof Error ? `${error.message}` : '') || '未知错误', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleAPIError<T extends unknown>(error: unknown, handler: (e: APIError) => T): T {
|
export function handleAPIError<T>(error: unknown, handler: (e: APIError) => T): T {
|
||||||
if (error instanceof APIError) {
|
if (error instanceof APIError) {
|
||||||
return handler(error);
|
return handler(error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user