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()) );
}
}
Can you create a one-file Slim application that shows the problem?
Hi Akrabat,
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
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.