Skip to content

Commit

Permalink
fix: improve RenderErrorPage() dev logging
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Aug 22, 2023
1 parent 6ba3a2a commit 2dbc2a2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/react-full/.testRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function testRun(viewFramework: 'vue' | 'react', cmd: 'npm run dev' | 'npm run p
)
if (!isV1Design) {
expectLog(
'[Warning] `throw RenderErrorPage()` is deprecated and will be removed in the next major release. Use `throw render()` or `throw redirect()` instead, see https://vite-plugin-ssr.com/render',
'[Warning] throw RenderErrorPage() is deprecated and will be removed in the next major release. Use throw render() or throw redirect() instead, see https://vite-plugin-ssr.com/render',
(log) => log.logSource === 'stderr'
)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"devDependencies": {
"@brillout/bump-dependencies": "^0.1.1",
"@brillout/dev-my-dep": "^0.1.5",
"@brillout/test-e2e": "^0.5.10",
"@brillout/test-e2e": "^0.5.11",
"@brillout/test-types": "^0.1.6",
"vitest": "^0.34.1"
},
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-e2e.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function tolerateError({ logSource, logText }) {
logText.includes('[Warning]') &&
logText.includes('throw RenderErrorPage()') &&
logText.includes(
'is deprecated and will be removed in the next major release. Use `throw render()` or `throw redirect()` instead'
'is deprecated and will be removed in the next major release. Use throw render() or throw redirect() instead'
)
)
}
Expand Down
25 changes: 17 additions & 8 deletions vite-plugin-ssr/shared/route/abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ function render(abortStatusCode: 401 | 403 | 404 | 429 | 500 | 503, abortReason?
*/
function render(url: `/${string}`, abortReason?: unknown): Error
function render(value: string | number, abortReason?: unknown): Error {
return render_(value, abortReason)
const args = [typeof value === 'number' ? String(value) : JSON.stringify(value)]
if (abortReason !== undefined) args.push(truncateString(JSON.stringify(abortReason), 30, null))
const abortCaller = 'render'
const abortCall = `throw render(${args.join(', ')})` as const
return render_(value, abortReason, abortCall, abortCaller)
}

function render_(
value: string | number,
abortReason: unknown | undefined,
abortCall: AbortCall,
abortCaller: 'render' | 'RenderErrorPage',
pageContextAddendum?: { _isLegacyRenderErrorPage: true } & Record<string, unknown>
): Error {
const pageContextAbort = { abortReason }
Expand All @@ -115,8 +121,8 @@ function render_(
const args = [typeof value === 'number' ? String(value) : JSON.stringify(value)]
if (abortReason !== undefined) args.push(truncateString(JSON.stringify(abortReason), 30, null))
objectAssign(pageContextAbort, {
_abortCaller: 'render' as const,
_abortCall: `throw render(${args.join(', ')})` as const
_abortCaller: abortCaller,
_abortCall: abortCall
})
}
if (typeof value === 'string') {
Expand All @@ -136,7 +142,7 @@ function render_(
}
}

type AbortCall = `throw redirect(${string})` | `throw render(${string})`
type AbortCall = `throw redirect(${string})` | `throw render(${string})` | `throw RenderErrorPage()`
type PageContextAbort = {
_abortCall: AbortCall
} & (
Expand All @@ -145,12 +151,12 @@ type PageContextAbort = {
_urlRedirect: UrlRedirect
} & Omit<AbortUndefined, '_urlRedirect'>)
| ({
_abortCaller: 'render'
_abortCaller: 'render' | 'RenderErrorPage'
abortReason: undefined | unknown
_urlRewrite: string
} & Omit<AbortUndefined, '_urlRewrite'>)
| ({
_abortCaller: 'render'
_abortCaller: 'render' | 'RenderErrorPage'
abortReason: undefined | unknown
abortStatusCode: number
} & Omit<AbortUndefined, 'abortStatusCode'>)
Expand All @@ -168,13 +174,16 @@ function AbortRender(pageContextAbort: PageContextAbort): Error {
return err
}

// TODO/v1-release: remove
/**
* @deprecated Use `throw render()` or `throw redirect()` instead, see https://vite-plugin-ssr.com/render'
*/
function RenderErrorPage({ pageContext = {} }: { pageContext?: Record<string, unknown> } = {}): Error {
assertWarning(
false,
'`throw RenderErrorPage()` is deprecated and will be removed in the next major release. Use `throw render()` or `throw redirect()` instead, see https://vite-plugin-ssr.com/render',
`${pc.cyan('throw RenderErrorPage()')} is deprecated and will be removed in the next major release. Use ${pc.cyan(
'throw render()'
)} or ${pc.cyan('throw redirect()')} instead, see https://vite-plugin-ssr.com/render`,
{ onlyOnce: false }
)
let abortStatusCode: 404 | 500 = 404
Expand All @@ -184,7 +193,7 @@ function RenderErrorPage({ pageContext = {} }: { pageContext?: Record<string, un
abortReason = 'Something went wrong'
}
objectAssign(pageContext, { _isLegacyRenderErrorPage: true as const })
return render_(abortStatusCode, abortReason, pageContext)
return render_(abortStatusCode, abortReason, 'throw RenderErrorPage()', 'RenderErrorPage', pageContext)
}

const stamp = '_isAbortError'
Expand Down

0 comments on commit 2dbc2a2

Please sign in to comment.