Skip to content

Commit

Permalink
runtime data errors are exceptions not asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
hazeycode authored and ethicnology committed May 24, 2023
1 parent 44cb1e2 commit ef33ec6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/src/close.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class Close {
/// Deserialize a nostr close message
/// - ["CLOSE", subscription_id]
Close.deserialize(input) {
assert(input.length == 2);
if (input.length != 2) {
throw 'Invalid length for CLOSE message';
}
subscriptionId = input[1];
}
}
4 changes: 3 additions & 1 deletion lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ class Event {
bool verify = true,
}) {
pubkey = pubkey.toLowerCase();
if (verify) assert(isValid() == true);
if (verify && isValid() == false) {
throw 'Invalid event';
}
}

/// Partial constructor, you have to fill the fields yourself
Expand Down
4 changes: 3 additions & 1 deletion lib/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Message {
Message.deserialize(String payload) {
dynamic data = jsonDecode(payload);
var messages = ["EVENT", "REQ", "CLOSE", "NOTICE", "EOSE", "OK", "AUTH"];
assert(messages.contains(data[0]), "Unsupported payload (or NIP)");
if (messages.contains(data[0]) == false) {
throw 'Unsupported payload (or NIP)';
}

type = data[0];
switch (type) {
Expand Down
4 changes: 3 additions & 1 deletion lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class Request {
/// Deserialize a nostr request message
/// - ["REQ", subscription_id, filter JSON, filter JSON, ...]
Request.deserialize(input) {
assert(input.length >= 3);
if (input.length < 3) {
throw 'Message too short';
}
subscriptionId = input[1];
filters = [];
for (var i = 2; i < input.length; i++) {
Expand Down

0 comments on commit ef33ec6

Please sign in to comment.