I'm using 3rd party library Cached network image, sometime it throws network errors, given these errors are thrown by a widget, asynchronously, how can I catch them?
or in general, if a 3rd party widget throws an exception, is there a way to catch it?
In CachedNetworkImage, you will get a callback errorWidget which will be returned only when a network error occurs or the URL is incorrect. You can return any widget that you want when the error occurs.
You cannot catch an error in CachedNetworkImage.
You can use Connectivity package to check for network related issues:
Link
Related
Please explain to me what is the difference between them. Can they be used together or only separately?
FlutterError.onError is a method that allows you to hook into the Flutter framework's error-handling mechanism, so you can receive callbacks when an error occurs during the build, layout, or painting phases.
runZonedGuarded is a utility function that allows you to run a block of code in a new zone with a custom error handler. The onError parameter is the error handler that will be called if an error occurs within the zone.
So, in summary:
FlutterError.onError is specific to the Flutter framework and is used
to handle errors during the build, layout, and painting phases.
runZonedGuarded.onError is more general and allows you to run a block
of code in a new zone with a custom error handler, which can be used
for a wider range of error-handling needs.
Yes, FlutterError.onError and runZonedGuarded.onError can be used together in a Flutter application.
For example, you can use FlutterError.onError to handle errors that occur during the build, layout, or painting phases, and use runZonedGuarded.onError to handle errors that occur within a specific block of code.
In this way, you can have multiple error handlers in your Flutter application to handle different types of errors in different parts of the code.
How to implement an error monitoring in audio_service?
It runs in isolate, but none of methods available expose Isolate to add error listener. Thus is there any other method for hooking it to error monitoring platform like ex. sentry?
Good news and bad news.
When the UI isolate is present and initiates the method call to the background isolate, any exception generated by the background task will be forwarded to the UI isolate where you can create an error zone or use something like sentry. For example, an error thrown by onPlay in the background isolate will be forwarded and thrown by play in the main UI isolate.
However, when the UI isolate is not around, or the media notification initiates the method call to the background isolate, the exception will be caught and discarded internally.
Making the latter case an uncaught exception would make a good feature request. Although note that the next version of audio_service will run under a single isolate which would also solve the problem.
I love SignalR and currently doing a few apps in it. Since SignalR is async if a logic error occurs on the service what is the best practice to handle that error?
I have been doing invoke to the client with an error object and adding it to a Reactive Extension observable, but that seems a little awkward.
The client is .Net Core and Blazor.
Suggestions?
SignalR is async if a logic error occurs on the service what is the best practice to handle that error?
ASP.NET Core SignalR provides built-in diagnostics logging feature that could help capture and log useful transports and Hub related information, which could help troubleshoot the issue.
Besides, from this doc, we can find:
Exceptions often contain sensitive information, such as connection information. Because of this, SignalR does not expose the details of exceptions that occur on the server to the client. However, instances of HubExceptionare sent to the client.
If you'd like to handle logic error occurs in Hub method, you can try to wrap the code logic in a try-catch block, then manually write exception in log.
Or you can create a new HubException instance with specified error message, which would be detected by client invoking this hub method.
try
{
//code logic here
//...
//...
//Convert.ToDateTime("hello");
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
catch (Exception ex)
{
throw new HubException(ex.Message);
}
I'm using ACRA in my application and can happily catch deliberately thrown exceptions in the UI code.
However if I throw the same type of exception in my background service ACRA does not catch and report the exception.
Should I expect ACRA to catch exceptions in a background service? If so do I need to do any additional configuration beyond the following which is in my application class attachBaseCOntext() method
CoreConfigurationBuilder builder = new CoreConfigurationBuilder(this);
builder.setBuildConfigClass(BuildConfig.class).setReportFormat(StringFormat.JSON);
builder.getPluginConfigurationBuilder(MailSenderConfigurationBuilder.class).setEnabled(true).setMailTo("kevin#electricpocket.com").setReportAsFile(true);
builder.getPluginConfigurationBuilder(ToastConfigurationBuilder.class).setEnabled(true).setResText(R.string.acra_toast_text);
ACRA.init(this, builder);
ACRA will only catch exceptions for the thread in which it is initialized.
I suspect your background service is running in another thread, and hence needs to initialize ACRA itself.
In advance, please excuse my lack of understanding for iPhone/Objective-C Best Practices; I come from a .NET/C# background.
Although, I've read many posts regarding exception handling for the iPhone, I'm still not exactly clear on what most people do for production code. Also, I haven't been able to find any open source apps with error handling that I would normally expect. Here are my questions:
1) If an unexpected result occurs that would cause the application to eventually fail, would you throw an exception or just wait for it to fail later? For example,
if (![fileManager createDirectoryAtPath: myNewDir
withIntermediateDirectories: YES
attributes: nil
error: &myError]) {
// My app shouldn't continue. Should I raise an exception?
// Or should I just log it and then wait for it to crash later?
}
2) Do you validate parameters? For example, in C# I would usually check for null, and if needed throw an ArgumentNullException.
3) When an app crashes, does the crash info get logged automatically or do I need to set the unhandled exception handler? Can I show a UIAlertView within this method to inform the user something bad happened, instead of having the app just disappear? (If so, I couldn't get it to work.)
4) And finally, why don't I see anyone actually using #try/#catch/#finally? It's used extensively in C#, but I haven't found open source apps using it. (Maybe I'm just looking at the wrong apps.)
Ad 1. The example you give is a somewhat classical example of when one should use a "checked exception" in Java. The caller of the method has to be prepared, that the call can fail for reasons which are outside of what it can control. In particular, in the example given, I would allow the error descriptor object allow to propagate back to the caller:
- (BOOL) prepareDirectories: (NSString*) path error: (NSError**) location {
if( ![fileManager createDirectoryAtPath: path
withIntermediateDirectories: YES
attributes: nil
error: location] ) {
return NO;
}
// ... additional set-up here
return YES;
}
If the error is really not recoverable, you may actually raise an exception, but that will also forfeit any chance to show a proper error message to the user of your application.
Ad 2. Yes, parameter validation is definitely something you should do. And raising an exception for unexpected nils is entirely appropriate. A bad parameter is a "programmer's error" and you cannot guarantee, that the program is still in a consistent state. For example, NSArray raises an exception, if you try to get an element using an non-existent index.
Ad 3. When the app crashes due to an uncaught exception, the fact is logged automatically. You can catch the exception if you really want to display an error message. However, the app is likely to be in an inconsistent state, and should not be allowed to continue, so simply allowing it to go down seems the best solution here.
Ad 4. I don't know.
In the specific case you mention, you should present a notification to the user about what just happened with instructions on how to fix the situation. Except in extreme circumstances your app should not quit. You should let the user fix whatever is wrong, then try again.
Parameter validation depends on a lot of factors. One thing to keep in mind is that in Obj-C it's perfectly valid to send messages to nil (nothing happens if you do) so in a lot of situations you don't need to check for nil. If I'm in a model class I always validate parameters in methods. If I'm not I almost never validate anything, type checking is good enough.
Some information should be logged but it's always helpful to log more specific information to your situation. Ideally you shouldn't ever reach the unhandled exception state.
Cocoa classes will rarely throw exceptions for environmental reasons, they are almost always because you did something wrong. For example, you wouldn't usually put a #try/#catch block around [NSString stringWithString:] because the only way it'll throw an exception is if you try and pass nil. Make sure you aren't passing nil and you won't have to worry about catching exceptions. When a method might fail because of something outside of your control, they usually communicate via an NSError. See the - (BOOL)save:(NSError **)error method of NSManagedObjectContext for example.