报告 PHP 符合常规 content-type 格式的 JSON 错误
Report PHP errors in JSON format complying with the regular content-type
我将 AngularJs 用于单页应用程序,并通过 JSON 与服务器端 PHP 通信。 phpheader设置JSON,但是php报错:
php_flag display_errors 1
php_flag display_startup_errors 1
php_value error_reporting 32767
是 html 并且不匹配常规答案的 content-type header header('Content-Type: application/json;charset=utf-8');
因此 angularjs 不断抛出 php 错误。我应该使用 multicontent types 还是应该做什么?
为什么您的应用程序依赖 PHP 错误?
如果可能,请提供引发错误的代码部分。
您永远不应该使用 PHP 错误来停止您的应用程序,如果您需要 return JSON 结果,请执行干净退出。
您可以使用 try...catch
模式或在实际调用它之前检查引发错误的语句(例如,检查是否可以继续执行)。
看这里:
- Clean way to throw php exception through jquery/ajax and json
永远记得关闭最终应用程序中的错误:它们可能会向攻击者泄露大量信息(而且,它们看起来很糟糕)。
如果你必须 return PHP error/exceptions 到客户端,这是不推荐的(但我知道,这更容易开发),你需要一个自定义 error/uncaught-exception PHP 的处理程序。这样您就可以自定义 errors/exceptions 的显示方式。
这是一个示例代码,它输出错误和未捕获的异常作为 JSON 对象。
// Set error handler
set_error_handler('api_error_handler');
function api_error_handler($errno, $errstr) {
return api_error($errstr, $errno, 500);
}
// Set uncaught exceptions handler
set_exception_handler('api_exception_handler');
function api_exception_handler($exception) {
return api_error($exception->getMessage(), $exception->getCode(), 500);
}
// Error/Exception helper
function api_error($error, $errno, $code) {
// In production, you might want to suppress all these verbose errors
// and throw a generic `500 Internal Error` error for all kinds of
// errors and exceptions.
if ($environment == 'production') {
$errno = 500;
$error = 'Internal Server Error!';
}
http_response_code($code);
header('Content-Type: application/json');
return json_encode([
'success' => false,
'errno' => $errno,
'error' => $error,
]);
}
但这还不是全部;由于用户定义的错误处理程序无法处理致命错误,因此仍会显示致命错误消息。您需要通过调用 ini_set()
来禁用显示错误:
ini_set('display_errors', 0);
那么如何处理致命错误呢?可以使用 register_shutdown_function()
. In the shutdown handler, we need to get the last error information with a call to error_get_last()
在关闭时处理致命错误。所以:
// Set shutdown handler
register_shutdown_function('api_fatal_error_handler');
function api_fatal_error_handler() {
$error = error_get_last();
if ($error && error_reporting() && $error['type'] === E_ERROR) {
return api_error($error['message'], E_CORE_ERROR, 500);
}
}
然后在javascript方面,你必须添加一个错误回调并向用户显示错误信息。
毕竟,为什么不使用成熟的 error/exception 处理程序包而不是实现所有这些呢?认识 Whoops.
我将 AngularJs 用于单页应用程序,并通过 JSON 与服务器端 PHP 通信。 phpheader设置JSON,但是php报错:
php_flag display_errors 1
php_flag display_startup_errors 1
php_value error_reporting 32767
是 html 并且不匹配常规答案的 content-type header header('Content-Type: application/json;charset=utf-8');
因此 angularjs 不断抛出 php 错误。我应该使用 multicontent types 还是应该做什么?
为什么您的应用程序依赖 PHP 错误? 如果可能,请提供引发错误的代码部分。
您永远不应该使用 PHP 错误来停止您的应用程序,如果您需要 return JSON 结果,请执行干净退出。
您可以使用 try...catch
模式或在实际调用它之前检查引发错误的语句(例如,检查是否可以继续执行)。
看这里:
- Clean way to throw php exception through jquery/ajax and json
永远记得关闭最终应用程序中的错误:它们可能会向攻击者泄露大量信息(而且,它们看起来很糟糕)。
如果你必须 return PHP error/exceptions 到客户端,这是不推荐的(但我知道,这更容易开发),你需要一个自定义 error/uncaught-exception PHP 的处理程序。这样您就可以自定义 errors/exceptions 的显示方式。
这是一个示例代码,它输出错误和未捕获的异常作为 JSON 对象。
// Set error handler
set_error_handler('api_error_handler');
function api_error_handler($errno, $errstr) {
return api_error($errstr, $errno, 500);
}
// Set uncaught exceptions handler
set_exception_handler('api_exception_handler');
function api_exception_handler($exception) {
return api_error($exception->getMessage(), $exception->getCode(), 500);
}
// Error/Exception helper
function api_error($error, $errno, $code) {
// In production, you might want to suppress all these verbose errors
// and throw a generic `500 Internal Error` error for all kinds of
// errors and exceptions.
if ($environment == 'production') {
$errno = 500;
$error = 'Internal Server Error!';
}
http_response_code($code);
header('Content-Type: application/json');
return json_encode([
'success' => false,
'errno' => $errno,
'error' => $error,
]);
}
但这还不是全部;由于用户定义的错误处理程序无法处理致命错误,因此仍会显示致命错误消息。您需要通过调用 ini_set()
来禁用显示错误:
ini_set('display_errors', 0);
那么如何处理致命错误呢?可以使用 register_shutdown_function()
. In the shutdown handler, we need to get the last error information with a call to error_get_last()
在关闭时处理致命错误。所以:
// Set shutdown handler
register_shutdown_function('api_fatal_error_handler');
function api_fatal_error_handler() {
$error = error_get_last();
if ($error && error_reporting() && $error['type'] === E_ERROR) {
return api_error($error['message'], E_CORE_ERROR, 500);
}
}
然后在javascript方面,你必须添加一个错误回调并向用户显示错误信息。
毕竟,为什么不使用成熟的 error/exception 处理程序包而不是实现所有这些呢?认识 Whoops.