在PHP中,哪些异常代码映射到http状态代码?

问题描述 投票:4回答:1

我正在开发(CakePHP)Web应用程序,从而创建自己的异常类。

目前,我尝试创建一个锁定的异常,该异常应返回HTTP状态码423(锁定):

<?php
namespace App\Exceptions;

use Cake\Network\Exception\HttpException;

class MeasuringPointLockedException extends HttpException{   
    /**
     * Constructor
     *
     * @param string $message If no message is given 'Locked' will be the message
     */
    public function __construct($message = 'Locked'){   
        parent::__construct($message, 422);
    }
}    
?>

[不幸的是,在某些时候我的代码423被消耗了,并被500取代(内部服务器错误)。我注意到,只有部分代码被替换,其他代码(例如400、404、422)则通过。

注意:HttpException是PHP内置异常的扩展。

我注意到,在这之间,响应代码423用于WebDAV服务,但是:

是否有任何文档,通过了哪些代码?抛出(而不是捕获)此类异常时,如何获得状态= 423?

php exception cakephp-3.0 http-status-codes
1个回答
0
投票

您可以在这里看到很多http异常:[https://book.cakephp.org/3/en/development/errors.html 1

这里也是如何实现此示例的好例子:

use Cake\Core\Exception\Exception;

class MissingWidgetException extends Exception
{
    // Context data is interpolated into this format string.
    protected $_messageTemplate = 'Seems that %s is missing.';

    // You can set a default exception code as well.
    protected $_defaultCode = 404;
}

throw new MissingWidgetException(['widget' => 'Pointy']);
© www.soinside.com 2019 - 2024. All rights reserved.