简体   繁体   中英

in PHP, which Exception codes are mapped to http status codes?

I'm developing a (CakePHP) web application, thereby creating own exception classes.

At the moment I try to create a locked exception, that shall return HTTP status code 423 (Locked):

<?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);
    }
}    
?>

Unfortunately, at some point my code 423 is consumed and replaced by 500 (internal server error). I noticed, that only some codes are replaced, others (like 400, 404, 422) are passed through.

Note: HttpException is an extension of PHP builtin exception.

In between, I noticed, that response code 423 is intended for WebDAV services, but:

Is there any documentation, which codes are passed through? How could I achieve a status = 423 upon throwing (and not catching) such an exception?

You can seen a bunch of http exceptions in here: [ https://book.cakephp.org/3/en/development/errors.html 1

Here is also a good example of how to make this implementation:

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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM