Using monolog to log my exceptions:
try {
// php code
} catch (\Exception $e) {
$log->log(100, $e->getMessage());
}
I had problems when two parallel processes logging an error. Sometimes it worked correctly, but sometimes an error occurred saying that monolog log file “error.txt” could not be opened.
Using the native php error logging:
ini_set('error_log', "error_log.txt");
error_reporting(-1);
I had no problems so far.
Is it safe to make error logs with two parallel processes with this method ?
EDIT
$log refers to monolog Logger object:
$log = new Logger('error');
$log->pushHandler(new StreamHandler('error.txt', Logger::DEBUG));
Related
How do you catch errors thrown by the HTTP client (for example a time out) so that it doesn't throw the curl error in the Laraval debugger (in debug mode) before you can do anything with the error to avoid stopping the execution?
use Illuminate\Support\Facades\Http;
try {
$request = Http::post('https://example.com/post', [
'password' => 'guest']);
} catch(ConnectException $e)
{
//log error
}
//continue with another mode
Instead, I'm always getting the Laravel's Ignition error page
Illuminate\Http\Client\ConnectionException
cURL error 28: Failed to connect to example.com port 443: Timed out
and the error is not caught by my code. Is it possible that the laravel debugger always have priority and can't be overridden in debug mode?
This is almost certainly a namespacing issue.
You'll need either this at the top of the file:
use Illuminate\Http\Client\ConnectionException;
or do this:
} catch(\Illuminate\Http\Client\ConnectionException $e)
Otherwise, you're actually trying to catch something in the current namespace named ConnectionException (i.e. something like App\Controllers\ConnectionException), which will never exist.
I am working on one project that requires me to execute some commands on remote server. I am using Laravel 5.5 with package name "laravelcollective/remote" that uses SSH2 to connect with the remote server.
However, I am facing some really weird issues with some servers. I get the following error message on some of the servers.
production.ERROR: Connection closed prematurely {"exception":"[object] (ErrorException(code: 0): Connection closed prematurely at /home/username/application_name/public_html/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php:3821, RuntimeException(code: 0): Unable to connect to remote server. at /home/username/application_name/public_html/vendor/laravelcollective/remote/src/Connection.php:143)
I am using try-catch block to catch exceptions but I am unable to catch this exception. All other Exceptions like connection timed out are being caught by the try-catch block except this one.
I am using try catch block like this:
try {
$commands = array('sudo apt-get update','sudo apt-get upgrade -y');
SSH::run($commands);
} catch (\Exception $e){
report($e);
}
But the try-catch block stops working with this connection closed prematurely error. I don't know if I am missing something or there is some bug with the library, has anyone faced the same issue before? How can I catch this error the right way?
it's because an exception is not thrown, just an error. and an error cannot be caught.
I suggest you set a global error handler that converts all the errors to exceptions, as seen in this answer here and it would be a good idea to read other answers to that question too.
Generate a custom exception handler and then in a provider or in your public/index.php set your error handler
I have developed a PHP Script which generates a simple financial report and sends it via SMTP (using the PHPMailer library).
I am attempting to host and execute the script using Azure Web Jobs, but unfortunately the job is failing each time I run it.
The WebJob is located at wwwroot\App_Data\jobs\triggered\send-sales-report\send_sales_report.php on Azure.
The contents of the script are as follows; as can be seen, the script imports a number of other scripts:
<?php
try
{
include "./../../../../inc/class.EmailMessage.inc";
include "./../../../../E-Mail.php";
$message = new EmailMessage('sales_report', array());
SendOrderEmail('Sales Report', $message->render(), 'name#emailprovider.com', 'Recipient Name');
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
In addition, the directory structure of the files mentioned above is as follows:
wwwroot
|___App_Data
|___jobs
|___triggered
|___send-sales-report
|___send_sales_report.php
|___inc
|___class.EmailMessage.inc
|___emails
|___sales_report.php
|___E-Mail.php
When I run the PHP WebJob using the Azure Portal (both manually and on a schedule), it fails with exit code 255; however, when I execute the script using the Kudu Console on Azure it works just fine. In addition, I have also replicated the running environment locally and it is working fine.
Any help is much appreciated.
I would recommend you to enable PHP Error Logs for your app. Create a .user.ini file and add the following to it:
log_errors = on
Refer this blogpost: Enable PHP error logging
The default location for the PHP Error logs will be: D:\home\LogFiles\php_errors.log
This should help in proceeding further.
255 is an error. See PHP exit status 255: what does it mean?. So, You may encounter a Fatal error in your PHP script.
It is possible to catch Fatal Errors by using register_shutdown_function function. The following code sample you can use to log the Fatal error when it occurred.
<?php
register_shutdown_function('shutdown_function');
try
{
include "./../../../../inc/class.EmailMessage.inc";
include "./../../../../E-Mail.php";
$message = new EmailMessage('sales_report', array());
SendOrderEmail('Sales Report', $message->render(), 'name#emailprovider.com', 'Recipient Name');
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
function shutdown_function()
{
$e = error_get_last();
print_r($e);
}
I'm building an application with ZF2.
I use ajax to POST some data on the application and when I trhrow a new Exception with this line:
throw new \Exception("Not Loged In.", 401);
The problem is everytime I throw a new error it returns a 500 even if I put anything as a second parameter of the exception.
Can anyone help me?
Thanks!
From zend framework's request lifecycle perspective every uncaught exception is an application error. You need a mechanism to convert that exceptions to meaningful HTTP responses before the framework convert them to an HTTP 50X for you.
For example, in your controller you can try something like below:
try {
$this->myService->tryToDoSomethingThatNeedsAuthentication();
} catch(AuthRequiredException $e) {
$this->getResponse()->setStatusCode($e->getCode()) // Assuming its 401
->setReasonPhrase($e->getMessage());
return;
} catch(\Exception) {
// handle other exceptions here
}
Problem is in your php configuration. Default Apache (or other server) hide details of your internal errors, so is returned short info:
Error 500 - internal server error
You must enable error_reporting:
in PHP:
http://php.net/manual/pl/function.error-reporting.php
or in php.ini in Apache configuration, and in .htaccess file it's possible. For developer's work you should show all errors.
I am trying to implement an PHPUnit integration-test for a Controller. Within the controller I instantiate an Object of the PHPRedis-class (https://github.com/phpredis/phpredis#usage):
//connect to redis database
try
{
$this->objPHPRedis = new \Redis();
$this->objPHPRedis->connect('127.0.0.1');
}
catch(\RedisException $e)
{
$this->log($e, 1);
}
catch (\Exception $e)
{
$this->log($e, 1);
}
This works great when I process normal requests, but it fails when I execute the test:
PHP Fatal error: Class Redis not found in /var/www/.../MyController.php on line 100.
I have no idea where to start searching for the problem. Does anybody have an idea what the problem could be?
Your CLI config for php has the redis module not loaded. Find your CLI php.ini, probably named cli_php.ini or something similar and make sure your redis module is loaded there as well.