没有一个明确的答案。这取决于消息提供的信息。
如果您的消息不提供任何敏感信息,您可以将它们打印给客户。
我喜欢使用$code
参数来将错误打印到客户端。
像这样的东西:
function clientError(Exception $e) {
$error = 'Unknown error!';
switch ($e->getCode()) {
case 404:
$error = 'Not found error!';
break;
case 403:
$error = 'You cannot access this page!';
break;
...
...
}
return "$error [error code: {$e->getCode()}]";
}
将错误信息保存在错误日志中,并将 clientError 打印到客户端:
try {
if (!$user->isMember()) {
throw new Exception("Guest {$user->id} tried to access to newPost.php page", 403);
}
}
catch (Exception $e) {
$errorLog->newError($e);
echo clientError($e);
}
此示例应在错误日志中添加一行消息:
“Guest 123 尝试访问 newPost.php 页面”
并打印:
“您无法访问此页面!”