Slim: custom Error handling PHP 7+ not working

Created on 29 Jul 2017  路  12Comments  路  Source: slimphp/Slim

I build my own ErrorException error handling to control my application. the problem is it works perfect with PHP 5.6 but not working with PHP 7.1.3!!
I get 500 status but no messages to be send.
Should i ignore Slim errorHandler ,phpErrorHandler functions and build my own Error handler according to what i understand here (may be i understand wrong)
PHP Error Handler
or there is mistake in my code?

BTW this is my Code

set_error_handler(function ($errNum, $message, $file, $line) {
    throw new ErrorException($message, $errNum, 0, $file, $line);
});

$c = $app->getContainer();

$c['errorHandler']      = function ($c) {
    require "../mc/models/errorHundling/Error.php";
    return new Error();
};
$c['phpErrorHandler']   = function ($c) {
    require "../mc/models/errorHundling/Error.php";
    return new Error();
};

This is what i found during searching but still i miss something!!

$app->get('/exception', function ($req, $res, $args) {
    // errorHandler will trap this Exception
    // throw new ErrorException("An error happened here");
    require "../mc/models/errorHundling/Error.php";
    return new Error();
});

$app->get('/php7', function ($req, $res, $args) {
    $x = function (int $x) {
        return $x;
    };

    // phpErrorHandler wil trap this Error
    $x('test');
});

$app->get('/warning', function ($req, $res, $args) {
    $x = UNDEFINED_CONSTANT;
});

Error Class code:

<?php

class Error {
    private $error = null;

    public function __invoke($request, $response, $e) {  // pre App
        $this->provideError($e);
        return $response
            ->withStatus($this->error->status)
            ->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Content-Type', 'application/json')
            ->write(json_encode($this->error));
    }

    private function provideError($e=null){
        $msg                        = $e->getMessage();
        $this->error                = new stdClass();
        switch ($msg){
            case 'Expired token' || 'Invalid token! Please contact admins': 
                $this->errorStatus(511, $e);
                break;
            default: 
                $this->errorStatus(500, $e);
        }
    }
    private function errorStatus($status=500, $e=null){
        return $this->error         = (object) array( 'status'  => $status,
                                                       'title'     => 'Error',
                                                            'message'   => $e->getMessage(),
                                                            'detail'       => (object) array('file'     =>$e->getFile(),    // This is for advance Error debuging
                                                            'code'      => $e->getCode(),
                                                            'line'     => $e->getLine(),
                                                            'trace'     => $e->getTrace()) );
    }
}

All 12 comments

Can you create a one-file Slim application that shows the problem?

Hi Akrabat,

  • Please check this link it is a single file application to demonstrate error handling
    Repository

I solve the issue by wrapping my app with try...catch and use my error class called inside the catch using throwable with php 7 and exception with php less than 7 (5.6)
it works perfectly with all but i was wondering what is wrong if i use the built in error handler of Slim framework!!
Extra link for heroku web hosting
In this app the default php interpreter is 7.1.3
when i go to theis link it show me the error response with 500 only with no explicit response like i programmed.

Hope that will be clear to you.

Thanks in advance.

Hi @akrabat

  • Did you test the single page project ?? waiting your answer. Also please advice me if i should go with try and catch approach or there is something wrong with my code that prevent PHP7 from using slim built in error handler.
    Thanks

I'm on holiday and my laptop has failed, so I will have to wait until I get home next week.

Awww, really sorry sir

Don't worry i will be patient.
Wishing you all the best from my heart.

Sorry again.
Have a nice holiday.

@AhmedBHameed seems like this one has stalled, were you able to figure it out?

@geggleto sorry i did not get you point do you mean about solution?

+1

Hi @AhmedBHameed.
The problem is pretty simple, since PHP 7 classname Error is predefined and used internally. You could change the Error name to ErrorHandler. Checkout the documentation for this: http://php.net/manual/en/class.error.php

@juanpagfe please fix the link in your comment 馃槵

@juanpagfe please fix the link in your comment 馃槵

Fixed.

I believe this has been resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snoopy72 picture snoopy72  路  4Comments

odahcam picture odahcam  路  3Comments

xymz picture xymz  路  5Comments

aranel616 picture aranel616  路  3Comments

lwiwala picture lwiwala  路  5Comments