【发布时间】:2020-07-09 04:10:27
【问题描述】:
我想为管理员渲染来自服务器的 500 错误回溯,所以:
- 在
server.js中,会话填充了从http_only cookie 检索到的用户,就像{'username': 'admin'}
polka()
.use(
sapper.middleware({
session: (req, res) => {
return { 'user': parseCookie('user') }
}
})
)
.listen(PORT);
- 在某些
index.js中有一个全局变量来存储来自服务器的 500 错误的可能回溯:
import { writable } from 'svelte/store';
export const error = writable();
- 在
index.html文章中预加载,如果出现500错误,如果当前用户为管理员,则在下方呈现回溯:
<script context="module">
import { error } from 'index.js';
export async function preload(page, session) {
return { article : await this.fetch('/api/article/').then(response => {
if (response.status == 500 && session.user.username === 'admin') {
error.set(response);
}
return response.json();
})}
}
</script>
<script>
export let article
</script>
<h1>{ article.title }</h1>
<div>{ article.text }</div>
<!-- 500 ERROR TRACEBACK --->
{#if $error}
{@html $error}
{/if}
那么,如果$error 是通过preload 函数设置的,那么它是否安全且仅在服务器端呈现?
如果没有,如何改进?
也许if (process.browser) 能以某种方式提供帮助?
谢谢
【问题讨论】: