I'm trying to catch an exception when sending an email, but Laravel won't do it. I read that Laravel turns every warning and error into an ErrorException but that doesn't seem to be working in my case.
$transportExchange = new SmtpTransport($myHost, 25, 'tls');
$transportExchange->setUsername('...');
$transportExchange->setPassword('...');
$configExchange = new Swift_Mailer($transportExchange);
try {
Mail::setSwiftMailer($configExchange);
Mail::to($email['to'])->send(new GeneralEmail($email));
} catch (ErrorException $ex) {
do_something();
}
When the password is not correct, I'm getting a Laravel error screen showing stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
But instead of that I want to reach the do_something() statement.
After three days I found the answer in here: Why does `catch (Exception $e)` not handle this `ErrorException`?
This is what I needed to do:
try {
static::$function_name($url);
} catch (\Exception $e) {}
Related
I am reading some extra information from Redis and the desired behaviour is to skip connection error silently, if any:
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (Error $e) {
;
} catch (Throwable $e) {
;
}
If Redis fails, monitoring system will show alert to right people to fix it.
Unfortunatelly the code above still causes Yii to fail and produce HTTP 500:
2018/04/09 12:28:04 [error] [php] Redis::connect(): connect() failed: Connection refused
What am I doing wrong?
You need to catch the Exception thrown...
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (\Exception $e) {
;
}
I think you can catch the very specific exception of Predis\Connection\ConnectionException if you need to.
I sometimes get in the log PHP Fatal error: Uncaught Exception: Connection reset by peer on socket_read()
How can I catch and ignore only this one exception, re-throwing any other?
My example handles all exceptions. If the exception contains the phrase, it allows you to handle that, otherwise, it rethrows an error message.
try {
// Your Code
} catch (Exception $e) {
if ( ! strpos($e->getMessage(), "Connection reset by peer") === false )
throw $e; // THROW IT, ITS A DIFFERENT ERROR
else
{
// Do Your Handling Code
}
}
I'm trying to ignore a PHP error via a try catch block but it doesn't seem to be working? I'm using it inside of of my controllers in Laravel.
try {
if (!self::isEmulatorOnline()) {
return;
}
$MUSdata = $command . chr(1) . $data;
$socket = \socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
$connect_timeval = array(
"sec"=>0,
"usec" => 100
);
\socket_set_option(
$socket,
SOL_SOCKET,
SO_SNDTIMEO,
$connect_timeval
);
\socket_connect($socket, Config::get('frontend.client_host_ip'), Config::get('frontend.mus_host_port'));
\socket_send($socket, $MUSdata, strlen($MUSdata), MSG_DONTROUTE);
\socket_close($socket);
}
catch(\PHPException $exception) {}
As you can see I am trying to silent the error exception, I know it is advised not to but its via an ajax request where I handle if the client IP and port can't be accessed using a different method.
Does anyone know why its returning the error exception even when making it silent out using try catch?
The error I am getting is
1/1) ErrorException
socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it.
On this line:
\socket_connect($socket, Config::get('frontend.client_host_ip'), Config::get('frontend.mus_host_port'));
You're trying to catch the wrong exception. socket_connect() throws an ErrorException and not an PHPException. You can also specify just the Exception class to catch all unhandled exceptions.
You can also add multiple catch blocks if you want to catch multiple exception classes to handle them differently.
Example:
try {
//
} catch (ErrorException $ex) {
// here you go.
}
In laravel you can catch error like this.
Try logging this
try {
//
} catch (\Exception $e) {
// catch error message
Log::info($e->getMessage());
// get http error code
Log::info($e->getCode());
}
I'm reading a URL that is finicky sometimes and throws a Uncaught exception Exception: SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054 I tried doing this:
try {
$body = Unirest\Request::get($url);
} catch (Exception $e) {
print $e;
return;
}
but still, the error is stopping my task. I would like to know if I am missing that will just execute the catch function and not stop it all together.
I'm trying to connect to a soap service using a WSDL file in php 5.6
The snippet below works fine if I'm on the network, but if I'm disconnected I get a fatal error.
try {
$soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
}
catch (SoapFault $fault) {
echo 'poop';
}
catch (Exception $exception) {
echo 'pee';
}
edit: it does seem to do something with the SoapFault, because I can see my 'poop' debug message, but it still results in a fatal error
These are the errors I get
Warning (2): SoapClient(): php_network_get_addresses: getaddrinfo failed: No such host is known.
Warning (2): SoapClient(http://soap.service.com/serivce.svc) [soapclient.soapclient]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known.
Error: SOAP-ERROR: Parsing Schema: can't import schema from 'http://soap.service.com/serivce.svc'
How can I gracefully handle the error so that php continues to run, so I can set a variable and render an HTML page indicating that there was a problem connecting
This was a cakephp issue
https://github.com/cakephp/cakephp/issues/8501
$restore = error_reporting(0);
try {
$soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
}
catch (SoapFault $e) {
trigger_error($e->getMessage()); // Overwrites E_ERROR with E_USER_NOTICE
}
finally {
error_reporting($restore);
}