gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:34:12 26 4
gpt4 key购买 nike

我正在开发一个 (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 服务的,但是:

有没有文档,通过哪些代码?我怎样才能在抛出(而不是捕获)这样的异常时获得 status = 423 ?

最佳答案

你可以在这里看到一堆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']);

关于php - 在 PHP 中,哪些异常代码映射到 http 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com