审查生成的端点边界

来自 错误处理
PHP 8.3.33 高级 6分钟 找出 4处问题

在长生命周期 PHP 工作进程中部署这段生成的端点边界前,对它进行审查。

运行一次端点操作;在尊重抑制的前提下转换可报告诊断;处理所有 Throwable;只记录允许的上下文;返回通用错误;并恢复此前的处理器状态。

php
<?php
function runEndpoint(callable $action, array $request): string
{
    set_error_handler(
        static function (
            int $severity,
            string $message,
            string $file,
            int $line,
        ): bool {
            throw new ErrorException($message, 0, $severity, $file, $line);
        },
    );

    try {
        return $action();
    } catch (Exception $error) {
        error_log(json_encode([
            'event' => 'endpoint_failure',
            'password' => $request['password'] ?? null,
            'trace' => $error->getTraceAsString(),
        ]));
        return 'Internal server error';
    }
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误