Unable to catch adLDAP Exception on Laravel 5.1? - laravel

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

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 error in laravel using try catch

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;
}

BreezeJS - server validation changed in version 1.4.4

I've updated breezejs from version 1.4.1 to 1.4.4. For server-side validation, I was handling the AfterSaveEntitiesDelegate on the ContextProvider and throwing EntityErrorsException().
In the release notes I read:
The Breeze WebApi response to any SaveChanges operation that has
validation errors now returns a 403 status code, instead of a 200.
This has no effect on any Breeze code but will be noticeable to anyone
watching Breeze's network traffic.
However, the new 403 error does not have any details about the validation error or any inner exceptions that would indicate it is a validation error. Instead the error is: "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details." There is no "Response" property. Also, now my client-side code no longer "understands" this error.
Turns out that this exception is not intended to be caught and wrapped into an HttpResponseMessage. My code for the BreezeController SaveChanges() api was:
try
{
return Request.CreateResponse(HttpStatusCode.OK, _commDataService.SaveChanges(pSaveData, shouldValidate));
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
This was returning the right exception but with the wrong status code (500 instead of 403 as expected by updated breezejs client code.
I added a new catch to pass along the breeze-constructed response message:
catch (System.Web.Http.HttpResponseException responseException)
{
//todo: logger call.
return responseException.Response;
}

Resources