60172ff
Branches > refactor/use-spec-1 > 60172ff > files / LICENSE
Add defn! and update serverbound message error reporting
Commit: 60172ff6138bb7ea03962ba3742dc2b0286c6485
Parent: a46b022
2 changed files:
src/dev/grahp/error.clj view file
@@ -87,4 +87,19 @@ k# ~k] (if (contains? m# k#) (get m# k#)- (raise ~msg {:map m# :key k#})))))+ (raise ~msg {:map m# :key k#}))))+ ([m k msg data-map]+ `(let [m# ~m+ k# ~k]+ (if (contains? m# k#)+ (get m# k#)+ (raise ~msg (merge {:map m# :key k#} ~data-map))))))++(defmacro defn!+ {:clj-kondo/lint-as 'clojure.core/defn}+ [fn-name docstring args & body]+ (assert (and (vector? args) (every? simple-symbol? args)))+ `(defn ~fn-name ~docstring ~args+ (adorn ~@body+ (str "Error while in function '" (ns-name *ns*) "/" '~fn-name "'")+ {:args ~(zipmap (map keyword args) args)})))src/net/bowmap/network/message/server.clj view file
@@ -2,12 +2,12 @@ (:require [clojure.alpha.spec :as s] [dev.grahp.build :refer [build]]- [dev.grahp.error :refer [adorn raise]]+ [dev.grahp.error :refer [adorn defn! get!]] [net.bowmap.network.message :as msg] [net.bowmap.network.packet :as pk] [net.bowmap.network.packet.read :refer [->bit-mask ->byte ->double ->enum ->float ->long ->short ->string- ->table ->ubyte ->uuid ->varint byte->]]+ ->table ->ubyte ->uuid ->varint]] [net.bowmap.network.state :as state]) (:import [io.netty.buffer ByteBuf]))@@ -174,7 +174,7 @@ :pitch ->float :flags ->byte]})) -(defn packet->message+(defn! packet->message "Deserializes one (framed, serialized) packet into a message. The initial length field of the packet should be stripped from the buffer. Can throw if:@@ -183,14 +183,10 @@ 3. 'buf' is not properly framed 4. The contents of 'buf' are not a valid packet" [packet state]- (-> (let [numeric-id (->varint packet)- packet-id (packet-ids {::state/connection-state state ::pk/id numeric-id})- reader (packets packet-id)]- (if reader- (adorn (reader packet)- "Error while calling packet reader" {:numeric-id numeric-id :packet-id packet-id :reader reader})- (raise "Failed to read message, no such message with that id." {:id numeric-id :state state :packet-id packet-id})))- (adorn "Error while turning serverbound packet into message" {:buf packet :state state})))+ (let [numeric-id (->varint packet)+ message-id (packet-ids {::state/connection-state state ::pk/id numeric-id})+ reader (get! packets message-id "No such message with that id!" {:state state})]+ (adorn (reader packet) "Error while calling packet reader" {:numeric-id numeric-id :packet-id message-id :reader reader}))) (s/fdef packet->message :args (s/cat :packet ::pk/packet :state ::state/connection-state)