Skip to content

Commit

Permalink
Merge pull request #36 from muzzammilshahid/handle-session-error
Browse files Browse the repository at this point in the history
session: fix exception handling in microtask
  • Loading branch information
muzzammilshahid authored Jun 1, 2024
2 parents 4dd5746 + 68e9d60 commit 72f38b1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
2 changes: 0 additions & 2 deletions example/in_memory_router.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "package:wampproto/serializers.dart";

import "package:xconn/exports.dart";
import "package:xconn/src/types.dart";

Expand Down
1 change: 1 addition & 0 deletions lib/exports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export "package:wampproto/auth.dart"
export "package:wampproto/serializers.dart" show CBORSerializer, JSONSerializer, MsgPackSerializer, Serializer;

export "src/client.dart" show Client;
export "src/exception.dart" show ApplicationError, ProtocolError;
export "src/router.dart" show Router;
export "src/server.dart" show Server;
export "src/session.dart" show Session;
Expand Down
27 changes: 27 additions & 0 deletions lib/src/exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ApplicationError implements Exception {
ApplicationError(this.message, {this.args, this.kwargs});

final String message;
final List<dynamic>? args;
final Map<String, dynamic>? kwargs;

@override
String toString() {
String errStr = message;
if (args?.isNotEmpty ?? false) {
String argsStr = args!.map((arg) => arg.toString()).join(", ");
errStr += ": $argsStr";
}
if (kwargs?.isNotEmpty ?? false) {
String kwargsStr = kwargs!.entries.map((entry) => "${entry.key}=${entry.value}").join(", ");
errStr += ": $kwargsStr";
}
return errStr;
}
}

class ProtocolError implements Exception {
ProtocolError(this.message);

final String message;
}
39 changes: 31 additions & 8 deletions lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "dart:async";
import "package:wampproto/idgen.dart";
import "package:wampproto/messages.dart" as msg;
import "package:wampproto/session.dart";
import "package:xconn/src/exception.dart";

import "package:xconn/src/helpers.dart";
import "package:xconn/src/types.dart";
Expand Down Expand Up @@ -90,30 +91,52 @@ class Session {
} else if (message is msg.Error) {
switch (message.msgType) {
case msg.Call.id:
_callRequests.remove(message.requestID);
var callRequest = _callRequests.remove(message.requestID);
callRequest?.completeError(
ApplicationError(message.uri, args: message.args, kwargs: message.kwargs),
);
break;

case msg.Register.id:
_registerRequests.remove(message.requestID);
var registerRequest = _registerRequests.remove(message.requestID);
registerRequest?.future.completeError(
ApplicationError(message.uri, args: message.args, kwargs: message.kwargs),
);
break;

case msg.UnRegister.id:
_unregisterRequests.remove(message.requestID);
var unregisterRequest = _unregisterRequests.remove(message.requestID);
unregisterRequest?.future.completeError(
ApplicationError(message.uri, args: message.args, kwargs: message.kwargs),
);
break;

case msg.Subscribe.id:
_subscribeRequests.remove(message.requestID);
var subscribeRequest = _subscribeRequests.remove(message.requestID);
subscribeRequest?.future.completeError(
ApplicationError(message.uri, args: message.args, kwargs: message.kwargs),
);
break;

case msg.UnSubscribe.id:
_unsubscribeRequests.remove(message.requestID);
var unsubscribeRequest = _unsubscribeRequests.remove(message.requestID);
unsubscribeRequest?.future.completeError(
ApplicationError(message.uri, args: message.args, kwargs: message.kwargs),
);
break;

case msg.Publish.id:
_publishRequests.remove(message.requestID);
}
var publishRequest = _publishRequests.remove(message.requestID);
publishRequest?.completeError(
ApplicationError(message.uri, args: message.args, kwargs: message.kwargs),
);
break;

throw Exception(wampErrorString(message));
default:
throw ProtocolError(wampErrorString(message));
}
} else {
throw ProtocolError("Unexpected message type ${message.runtimeType}");
}
}

Expand Down
1 change: 0 additions & 1 deletion test/router_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "package:test/test.dart";
import "package:wampproto/messages.dart" as msg;
import "package:wampproto/serializers.dart";

import "package:xconn/exports.dart";
import "package:xconn/src/types.dart";
Expand Down

0 comments on commit 72f38b1

Please sign in to comment.