55177a2

Branches > main > 55177a2 > files / src / net / bowmap / core.clj

Improve error messages of trying to encode bad packets

Grahp, 2026-03-08

Commit: 55177a21642c6a57adeec2f988f4a165b7d8bb00
Parent: 9348299

2 changed files:

src/net/bowmap/packet/client.clj modified (view file)

@@ -3,7 +3,7 @@   (:require    [clojure.alpha.spec :as s]    [dev.grahp.build :refer [build]]-   [dev.grahp.error :refer [assert!]]+   [dev.grahp.error :refer [assert! raise]]    [net.bowmap.client.state :as state]    [net.bowmap.packet :as pk]    [net.bowmap.packet.write :as write])@@ -229,14 +229,7 @@   (packets (::pk/id pk)))  ;; Maybe this should just be a function of the packet :p-(defn encode-packet-  "Encodes packet 'pk' into a (new) ByteBuf of the packet bytes.-   Like decoding, encoding a packet also requires the connection state, as-    packet ids are not unique across connection state, so there isn't a-    direct mapping of packet ids to numeric ids.-   There are several packets (disconnect, keep alive) that are logically the-    same across connection states, and so can be sent without needing to switch-    on the connection state."+(defn- encode-packet*   [pk state]   {:pre [(pk/packet? pk)]}   (let [id (assert! (numeric-id state (::pk/id pk)) {:pk pk :state state})@@ -249,3 +242,16 @@     (-> (Unpooled/buffer (+ (ByteBuf/.writerIndex data-buf) 5))         (write/write-varint! (ByteBuf/.writerIndex data-buf))         (ByteBuf/.writeBytes data-buf))))++(defn encode-packet+  "Encodes packet 'pk' into a (new) ByteBuf of the packet bytes.+   Like decoding, encoding a packet also requires the connection state, as+    packet ids are not unique across connection state, so there isn't a+    direct mapping of packet ids to numeric ids.+   There are several packets (disconnect, keep alive) that are logically the+    same across connection states, and so can be sent without needing to switch+    on the connection state."+  [pk state]+  (try (encode-packet* pk state)+       (catch Throwable e+         (raise "Error while encoding packet:" {:pk pk :state state} e))))

src/net/bowmap/packet/write.clj modified (view file)

@@ -45,7 +45,10 @@ ;; TODO this is where optionality and nil checking should be, and the packet stuff should use this. (defn write-table! [buf codec-fields m]   (reduce (fn [acc {:keys [field codec]}]-            (codec acc (get m field)))+            (try+              (codec acc (get m field))+              (catch Throwable e+                (raise "Error while encoding packet field" {:field field :map m} e))))           buf codec-fields))  (declare writers)