Cphalcon: [NFR] Exception Error Page Handling

Created on 26 Aug 2017  路  4Comments  路  Source: phalcon/cphalcon

Expected and Actual Behavior

Recently I wanted to throw errors based on database output for instance if a user tries to view an ID of object that's not in the database I want to throw a 404. I find my self-having to manually dispatch the error controller to do this.

Exepected behavour:

$player = Players::findFirstByPid($id);
    if(!$player) {
        return throw new HttpNotFoundException('message');
    }
// continue execution
````
should then be caught and invoke the defined not found controller::method

Actual behavour:
```php
$player = Players::findFirstByPid($id);
if(!$player) {
    return $this->dispatcher->forward([
        'controller' => 'Index',
        'action' => 'httpError',
        'params' => ["No player found for ID: {$id}"]
    ]);
}

This then also re-execution the ControllerBase::initialize() method, wich adds extra unwanted load

Most helpful comment

The framework shouldn't couple application specific actions and controllers to an exception. So no, the framework shouldn't implement this. As I said, you can literally write the exact code you want now using the framework.

Create your custom application specific error handler

class HttpNotFoundException extends \Exception
{
    public function __construct($message)
    {
        // Do your application specific dispatch/forwarding as needed.
        // This logic is unique for EVERY APPLICATION - Setting headers, redirecting externally,
        // forwarding to view, logging custom data, returning JSON, etc.
    } 
}

USE YOUR EXISTING CODE AS IS

$player = Players::findFirstByPid($id);
    if (!$player) {
        throw new HttpNotFoundException('message');
    }

Phalcon is not going to attempt to predefine common scenarios as it would be a one off and everyone has different needs. Phalcon is a framework, not designed to replace your application needs.

All 4 comments

Just wrap your own exception that dispatches just as you've specified. Create your own HttpExceptions to dispatch what you want. The framework provides the tools required for you to build/extend your application.

Just wrap your own exception that dispatches just as you've specified. Create your own HttpExceptions to dispatch what you want. The framework provides the tools required for you to build/extend your application.

While entirely possible. Having something readily available for such a common problem would be a better solution then telling everyone just make your own wrapper.

I worked and currently still work with symfony and using their HttpExeceptions is easy to quickly and easily a 404, 403 or any status code you need to use to display to the user.

http://api.symfony.com/3.2/Symfony/Component/HttpKernel/Exception.html

The framework shouldn't couple application specific actions and controllers to an exception. So no, the framework shouldn't implement this. As I said, you can literally write the exact code you want now using the framework.

Create your custom application specific error handler

class HttpNotFoundException extends \Exception
{
    public function __construct($message)
    {
        // Do your application specific dispatch/forwarding as needed.
        // This logic is unique for EVERY APPLICATION - Setting headers, redirecting externally,
        // forwarding to view, logging custom data, returning JSON, etc.
    } 
}

USE YOUR EXISTING CODE AS IS

$player = Players::findFirstByPid($id);
    if (!$player) {
        throw new HttpNotFoundException('message');
    }

Phalcon is not going to attempt to predefine common scenarios as it would be a one off and everyone has different needs. Phalcon is a framework, not designed to replace your application needs.

Well i totally agree. This is useless, what you would expect phalcon should do with this throwed exception? You would need to configure it somehow or create some handler so create some class and method anyway. Much faster and easier is just to write your own code in php.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bestirani2 picture bestirani2  路  3Comments

kkstun picture kkstun  路  3Comments

linxlad picture linxlad  路  3Comments

abcpremium picture abcpremium  路  3Comments

hailie-rei picture hailie-rei  路  3Comments