How to properly handle exception inside controller? - laravel

I am trying to upload a file. And if it throws an exception I don't want to see the "whoops" page. Instead it will go back the previous page with a message. This is what I tried,
try {
$data = Excel::toArray(new Import, request('file'));
} catch (\Exception $e) {
return back()->withErrors("an exception occured");
}
But it still gives me the whoops page whenever any exception occurs.
How to solve it?

Laravel 5.0 and up ships with an error handler at app/Exceptions/Handler.php. Here you can define a handler for any exception in your application. This way you don't need to add extra error handling in your controllers.
All information can be found in the documentation: https://laravel.com/docs/6.x/errors#the-exception-handler

I don't believe you will be able to catch/handle a Fatal Error. The script has stopped and is unrecoverable at this point. A shutdown handler is running as the script is shutting down. This is being handled by a shutdown function via register_shutdown_function.
Illuminate\Foundation\Bootstrap\HandleExceptions#handleShutdown will check to see if there was an error and if it was fatal and then pass a Symfony\Component\Debug\Exception\FatalErrorException to its own handler as an example.

Related

Object not set to an instance on OnPause()

I get 'Object not set to an instance' error on my App every time my App request permission to access READ_PHONE_STATE and ACCESS_FINE_LOCATION in OnPause() method. I didn't write code to request permission, the App request permission automatically.
As soon as the permission dialog shows up, OnPause() method gets triggered and the error occurs on this line : base.OnPause();
protected override void OnPause()
{
base.OnPause(); //error occurs here
}
How can i solve this issue ?
What's the best method of requesting all the relevant permissions ? And how can I you handle this method in OnResume() and OnPause() methods.
I finally found the issue. The problem was caching on my phone. Clicking Ctrl+F5, forced the Application to clear all the cache. Rebuild didn't work. After clicking Ctrl+F5, the permission dialogs showed without triggering the OnPause function - and object not set to an instance error message disappeared.

How to catch System.InvalidOperationException: Connection was disconnected before invocation result was received

I´m getting this error exactly how the exception message says.
If a mobile client loses connection before the proxy.Invoke() result arrive, the exception is raised. That´s ok, but I need to catch this to avoid an app crash.
I try/catch all the proxy.Invoke() and proxy.Invoke<T>() calls, with no effect though.
How can I catch that exception?
Note: I´m using SignalR client 2.2.0 in a Xamarin client (PCL)
If you are calling your proxy.Invoke() without await proxy.Invoke() then the exception won't bubble up from the Invoke task to your executing code.
I've had to deal with this error before (with Xamarin in a PCL), and simply executing my Invoke like such worked for me:
try
{
await hubProxy.Invoke("SomeMethod", args);
}
catch (InvalidOperationException ex)
{
// Do what you need to with the exception
}
There is also a conversation about it here on the SignalR GitHub.

How to intercept and handle all exception of Windows phone Application?

We did't have any Test team support for our Product Development.
so.
we need intercept and handle all Exception for improve User experience.
is There have any Soluction in windows phone Application?
as Fllow in app.xaml.cs file. we found :
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is QuitException)
return;
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
Yes, this should catch all the exceptions that you missed in your app. Considering that you obviously are not catching many exception somewhere else and are looking for a simple solution, this event handler might work, but I seriously don't recommend it.
This event handler catches the exceptions and then should crash/quit your app. If you handled your exceptions only here, this would lead to a huge crash count. Sometimes exceptions happen, but the app can continue working normally. That's why I recommend that you handle them as they happen in your code, and not here. That way you have a full control of how your app continues and if it continues at all, and reduce the number of "unhandled exceptions" and app crashes.
Put your code in Try-Catch Block. I was also facing such problem, but then handled by Exception Handling Method.
try
{
// your code
}
catch (Exception ex)
{
throw (ex);
}

Catching server side ajax handling errors in browser

Let's say I have some ajax based component, whose handler in server throws for some reason an exception (e.g. programming error, can't access database). And basically server responds with internal server error or some such. How can I catch this situation in browser and display for examplen an error message somewhere.
When user clicks this link, the server will show an error page and the browser should detect based on http status code that something went wrong and somehow to react to it.
new AjaxLink<Link>("link") {
public void onClick(AjaxRequestTarget target) {
throw new RuntimeException();
}
}
See org.apache.wicket.settings.IExceptionSettings#getAjaxErrorHandlingStrategy.
There is an example of this at http://www.wicket-library.com/wicket-examples/ajax/links (failure and exception links).
Normally, Wicket Ajax classes like AjaxFallbackLink for example, have a method
updateAjaxAttributes(AjaxRequestAttributes attributes)
where you can register error handlers, that execute javascript within the browser.
Override this method, create an AjaxCallListener and call
AjaxCallListener listener = new AjaxCallListener();
listener.onFailure(ADD_JAVASCRIPT_TO_EXECUTE_HERE);
attributes.getAjaxCallListeners().add(listener);
see the documentation of
org.apache.wicket.ajax.attributes.AjaxCallListener.getFailureHandler(Component component)

C# windows service crashes without hitting catch block

I have a c# windows service that crashes without logging almost everyday after running for a few hours. Recently I added catch blocks to literally every method and still it doesn't help. Since I am using asynchronous callbacks on MSMQ, I guess there could be some multi-threading issues, but I have no clear clue. Any insight into this problem will be very helpful. Here is the pseudo code:
public MyService : ServiceBase
{
onStart()
{
try
{
someQueue.BeginReceive()
}
catch(Exception e)
{
log error and throw
}
}
void someQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
try
{
//process the message
}
catch(Exception e)
{
//log
}
finally
{
someQueue.Refresh()
someQueue.BeginReceive();
}
}
}
You can check Event Viewer to know the reason of Service getting stopped.
Open start menu and start Event Viewer, in application section you'll find the error
I know years passed since this request. But I got the same issue and no solution worked for me, other than the following.
In my case, the Event Viewer was not giving any details about the crash, and all user rights were ok.
the issue was a line of code in the OnStart method:
Debugger.Launch();
It was trying to launch the debugger, but that was not installed on that server. As such, it never completed and no exception was caught by my code.
Got rid of that, and it started working.

Resources