slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Error rendering and description prop #3072

Closed ybelenko closed 3 years ago

ybelenko commented 3 years ago

I'm a bit confused with error rendering in Slim4.

You extended \Exception with new props title, description in \Slim\Exception\HttpException. There are no docs about these props, so by name itself I consider description is debug information and title is mainly a prop for something like 'Ooops, something went wrong'. Still don't know what should I pass into inherited message prop. It would be nice to get some help with what exception props are rendered and when.

After a small investigation of source code:

Renderer $title $message $description
HtmlErrorRenderer Always $displayErrorDetails is on displayErrorDetails is off
JsonErrorRenderer Always $displayErrorDetails is on Never
PlainTextErrorRenderer Always $displayErrorDetails is on Never
XmlErrorRenderer Always $displayErrorDetails is on Never

So it's not really obvious when to use description property since I can see it only with HtmlErrorRenderer with displayErrorDetails is off which is even more weird for development.

l0gicgate commented 3 years ago

It's pretty easy to deduce that information looking at the implementation of HttpMethodNotAllowedException and HttpNotFoundException:

class HttpMethodNotAllowedException extends HttpSpecializedException
{
    /**
     * @var string[]
     */
    protected $allowedMethods = [];

    /**
     * @var int
     */
    protected $code = 405;

    /**
     * @var string
     */
    protected $message = 'Method not allowed.';

    protected $title = '405 Method Not Allowed';
    protected $description = 'The request method is not supported for the requested resource.';
}
class HttpNotFoundException extends HttpSpecializedException
{
    /**
     * @var int
     */
    protected $code = 404;

    /**
     * @var string
     */
    protected $message = 'Not found.';

    protected $title = '404 Not Found';
    protected $description = 'The requested resource could not be found. Please verify the URI and try again.';
}

It's not documented as these are used internally. I agree that we probably should document them as people can extend HttpSpecializedException and throw them within their own application for their own special purpose.

I created an issue if someone wants to pick it up: https://github.com/slimphp/Slim/issues/3072