Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed May 2, 2024
1 parent 7c344d5 commit 2c1b534
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/src/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "package:wamp/src/wsjoiner.dart";
import "package:wampproto/messages.dart";
import "package:wampproto/serializers.dart";

String getSubProtocol(Serializer serializer) {
Expand All @@ -12,3 +13,16 @@ String getSubProtocol(Serializer serializer) {
throw ArgumentError("invalid serializer");
}
}

String wampErrorString(Error err) {
String errStr = err.uri;
if (err.args.isNotEmpty) {
String args = err.args.map((arg) => arg.toString()).join(", ");
errStr += ": $args";
}
if (err.kwargs.isNotEmpty) {
String kwargs = err.kwargs.entries.map((entry) => "${entry.key}=${entry.value}").join(", ");
errStr += ": $kwargs";
}
return errStr;
}
24 changes: 24 additions & 0 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "package:wampproto/idgen.dart";
import "package:wampproto/messages.dart" as msg;
import "package:wampproto/session.dart";

import "helpers.dart";

class Session {
Session(this._baseSession) {
_wampSession = WAMPSession(serializer: _baseSession.serializer);
Expand Down Expand Up @@ -87,6 +89,28 @@ class Session {
_subscriptions.remove(request.subscriptionId);
request.future.complete();
}
} else if (message is msg.Error) {
switch (message.msgType) {
case msg.Call.id:
_callRequests.remove(message.requestID);

case msg.Register.id:
_registerRequests.remove(message.requestID);

case msg.UnRegister.id:
_unregisterRequests.remove(message.requestID);

case msg.Subscribe.id:
_subscribeRequests.remove(message.requestID);

case msg.UnSubscribe.id:
_unsubscribeRequests.remove(message.requestID);

case msg.Publish.id:
_publishRequests.remove(message.requestID);
}

throw Exception(wampErrorString(message));
}
}

Expand Down

0 comments on commit 2c1b534

Please sign in to comment.