Unable to catch error in laravel using try catch - laravel

I wrote a code to try and catch error.
I want that if the error occurs then laravel continues to the next one and i write the error in the error file as shown here.
Laravel's black page of error pops up which shows "ErrorException (E_WARNING) followed by the whole error on the left page and the programme is abruptly stopped.
Is there a way to skip the error notification which brings my whole code to a stop.
My attempt at coding to catch the error is here:
try {
$jsondata_car = (file_get_contents($urltofind_car));
} catch (Exception $e) {
$errorfile = fopen("errors", 'a');
fwrite($errorfile, $e - > getmessage().
"\n");
$failed_car = 9999999;
report($e);
return false;
}
Am I missing out something?
The error I get is here
file_get_contents(): failed to open stream:
HTTP request failed! HTTP/1.1 400 Bad Request
The error is shown on the line
$jsondata_car = (file_get_contents($urltofind_car));
The solution is here
Exception is used which should have contained a namespace.
The problem is that this does not throw up an error even though I had not written
Use Exception;
at the beginning.
As suggested by #simon R I made the rectification and it worked.

It is most likely still throwing the error because you haven't imported the Exception class
Ensure you either add
Use Exception;
to your class.
Alternative update your code to namespace it;
try {
$jsondata_car = (file_get_contents($urltofind_car));
} catch (\Exception $e) {
$errorfile = fopen("errors", 'a');
fwrite($errorfile, $e - > getmessage().
"\n");
$failed_car = 9999999;
report($e);
return false;
}

Related

doing something after Laravel's http client exception

I need to access to an online file and reading is content, but if for some reason the file isn't available due network/server problem ill use a local version stored on my server.
For doing that I've written this simple code:
$response = Http::timeout(2)->get('http.site/file.json');
And I add this for checking if all is ok:
if($response->successful()){
$list = $response->body();
} else {
$list = file_get_contents(asset('storage/list.json'));
}
But if I have a problem (for testing I just add a not correct address) of connection an exception is thrown and I cannot go into "else" part.
So I add a "try, catch":
try{
$response = Http::timeout(2)->get('http.site/file.json');
return $response->body();
}catch (Exception $ex) {
return file_get_contents(asset('storage/pharmaciesList.json'));
}
which is the correct way to treat this kind of code, and is it correct using a try catch without taking care of the exception?
Of course it's the right way, every function that could throw an exception should be enclosed in a try catch block. Otherwise (you said you're using Laravel) you can handle that exception in the exception handler.

How to throw exception with MessageBox in Xamarin

I'm make an app which get data from API, but sometimes error will occur.
I have throw exception in my code when StatusCode is not Ok. But i have to handle it.
So, my question is how to get the exception on another thread and throw it with message box in xamarin.
ProductsNew = await Task.Run(() =>
{
ProductViewData productNewViewData = new ProductViewData();
return productNewViewData.GetProductNew("5");
});
Thanks for any help.
Put your code in try and catch block and show the error on message box.
try
{
//code
}
catch(Exception ex)
{
DisplayAlert("Error",ex.Message,"Ok");
}

Laravel package whoops exception

In my Laravel package I have this code:
try {
$var_msg = "Exception example";
throw new InvalidNameException($var_msg);
}
catch (InvalidNameException $e) {
abort(403, $e->getMessage());
//report($e);Exception Log
}
The error is displayed as html error page. But, I'd like to report the error as an whoops error.
Take this code as reference.
$whoops = new \Whoops\Run();
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
// Set Whoops as the default error and exception handler used by PHP:
$whoops->register();
throw new \RuntimeException("Oopsie!");

Unable to catch adLDAP Exception on Laravel 5.1?

When I get an exception while running the below code, it does not return the exception message but loads Laravel’s error handling page. I’m trying to return the exception error message.
try {
$adldap = new adLDAP(Config::get('app.ldap'));
}
catch (adLDAPException $e) {
return $e;
}
I just encountered this issue and discovered that AdLDAP 5.0.0 onwards requires you to use 'adLDAP\Exceptions\adLDAPException'.
Rob Ganly

magento newsletter keeps returning an error even after email is saved

There was a problem with the subscription.
this is the message I get after I enter an email. When I go to the admin panel the email is added.
I would like it NOT to display this message. any suggestions please
The only place in Magento that error string occurs is in the following try/catch block
#File: app/code/core/Mage/Newsletter/controllers/SubscriberController.php
try {
//...snip!...
}
catch (Mage_Core_Exception $e) {
$session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
}
catch (Exception $e) {
$session->addException($e, $this->__('There was a problem with the subscription.'));
}
Since you reported the error message as "There was a problem with the subscription.", that means the newsletter subscription code is throwing some sore of PHP exception, caught by the catch (Exception $e) { block. Magento does not output the messages from PHP exceptions. If I was in your position I'd temporarily change the exception handling code to include the error message
$session->addException($e, $this->__('There was a problem with the subscription. ' . $e->getMessage()));
This will let you track down the PHP error that's triggering your error message.
Per the comment below, the only place the "Cannot set standard header from addHeader()" exception error exists is
#File: lib/Zend/Mail.php
$prohibit = array('to', 'cc', 'bcc', 'from', 'subject',
'reply-to', 'return-path',
'date', 'message-id',
);
if (in_array(strtolower($name), $prohibit)) {
/**
* #see Zend_Mail_Exception
*/
#require_once 'Zend/Mail/Exception.php';
throw new Zend_Mail_Exception('Cannot set standard header from addHeader()');
}
$value = $this->_filterOther($value);
$value = $this->_encodeHeader($value);
$this->_storeHeader($name, $value, $append);
return $this;
My guess is somewhere in your system someone's added some custom code that tries to set one of the standard email headers via the addHeader method.

Resources