Skip to content

Commit cfd3cbc

Browse files
committed
fix: note password access
Signed-off-by: Innei <i@innei.in>
1 parent 9d0ff89 commit cfd3cbc

11 files changed

Lines changed: 118 additions & 122 deletions

File tree

src/app/(app)/error.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { useEffect } from 'react'
44

55
// import { captureException } from '@sentry/nextjs'
66

7-
import { NotFound404 } from '~/components/common/404'
87
import { NormalContainer } from '~/components/layout/container/Normal'
98
import { StyledButton } from '~/components/ui/button'
10-
import { isRequestError, pickStatusCode } from '~/lib/is-error'
119

1210
// eslint-disable-next-line react/display-name
1311
export default ({ error, reset }: any) => {
@@ -16,21 +14,13 @@ export default ({ error, reset }: any) => {
1614
// captureException(error)
1715
}, [error])
1816

19-
if (isRequestError(error) && pickStatusCode(error) === 404) {
20-
return (
21-
<div className="flex min-h-[calc(100vh-10rem)] flex-col center">
22-
<NotFound404 />
23-
</div>
24-
)
25-
}
26-
2717
return (
2818
<NormalContainer>
2919
<div className="flex min-h-[calc(100vh-10rem)] flex-col center">
3020
<h2 className="mb-5">
3121
<p>
3222
服务端渲染页面时出现了错误,可能是 Next.js 服务访问 API
33-
超时。请刷新重试。
23+
数据发生异常。请刷新重试。
3424
</p>
3525
<p>
3626
多次出现错误请联系开发者 <a href="mailto:i@innei.in">Innei</a>

src/app/(app)/notes/[id]/layout.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
import { Suspense } from 'react'
33
import type { Metadata } from 'next'
44

5+
import { BizErrorPage } from '~/components/common/BizErrorPage'
56
import { buildRoomName, RoomProvider } from '~/components/modules/activity'
67
import { CommentAreaRootLazy } from '~/components/modules/comment'
8+
import { NotePasswordForm } from '~/components/modules/note'
79
import { NoteFontSettingFab } from '~/components/modules/note/NoteFontFab'
810
import { NoteMainContainer } from '~/components/modules/note/NoteMainContainer'
911
import { TocFAB } from '~/components/modules/toc/TocFAB'
1012
import { BottomToUpSoftScaleTransitionView } from '~/components/ui/transition'
1113
import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
1214
import { getOgUrl } from '~/lib/helper.server'
1315
import { getSummaryFromMd } from '~/lib/markdown'
16+
import { unwrapRequest } from '~/lib/request.server'
1417
import {
1518
CurrentNoteDataProvider,
1619
SyncNoteDataAfterLoggedIn,
@@ -30,7 +33,9 @@ export const generateMetadata = async ({
3033
}
3134
}): Promise<Metadata> => {
3235
try {
33-
const data = (await getData(params)).data
36+
const res = await getData(params)
37+
38+
const data = res.data
3439
const { title, text } = data
3540
const description = getSummaryFromMd(text ?? '')
3641

@@ -66,7 +71,25 @@ export default async (
6671
) => {
6772
const { params } = props
6873
const { id: nid } = params
69-
const data = await getData(params)
74+
const { data, error, status, bizMessage } = await unwrapRequest(
75+
getData(params),
76+
)
77+
78+
if (status === 403) {
79+
return (
80+
<Paper>
81+
<NotePasswordForm />
82+
<CurrentNoteNidProvider nid={nid} />
83+
</Paper>
84+
)
85+
}
86+
87+
if (error) {
88+
if (bizMessage) {
89+
return <BizErrorPage status={status} bizMessage={bizMessage} />
90+
}
91+
throw error
92+
}
7093

7194
const { id: noteObjectId, allowComment } = data.data
7295

src/app/(app)/notes/error.tsx

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/components/common/404.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* eslint-disable react/no-unknown-property */
22
export const NotFound404 = () => {
33
return (
4-
<div className="absolute inset-0 flex flex-col space-y-6 center">
5-
<$404SVG className="size-[400px]" />
6-
<p>这颗星球还没有知识哦,去其他地方探索吧</p>
4+
<div className="min-h-[500px]">
5+
<div className="fixed inset-0 flex flex-col space-y-6 center">
6+
<$404SVG className="size-[400px]" />
7+
<p>这颗星球还没有知识哦,去其他地方探索吧</p>
8+
</div>
79
</div>
810
)
911
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { FC } from 'react'
2+
3+
export const BizErrorPage: FC<{
4+
bizMessage: string
5+
status: number
6+
}> = ({ bizMessage, status }) => {
7+
return (
8+
<div className="flex min-h-[calc(100vh-10rem)] flex-col center">
9+
<h2 className="mb-5 flex flex-col gap-2 text-center">
10+
<p>数据接口请求出现错误</p>
11+
<p>
12+
HTTP Status: <strong>{status}</strong>
13+
</p>
14+
<p>
15+
错误信息:<strong>{bizMessage}</strong>
16+
</p>
17+
</h2>
18+
</div>
19+
)
20+
}

src/components/modules/comment/CommentBox/hooks.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import type React from 'react'
1717
import type { createInitialValue } from './providers'
1818

1919
import { useIsLogged } from '~/atoms/hooks'
20-
import { apiClient, getErrorMessageFromRequestError } from '~/lib/request'
20+
import { apiClient } from '~/lib/request'
21+
import { getErrorMessageFromRequestError } from '~/lib/request.shared'
2122
import { jotaiStore } from '~/lib/store'
2223
import { toast } from '~/lib/toast'
2324

src/constants/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { env } from 'next-runtime-env'
33
import { isClientSide, isDev } from '~/lib/env'
44

55
export const API_URL: string = (() => {
6-
if (isDev) return env('NEXT_PUBLIC_API_URL')
6+
if (isDev) return env('NEXT_PUBLIC_API_URL') || ''
77

88
if (isClientSide && env('NEXT_PUBLIC_CLIENT_API_URL')) {
9-
return env('NEXT_PUBLIC_CLIENT_API_URL')
9+
return env('NEXT_PUBLIC_CLIENT_API_URL') || ''
1010
}
1111

1212
return env('NEXT_PUBLIC_API_URL') || '/api/v2'

src/lib/is-error.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/lib/request.server.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { notFound } from 'next/navigation'
44

55
import { RequestError } from '@mx-space/api-client'
66

7+
import { getErrorMessageFromRequestError } from './request.shared'
8+
79
export const requestErrorHandler = (error: Error | RequestError) => {
810
if (
911
error instanceof RequestError &&
@@ -13,3 +15,46 @@ export const requestErrorHandler = (error: Error | RequestError) => {
1315
}
1416
throw error
1517
}
18+
19+
export async function unwrapRequest<T>(requestPromise: Promise<T>): Promise<{
20+
data: T
21+
status: number
22+
error: null
23+
bizMessage?: undefined
24+
}>
25+
// @ts-expect-error
26+
export async function unwrapRequest<T>(requestPromise: Promise<T>): Promise<{
27+
data: null
28+
status: number
29+
error: Error
30+
bizMessage: string
31+
}>
32+
33+
export async function unwrapRequest<T>(requestPromise: Promise<T>) {
34+
try {
35+
const data = await requestPromise
36+
return {
37+
status: 200,
38+
data,
39+
error: null,
40+
}
41+
} catch (error) {
42+
if (error instanceof RequestError) {
43+
if (error.status === 404) {
44+
notFound()
45+
}
46+
47+
return {
48+
status: error.status || 500,
49+
error,
50+
data: null,
51+
bizMessage: getErrorMessageFromRequestError(error),
52+
}
53+
}
54+
return {
55+
status: 500,
56+
error: 'Internal Server Error',
57+
data: null,
58+
}
59+
}
60+
}

src/lib/request.shared.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { FetchError } from 'ofetch'
2+
3+
import { RequestError } from '@mx-space/api-client'
4+
5+
export const getErrorMessageFromRequestError = (error: RequestError) => {
6+
if (!(error instanceof RequestError)) return (error as Error).message
7+
const fetchError = error.raw as FetchError
8+
const messagesOrMessage = fetchError.response?._data?.message
9+
const bizMessage =
10+
typeof messagesOrMessage === 'string'
11+
? messagesOrMessage
12+
: Array.isArray(messagesOrMessage)
13+
? messagesOrMessage[0]
14+
: undefined
15+
16+
return bizMessage || fetchError.message
17+
}

0 commit comments

Comments
 (0)