From ef33ec63b14503510fe21e510dd4312764e2c46c Mon Sep 17 00:00:00 2001 From: hazeycode <22148308+hazeycode@users.noreply.github.com> Date: Sun, 21 May 2023 21:16:30 +0100 Subject: [PATCH] runtime data errors are exceptions not asserts --- lib/src/close.dart | 4 +++- lib/src/event.dart | 4 +++- lib/src/message.dart | 4 +++- lib/src/request.dart | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/src/close.dart b/lib/src/close.dart index fbbbe7a..34f08ba 100644 --- a/lib/src/close.dart +++ b/lib/src/close.dart @@ -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]; } } diff --git a/lib/src/event.dart b/lib/src/event.dart index 96f7c25..c3c9de6 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -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 diff --git a/lib/src/message.dart b/lib/src/message.dart index 99acf5a..a02cfcd 100644 --- a/lib/src/message.dart +++ b/lib/src/message.dart @@ -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) { diff --git a/lib/src/request.dart b/lib/src/request.dart index 26ca262..e229809 100644 --- a/lib/src/request.dart +++ b/lib/src/request.dart @@ -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++) {