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

时间:2017-04-13 12:39:59

标签: php exception cakephp-3.0 http-status-codes

我正在开发一个(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?

1 个答案:

答案 0 :(得分:0)

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

这也是实现此实现的一个很好的例子:

use Cake\Core\Exception\Exception;

class MissingWidgetException extends Exception
{
    // Set your message here
    protected $_messageTemplate = 'Your message';

    // Set your status code here
    protected $_defaultCode = 404;
}

throw new MissingWidgetException(['widget' => 'Pointy']);
相关问题