我的服务器日志中出现以下错误。
用户只能在浏览器中看到这个(来源)
<html>
<head>
<title>Error</title></head>
<body text="#000000">
<h1>Error 500</h1>HTTP Web Server: Command Not Handled Exception</body>
</html>
所以我可以通过服务器日志判断服务器抛出了一个安全异常,可能是因为我的 java.policy 文件中的设置有误。但我的问题不是导致错误的原因,而是如何避免用户收到这些丑陋的 500 错误。
我希望我在应用程序中设置的错误页面像任何其他异常一样呈现给用户。
可能的?
最佳答案
你的代码中的 try/catch 块越多越好(当然在合理范围内):
try {
// code that might throw an error
} catch (e) {
// examine the error to see if there's a workaround
// if not, log it and inform the user
} finally {
// any code that needs to run whether or not there was an error
}
这样,如果某件事失败了,它就会优雅地失败。一定要让用户清楚出现问题(并且最好向他们提供他们可以实际跟进的说明)......如果出现问题并且用户认为出现问题,静默失败甚至比丑陋的错误页面更糟糕一切都很好。
附言正如 Stephan 指出的那样,有些错误根本无法被发现。例如,如果 XPage 没有由有权运行 XPage 的人签名,它甚至不会尝试运行您的代码……页面本身是无效的,因此您在运行时无能为力。始终确保在部署期间对 XPage 进行签名。
关于security - 服务器抛出异常时如何避免用户收到 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581337/