Skip to content

Commit

Permalink
fix: note password access
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Apr 10, 2024
1 parent 9d0ff89 commit cfd3cbc
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 122 deletions.
12 changes: 1 addition & 11 deletions src/app/(app)/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { useEffect } from 'react'

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

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

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

if (isRequestError(error) && pickStatusCode(error) === 404) {
return (
<div className="flex min-h-[calc(100vh-10rem)] flex-col center">
<NotFound404 />
</div>
)
}

return (
<NormalContainer>
<div className="flex min-h-[calc(100vh-10rem)] flex-col center">
<h2 className="mb-5">
<p>
服务端渲染页面时出现了错误,可能是 Next.js 服务访问 API
超时。请刷新重试。
数据发生异常。请刷新重试。
</p>
<p>
多次出现错误请联系开发者 <a href="mailto:[email protected]">Innei</a>
Expand Down
27 changes: 25 additions & 2 deletions src/app/(app)/notes/[id]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
import { Suspense } from 'react'
import type { Metadata } from 'next'

import { BizErrorPage } from '~/components/common/BizErrorPage'
import { buildRoomName, RoomProvider } from '~/components/modules/activity'
import { CommentAreaRootLazy } from '~/components/modules/comment'
import { NotePasswordForm } from '~/components/modules/note'
import { NoteFontSettingFab } from '~/components/modules/note/NoteFontFab'
import { NoteMainContainer } from '~/components/modules/note/NoteMainContainer'
import { TocFAB } from '~/components/modules/toc/TocFAB'
import { BottomToUpSoftScaleTransitionView } from '~/components/ui/transition'
import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
import { getOgUrl } from '~/lib/helper.server'
import { getSummaryFromMd } from '~/lib/markdown'
import { unwrapRequest } from '~/lib/request.server'
import {
CurrentNoteDataProvider,
SyncNoteDataAfterLoggedIn,
Expand All @@ -30,7 +33,9 @@ export const generateMetadata = async ({
}
}): Promise<Metadata> => {
try {
const data = (await getData(params)).data
const res = await getData(params)

const data = res.data
const { title, text } = data
const description = getSummaryFromMd(text ?? '')

Expand Down Expand Up @@ -66,7 +71,25 @@ export default async (
) => {
const { params } = props
const { id: nid } = params
const data = await getData(params)
const { data, error, status, bizMessage } = await unwrapRequest(
getData(params),
)

if (status === 403) {
return (
<Paper>
<NotePasswordForm />
<CurrentNoteNidProvider nid={nid} />
</Paper>
)
}

if (error) {
if (bizMessage) {
return <BizErrorPage status={status} bizMessage={bizMessage} />
}
throw error
}

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

Expand Down
74 changes: 0 additions & 74 deletions src/app/(app)/notes/error.tsx

This file was deleted.

8 changes: 5 additions & 3 deletions src/components/common/404.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable react/no-unknown-property */
export const NotFound404 = () => {
return (
<div className="absolute inset-0 flex flex-col space-y-6 center">
<$404SVG className="size-[400px]" />
<p>这颗星球还没有知识哦,去其他地方探索吧</p>
<div className="min-h-[500px]">
<div className="fixed inset-0 flex flex-col space-y-6 center">
<$404SVG className="size-[400px]" />
<p>这颗星球还没有知识哦,去其他地方探索吧</p>
</div>
</div>
)
}
Expand Down
20 changes: 20 additions & 0 deletions src/components/common/BizErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { FC } from 'react'

export const BizErrorPage: FC<{
bizMessage: string
status: number
}> = ({ bizMessage, status }) => {
return (
<div className="flex min-h-[calc(100vh-10rem)] flex-col center">
<h2 className="mb-5 flex flex-col gap-2 text-center">
<p>数据接口请求出现错误</p>
<p>
HTTP Status: <strong>{status}</strong>
</p>
<p>
错误信息:<strong>{bizMessage}</strong>
</p>
</h2>
</div>
)
}
3 changes: 2 additions & 1 deletion src/components/modules/comment/CommentBox/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import type React from 'react'
import type { createInitialValue } from './providers'

import { useIsLogged } from '~/atoms/hooks'
import { apiClient, getErrorMessageFromRequestError } from '~/lib/request'
import { apiClient } from '~/lib/request'
import { getErrorMessageFromRequestError } from '~/lib/request.shared'
import { jotaiStore } from '~/lib/store'
import { toast } from '~/lib/toast'

Expand Down
4 changes: 2 additions & 2 deletions src/constants/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { env } from 'next-runtime-env'
import { isClientSide, isDev } from '~/lib/env'

export const API_URL: string = (() => {
if (isDev) return env('NEXT_PUBLIC_API_URL')
if (isDev) return env('NEXT_PUBLIC_API_URL') || ''

if (isClientSide && env('NEXT_PUBLIC_CLIENT_API_URL')) {
return env('NEXT_PUBLIC_CLIENT_API_URL')
return env('NEXT_PUBLIC_CLIENT_API_URL') || ''
}

return env('NEXT_PUBLIC_API_URL') || '/api/v2'
Expand Down
9 changes: 0 additions & 9 deletions src/lib/is-error.ts

This file was deleted.

45 changes: 45 additions & 0 deletions src/lib/request.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { notFound } from 'next/navigation'

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

import { getErrorMessageFromRequestError } from './request.shared'

export const requestErrorHandler = (error: Error | RequestError) => {
if (
error instanceof RequestError &&
Expand All @@ -13,3 +15,46 @@ export const requestErrorHandler = (error: Error | RequestError) => {
}
throw error
}

export async function unwrapRequest<T>(requestPromise: Promise<T>): Promise<{
data: T
status: number
error: null
bizMessage?: undefined
}>
// @ts-expect-error
export async function unwrapRequest<T>(requestPromise: Promise<T>): Promise<{
data: null
status: number
error: Error
bizMessage: string
}>

export async function unwrapRequest<T>(requestPromise: Promise<T>) {
try {
const data = await requestPromise
return {
status: 200,
data,
error: null,
}
} catch (error) {
if (error instanceof RequestError) {
if (error.status === 404) {
notFound()
}

return {
status: error.status || 500,
error,
data: null,
bizMessage: getErrorMessageFromRequestError(error),
}
}
return {
status: 500,
error: 'Internal Server Error',
data: null,
}
}
}
17 changes: 17 additions & 0 deletions src/lib/request.shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { FetchError } from 'ofetch'

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

export const getErrorMessageFromRequestError = (error: RequestError) => {
if (!(error instanceof RequestError)) return (error as Error).message
const fetchError = error.raw as FetchError
const messagesOrMessage = fetchError.response?._data?.message
const bizMessage =
typeof messagesOrMessage === 'string'
? messagesOrMessage
: Array.isArray(messagesOrMessage)
? messagesOrMessage[0]
: undefined

return bizMessage || fetchError.message
}
21 changes: 1 addition & 20 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { nanoid } from 'nanoid'
import { createFetch } from 'ofetch'
import type { IRequestAdapter } from '@mx-space/api-client'
import type { FetchError } from 'ofetch'

import {
allControllers,
createClient,
RequestError,
} from '@mx-space/api-client'
import { allControllers, createClient } from '@mx-space/api-client'

import { isLogged } from '~/atoms'
import { API_URL } from '~/constants/env'
Expand Down Expand Up @@ -117,17 +112,3 @@ export const attachFetchHeader = (key: string, value: string) => {
}
}
}

export const getErrorMessageFromRequestError = (error: RequestError) => {
if (!(error instanceof RequestError)) return (error as Error).message
const fetchError = error.raw as FetchError
const messagesOrMessage = fetchError.response?._data?.message
const bizMessage =
typeof messagesOrMessage === 'string'
? messagesOrMessage
: Array.isArray(messagesOrMessage)
? messagesOrMessage[0]
: undefined

return bizMessage || fetchError.message
}

0 comments on commit cfd3cbc

Please sign in to comment.