centrifugal / centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
https://pub.dartlang.org/packages/centrifuge
MIT License
113 stars 34 forks source link

How to catch exception on publish? #60

Closed SimonVillage closed 2 years ago

SimonVillage commented 2 years ago

My publish response is throwing an exception. I am trying to catch the exception but it doesn't work:

    try {
      subscription.publish(data);
    } catch (e) {
      print(e);
    }
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Error{code: 0, message: Points already collected within 24h}
#0      Transport._processResult
package:centrifuge/src/transport.dart:148
#1      Transport.sendMessage
package:centrifuge/src/transport.dart:95
<asynchronous suspension>
#2      ClientImpl.publish
package:centrifuge/src/client.dart:183
<asynchronous suspension>

I return the error like this on my backend:

{ error: { message: e.message } }

I wonder if it would be better to not return an error but something like a custom error to now have it convert into an exception? Something like

{ result: { data: { success: false, message: "Foo" } } };
SimonVillage commented 2 years ago
    try {
      subscription.publish(data);
    } on Error catch (e) {
      print(e);
    }

You have to define on Error then it will work. Would be better to have a different naming like CentrifugeError. I will close this issue anyways.