Cphalcon: Phalcon 3.3 Micro error handler

Created on 17 Jan 2018  路  18Comments  路  Source: phalcon/cphalcon

Expected and Actual Behavior

The Micro Application error handler isn't working properly on the latest version of Phalcon. The exception isn't caught by the handler.

Expected output

{"code":401,"status":"error","message":"Invalid email or password."}

Actual output

screen shot 2018-01-17 at 15 24 30

Script to reproduce

<?php

$app = new Phalcon\Mvc\Micro();

$app->get(
    '/',
    function () {
        throw new \Exception('Invalid email or password.', 401);
    }
);

$app->error(
    function ($exception) {
        echo json_encode(
            [
                'code'    => $exception->getCode(),
                'status'  => 'error',
                'message' => $exception->getMessage(),
            ]
        );
    }
);

$app->handle();

Details

  • Phalcon version: 3.3
  • PHP Version: 7.1 & 7.2
  • Operating System: macOS & Ubuntu 16.04
  • Installation type: Installing via package manager
  • Server: Nginx & Apache

Most helpful comment

Not on the same function! You need to return false or response object in $app->error() handler

All 18 comments

You have Xdebug enabled. That is the correct behavior. /close

That makes no sense, and my Plesk server doesn't have Xdebug enabled but the logs are still being filled with errors. Isn't this error handler supposed to handle the errors?

Can you describe what is wrong with your output. Because it looks fine to me.

If what you expect is for the error handler to "catch" the exception, then this will not happen by design.
The error handler is a function that will purely be called in case of an fatal error, it does not have any "try/catch" functionality. It is just there for you, to be able to send a valid response back to the client.

Then I must have understood it wrong. The documentation says "can be used to trap any errors that originate from exceptions", so I thought it would catch them and handle the error. I don't see the point of this error handler then. I can't even send a valid JSON when it prints a PHP error.

You can just disable the printing of PHP error in you php.ini.
That I will always recommend on a production server anyway.

You're exception is caught everything is handled normally. XDebug handles all exceptions as well with their own table output. Just disable it and everything works as expected.

It's not XDebug. I've tried on a server without XDebug and it still throws a Fatal Error:

{"code":401,"status":"error","message":"Invalid email or password."}
Fatal error: Uncaught Exception: Invalid email or password. in .../teste.php:8 Stack trace: #0 [internal function]: Closure->{closure}() #1 .../teste.php(24): Phalcon\Mvc\Micro->handle() #2 {main} thrown in .../teste.php on line 8

So is it supposed to catch the exception or not? I think I'll check the source myself... :D

Ah above comment is right ... it's the built in display error setting for PHP.

@virgofx So, do you agree if I say that I shouldn't use this error handler for, as an example, throw a 401 response with a Invalid password or even a 400 error with a bad response error.

Because if I do, even if the error isn't shown in the browser/app, it is logged. The logs would get huge because of failed login attempts. <-- Just an example...

It is html_errors = On guys.

Both XDebug and html_errors are off in my server.

http://testes.lavaimagem.info/phalcon_micro_handler.php

That is the script I specified in the original comment above. It works here, but then I take a look at the apache error.log file:

screen shot 2018-01-19 at 11 05 49

I just need to know if this is indeed the intended behaviour, which, for me, still makes no sense... :p

https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/micro.zep#L977

As you see you either need to return response or false here.

First best to check the actual code to check if you are not doing something wrong. There is no any behavior for not returning anything. Error method exepects that this handler returns response object or false.

@Jurigag I suppose that's some other error handler. You can't throw an Exception and then return some boolean on the same function.

Not on the same function! You need to return false or response object in $app->error() handler

Yeah, return false does the trick. Why isn't that on the documentation?
Thanks mate.

I don't know, ask @niden xD

@Jurigag do you know if is possible to put the error code handler on middleware? Like we do on ResponseMiddleware to format successfully responses?

It's display_errors = On

Was this page helpful?
0 / 5 - 0 ratings