Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
740 stars 429 forks source link

Better way to handle the _manage #267

Closed ericel closed 3 years ago

ericel commented 3 years ago

💬 Questions and Help

This part of the code throws an error when the file from the web doesn't exist.

Stream<FileResponse> _manageResponse(
      CacheObject cacheObject, FileServiceResponse response) async* {
    final hasNewFile = statusCodesNewFile.contains(response.statusCode);
    final keepOldFile = statusCodesFileNotChanged.contains(response.statusCode);
    if (!hasNewFile && !keepOldFile) {
      throw  HttpExceptionWithStatus(
        response.statusCode,
        'Invalid statusCode: ${response?.statusCode}',
        uri: Uri.parse(cacheObject.url),
      ); 
    }

It would be nice if a try / catch block can handle the error. What would be a better way to silence this error... I tried

 try {
      var file = await instance.getSingleFile(url, key: key, headers: headers);
      return file;
    } catch (e) {
      print(e.string);
     return null;
    }

That doesn't seem to handle the error.

renefloor commented 3 years ago

I would expect the try-catch block to work. What's happening if you do that? Does the app crash?

ericel commented 3 years ago

I would expect the try-catch block to work. What's happening if you do that? Does the app crash?

Yes app crashes

renefloor commented 3 years ago

I tried your code, funny thing is it crashing in your print statement (print(e.string);):

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: 
Class 'SocketException' has no instance getter 'string'.
Receiver: Instance of 'SocketException'
Tried calling: string
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)

Replacing that with print(e) just prints the error and returns null. My complete test example: https://gist.github.com/renefloor/2a1593a2431306093d707cbccacb4e92

ericel commented 3 years ago

I tried your code, funny thing is it crashing in your print statement (print(e.string);):

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: 
Class 'SocketException' has no instance getter 'string'.
Receiver: Instance of 'SocketException'
Tried calling: string
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)

Replacing that with print(e) just prints the error and returns null. My complete test example: https://gist.github.com/renefloor/2a1593a2431306093d707cbccacb4e92

That was a typo error when reporting the issue here, it should be a e.toString(). It doesn't really solve this:

Screen Shot 2021-01-15 at 12 26 54 AM
renefloor commented 3 years ago

That looks like the debugger that stops. The Dart VM debugger has a bug that it doesn't always know that an exception is caught. See this recent comment, but the debate is already quite old: https://github.com/dart-lang/sdk/issues/37953#issuecomment-760256848

Is it correct that your debugger pauses, but your app doesn't crash?

ericel commented 3 years ago

Right! thank you.