navid/veins / commit

fbccfbdb04ba

Navid M <navidmomtahen@gmail.com> · 2026-11-26

parents: 171992e32d6e

Browse files at this commit · Download source

all: make these errors more descriptive
--- a/veins/src/cli/clone_cmd.sx+++ b/veins/src/cli/clone_cmd.sx@@ -58,7 +58,7 @@     return out } -fn send_chunks(c: ref client.Client, repo: layout.Repo, all: list[bytes.Bytes]) bool = trust {+fn send_chunks(c: mut ref client.Client, repo: layout.Repo, all: list[bytes.Bytes]) bool = trust {     val n: usize = @len(all)     val off: mut usize = 0     for off < n {@@ -84,6 +84,20 @@         off = lim     }     return true+}++fn report_client_error(c: ref client.Client, context: bytes.Bytes) void = trust {+    val detail: bytes.Bytes = client.error_text(c)+    cmsg.error_prefix()+    if detail.len > 0 {+        stdio.eprint("{s}: {s}\n", {+            str.view(context.ptr, context.len as i64),+            str.view(detail.ptr, detail.len as i64)+        })+    } else {+        stdio.eprint("{s}\n", {str.view(context.ptr, context.len as i64)})+    }+    bytes.free_owned(detail) }  pub fn run(args: list[ref u8], start: usize) i32 = trust {@@ -172,8 +186,7 @@     }     val refs_opt = client.list_refs(c)     if refs_opt is none {-        cmsg.error_prefix()-        stdio.eprint("cannot list refs\n", {})+        report_client_error(c, bytes.bytes(c"cannot list remote refs", 23))         client.free_client(c)         layout.free_repo(repo)         return 1@@ -202,11 +215,17 @@         return 0     }     val haves: list[bytes.Bytes] = lists.new[bytes.Bytes]()-    client.negotiate(c, wants, haves)+    if !client.negotiate(c, wants, haves) {+        report_client_error(c, bytes.bytes(c"negotiation failed", 18))+        @listfree(haves)+        transfer.free_id_bytes(wants)+        client.free_client(c)+        layout.free_repo(repo)+        return 1+    }     @listfree(haves)     if !client.fetch_skeleton(c, repo, wants) {-        cmsg.error_prefix()-        stdio.eprint("fetch failed\n", {})+        report_client_error(c, bytes.bytes(c"fetch failed", 12))         transfer.free_id_bytes(wants)         client.free_client(c)         layout.free_repo(repo)@@ -221,8 +240,7 @@     transfer.free_id_bytes(chunks)     store.close(st)     if !chunks_ok {-        cmsg.error_prefix()-        stdio.eprint("chunk fetch failed\n", {})+        report_client_error(c, bytes.bytes(c"chunk fetch failed", 18))         client.free_client(c)         layout.free_repo(repo)         return 1--- a/veins/src/cli/fetch_cmd.sx+++ b/veins/src/cli/fetch_cmd.sx@@ -14,7 +14,7 @@ val msg_refs      = use("../protocol/messages.sx") val cmsg          = use("../core/msg.sx") -fn send_chunks(c: ref client.Client, repo: layout.Repo, all: list[bytes.Bytes]) bool = trust {+fn send_chunks(c: mut ref client.Client, repo: layout.Repo, all: list[bytes.Bytes]) bool = trust {     val st: store.Store = store.open_all(repo)     val missing: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()     for x in all {@@ -51,6 +51,20 @@     }     @listfree(missing)     return true+}++fn report_client_error(c: ref client.Client, context: bytes.Bytes) void = trust {+    val detail: bytes.Bytes = client.error_text(c)+    cmsg.error_prefix()+    if detail.len > 0 {+        stdio.eprint("{s}: {s}\n", {+            str.view(context.ptr, context.len as i64),+            str.view(detail.ptr, detail.len as i64)+        })+    } else {+        stdio.eprint("{s}\n", {str.view(context.ptr, context.len as i64)})+    }+    bytes.free_owned(detail) }  pub fn run(args: list[ref u8], start: usize) i32 = trust {@@ -136,8 +150,7 @@     }     val refs_opt = client.list_refs(c)     if refs_opt is none {-        cmsg.error_prefix()-        stdio.eprint("cannot list refs\n", {})+        report_client_error(c, bytes.bytes(c"cannot list remote refs", 23))         client.free_client(c)         layout.free_repo(repo)         return 1@@ -164,14 +177,32 @@             @append(wants, bytes.Bytes.dup(bytes.bytes(r.target.ptr, 32)))         }     }-    client.negotiate(c, wants, haves)+    if @len(wants) == 0 {+        transfer.free_id_bytes(haves)+        @listfree(wants)+        store.close(st)+        msg_refs.free_refs(refs)+        client.free_client(c)+        layout.free_repo(repo)+        cmsg.info_prefix()+        stdio.print("no refs to fetch\n", {})+        return 0+    }+    if !client.negotiate(c, wants, haves) {+        report_client_error(c, bytes.bytes(c"negotiation failed", 18))+        transfer.free_id_bytes(wants)+        transfer.free_id_bytes(haves)+        store.close(st)+        client.free_client(c)+        layout.free_repo(repo)+        return 1+    }     val passed: bool = client.fetch_skeleton(c, repo, wants)     transfer.free_id_bytes(wants)     transfer.free_id_bytes(haves)     store.close(st)     if !passed {-        cmsg.error_prefix()-        stdio.eprint("fetch failed\n", {})+        report_client_error(c, bytes.bytes(c"fetch failed", 12))         client.free_client(c)         layout.free_repo(repo)         return 1@@ -189,8 +220,7 @@     @listfree(commits)     store.close(st2)     if !chunks_ok {-        cmsg.error_prefix()-        stdio.eprint("chunk fetch failed\n", {})+        report_client_error(c, bytes.bytes(c"chunk fetch failed", 18))         client.free_client(c)         layout.free_repo(repo)         return 1--- a/veins/src/cli/push_cmd.sx+++ b/veins/src/cli/push_cmd.sx@@ -15,6 +15,20 @@ val transfer      = use("../protocol/transfer.sx") val msg_refs      = use("../protocol/messages.sx") val cmsg          = use("../core/msg.sx")++fn report_client_error(c: ref client.Client, context: bytes.Bytes) void = trust {+    val detail: bytes.Bytes = client.error_text(c)+    cmsg.error_prefix()+    if detail.len > 0 {+        stdio.eprint("{s}: {s}\n", {+            str.view(context.ptr, context.len as i64),+            str.view(detail.ptr, detail.len as i64)+        })+    } else {+        stdio.eprint("{s}\n", {str.view(context.ptr, context.len as i64)})+    }+    bytes.free_owned(detail)+}  fn find_server_target(refs: list[msg_refs.RefInfo], name: bytes.Bytes) bytes.Bytes = trust {     for r in refs {@@ -153,8 +167,7 @@         }     }     if !client.push_objects(c, repo, objects) {-        cmsg.error_prefix()-        stdio.eprint("push transfer failed\n", {})+        report_client_error(c, bytes.bytes(c"push failed", 11))         client.free_client(c)         transfer.free_id_bytes(commits)         transfer.free_id_bytes(objects)@@ -162,6 +175,14 @@         return 1     }     val refs_opt = client.list_refs(c)+    if refs_opt is none {+        report_client_error(c, bytes.bytes(c"cannot list remote refs", 23))+        client.free_client(c)+        transfer.free_id_bytes(commits)+        transfer.free_id_bytes(objects)+        layout.free_repo(repo)+        return 1+    }     val old: mut bytes.Bytes = bytes.empty()     if refs_opt is some refs {         old = find_server_target(refs, bookmark)@@ -178,15 +199,18 @@     )     val pushed: bool = client.update_refs(c, updates)     msg_refs.free_updates(updates)+    if !pushed {+        report_client_error(c, bytes.bytes(c"ref update rejected", 19))+        client.free_client(c)+        transfer.free_id_bytes(commits)+        transfer.free_id_bytes(objects)+        layout.free_repo(repo)+        return 1+    }     client.free_client(c)     transfer.free_id_bytes(commits)     transfer.free_id_bytes(objects)     layout.free_repo(repo)-    if !pushed {-        cmsg.error_prefix()-        stdio.eprint("ref update rejected\n", {})-        return 1-    }     cmsg.info_prefix()     stdio.print(         "pushed {s} to {s}\n",--- a/veins/src/protocol/client.sx+++ b/veins/src/protocol/client.sx@@ -27,6 +27,7 @@     authed: mut bool     fingerprint: mut bytes.Bytes     repo_name: mut bytes.Bytes+    last_error: mut bytes.Bytes }  pub type WhoAmI = {@@ -415,6 +416,7 @@     c.authed = false     c.fingerprint = bytes.empty()     c.repo_name = bytes.Bytes.dup(repo_name)+    c.last_error = bytes.empty()     if key_sel.len > 0 {         val key_opt = keys.load(key_sel)         if key_opt is some key {@@ -476,22 +478,147 @@     }     if c.repo_name.ptr != none {         bytes.free_ptr(c.repo_name.ptr)+    }+    if c.last_error.ptr != none {+        bytes.free_ptr(c.last_error.ptr)     }     @free(c as ref void) } -fn request(c: ref Client, kind: u8, payload: bytes.Bytes) option[wire.Frame] = trust {+fn clear_error(c: mut ref Client) void = trust {+    if c.last_error.ptr != none {+        bytes.free_ptr(c.last_error.ptr)+        c.last_error = bytes.empty()+    }+}++fn set_error(c: mut ref Client, text: bytes.Bytes) void = trust {+    clear_error(c)+    c.last_error = bytes.Bytes.dup(text)+}++/// Return the most recent client-side protocol error. The result is owned.+pub fn error_text(c: ref Client) bytes.Bytes = trust {+    if c.last_error.ptr == none {+        return bytes.empty()+    }+    return bytes.Bytes.dup(c.last_error)+}++fn error_code_name(code: u8) bytes.Bytes = {+    if code == wire.ERR_UNSUPPORTED_VERSION {+        return bytes.bytes(c"unsupported protocol version", 28)+    }+    if code == wire.ERR_BAD_FRAME {+        return bytes.bytes(c"bad frame", 9)+    }+    if code == wire.ERR_NOT_FOUND {+        return bytes.bytes(c"object not found", 16)+    }+    if code == wire.ERR_HASH_MISMATCH {+        return bytes.bytes(c"hash mismatch", 13)+    }+    if code == wire.ERR_NON_CANONICAL {+        return bytes.bytes(c"non-canonical object", 20)+    }+    if code == wire.ERR_MISSING_CLOSURE {+        return bytes.bytes(c"missing object closure", 22)+    }+    if code == wire.ERR_REF_CONFLICT {+        return bytes.bytes(c"ref conflict", 12)+    }+    if code == wire.ERR_REJECTED {+        return bytes.bytes(c"request rejected", 16)+    }+    if code == wire.ERR_INTERNAL {+        return bytes.bytes(c"server internal error", 21)+    }+    if code == wire.ERR_UNAUTHENTICATED {+        return bytes.bytes(c"authentication required", 23)+    }+    if code == wire.ERR_FORBIDDEN {+        return bytes.bytes(c"permission denied", 17)+    }+    if code == wire.ERR_AUTH_FAILED {+        return bytes.bytes(c"authentication failed", 21)+    }+    return bytes.bytes(c"unknown server error", 20)+}++fn set_error_frame(c: mut ref Client, f: wire.Frame) void = trust {+    val code: u8 = wire.read_error(f.payload)+    val name: bytes.Bytes = error_code_name(code)+    val message: bytes.Bytes = wire.error_message(f.payload)+    val b: mut buf.Buf = buf.new(name.len + message.len + 32)+    buf.Buf.put_bytes(b, bytes.bytes(c"server error: ", 14))+    buf.Buf.put_bytes(b, name)+    if message.len > 0 {+        buf.Buf.put_bytes(b, bytes.bytes(c": ", 2))+        buf.Buf.put_bytes(b, message)+    }+    set_error(c, buf.Buf.view(b))+    buf.Buf.free(b)+    if message.ptr != none {+        bytes.free_owned(message)+    }+}++fn set_error_kind(c: mut ref Client, context: bytes.Bytes, kind: u8) void = trust {+    val b: mut buf.Buf = buf.new(context.len + 32)+    buf.Buf.put_bytes(b, context)+    buf.Buf.put_bytes(b, bytes.bytes(c" (message kind ", 15))+    buf.Buf.put_u8(b, kind)+    buf.Buf.put_u8(b, 41)+    set_error(c, buf.Buf.view(b))+    buf.Buf.free(b)+}++fn id_text(id: objid.Id) bytes.Bytes = trust {+    val hex: ref u8 = objid.Id.hex(id)+    val out: bytes.Bytes = bytes.Bytes.dup(bytes.from_cstr(hex))+    @free(hex as ref void)+    return out+}++fn set_error_object(c: mut ref Client, context: bytes.Bytes, id: objid.Id) void = trust {+    val hex: bytes.Bytes = id_text(id)+    val b: mut buf.Buf = buf.new(context.len + hex.len + 8)+    buf.Buf.put_bytes(b, context)+    buf.Buf.put_u8(b, 32)+    buf.Buf.put_bytes(b, hex)+    set_error(c, buf.Buf.view(b))+    buf.Buf.free(b)+    bytes.free_owned(hex)+}++fn request(c: mut ref Client, kind: u8, payload: bytes.Bytes) option[wire.Frame] = trust {     val rid: u32 = (time.unix_now_millis() & 0x7fffffff) as u32     if !wire.write_frame(c.s, kind, rid, payload) {+        set_error(c, bytes.bytes(c"connection closed while sending request", 39))         return none     }-    return wire.read_frame(c.s)+    val f_opt = wire.read_frame(c.s)+    match f_opt {+        some f => {+            if f.kind == wire.ERROR {+                set_error_frame(c, f)+                wire.free_frame(f)+                return none+            }+            return some f+        }+        none => {+            set_error(c, bytes.bytes(c"connection closed while waiting for server response", 45))+            return none+        }+    } } -pub fn list_refs(c: ref Client) option[list[msg.RefInfo]] = trust {+pub fn list_refs(c: mut ref Client) option[list[msg.RefInfo]] = trust {     val f_opt = request(c, wire.LIST_REFS, bytes.empty())     if f_opt is some f {         if f.kind != wire.REFS {+            set_error_kind(c, bytes.bytes(c"unexpected response to ref listing", 34), f.kind)             wire.free_frame(f)             return none         }@@ -504,13 +631,17 @@     return none } -pub fn negotiate(c: ref Client, wants: list[bytes.Bytes], haves: list[bytes.Bytes]) bool = trust {+pub fn negotiate(c: mut ref Client, wants: list[bytes.Bytes], haves: list[bytes.Bytes]) bool = trust {     val body: bytes.Bytes = msg.encode_negotiate(wants, haves)     val f_opt = request(c, wire.NEGOTIATE, body)     if f_opt is some f {-        val passed: bool = f.kind == wire.NEGOTIATE_OK+        if f.kind == wire.NEGOTIATE_OK {+            wire.free_frame(f)+            return true+        }+        set_error_kind(c, bytes.bytes(c"unexpected response to negotiation", 34), f.kind)         wire.free_frame(f)-        return passed+        return false     }     return false }@@ -557,7 +688,7 @@     return none } -fn receive_stream(c: ref Client, w: mut ref writer.Writer) bool = trust {+fn receive_stream(c: mut ref Client, w: mut ref writer.Writer) bool = trust {     val hash: ref void = @alloc(b3.hasher_state_size()) as ref void     b3.hasher_init(hash)     val good: mut bool = true@@ -574,6 +705,7 @@                 f = x             }             none => {+                set_error(c, bytes.bytes(c"connection closed while receiving data", 38))                 good = false                 break             }@@ -582,6 +714,7 @@             val digest: ref u8 = @alloc(32) as ref u8             b3.hasher_final(hash, digest)             if f.payload.len != 32 || !bytes.Bytes.eql(bytes.bytes(digest, 32), f.payload) {+                set_error(c, bytes.bytes(c"server sent a corrupt transfer checksum", 39))                 good = false             }             bytes.free_ptr(digest)@@ -589,11 +722,13 @@             break         }         if f.kind == wire.ERROR {+            set_error_frame(c, f)             wire.free_frame(f)             good = false             break         }         if f.kind != wire.RECORDS {+            set_error_kind(c, bytes.bytes(c"unexpected message while receiving data", 39), f.kind)             wire.free_frame(f)             good = false             break@@ -627,10 +762,11 @@     return writer.open(repo) } -pub fn fetch_skeleton(c: ref Client, repo: layout.Repo, wants: list[bytes.Bytes]) bool = trust {+pub fn fetch_skeleton(c: mut ref Client, repo: layout.Repo, wants: list[bytes.Bytes]) bool = trust {     val body: bytes.Bytes = msg.encode_ids(wants)     val rid: u32 = (time.unix_now_millis() & 0x7fffffff) as u32     if !wire.write_frame(c.s, wire.FETCH_SKELETON, rid, body) {+        set_error(c, bytes.bytes(c"connection closed while requesting objects", 42))         return false     }     val w_opt = open_writer(repo)@@ -640,6 +776,7 @@             w = x         }         none => {+            set_error(c, bytes.bytes(c"cannot open a local pack for incoming objects", 45))             return false         }     }@@ -651,7 +788,7 @@     return writer.finish(w) } -pub fn get_chunks(c: ref Client, repo: layout.Repo, ids: list[bytes.Bytes]) bool = trust {+pub fn get_chunks(c: mut ref Client, repo: layout.Repo, ids: list[bytes.Bytes]) bool = trust {     if @len(ids) == 0 {         return true     }@@ -658,6 +795,7 @@     val body: bytes.Bytes = msg.encode_ids(ids)     val rid: u32 = (time.unix_now_millis() & 0x7fffffff) as u32     if !wire.write_frame(c.s, wire.GET_CHUNKS, rid, body) {+        set_error(c, bytes.bytes(c"connection closed while requesting chunks", 41))         return false     }     val w_opt = open_writer(repo)@@ -667,6 +805,7 @@             w = x         }         none => {+            set_error(c, bytes.bytes(c"cannot open a local pack for incoming chunks", 44))             return false         }     }@@ -678,7 +817,7 @@     return writer.finish(w) } -pub fn have_query(c: ref Client, ids: list[bytes.Bytes]) option[list[u64]] = trust {+pub fn have_query(c: mut ref Client, ids: list[bytes.Bytes]) option[list[u64]] = trust {     val body: bytes.Bytes = msg.encode_ids(ids)     val f_opt = request(c, wire.HAVE_QUERY, body)     if f_opt is some f {@@ -716,9 +855,10 @@     return out } -pub fn push_objects(c: ref Client, repo: layout.Repo, ids: list[objid.Id]) bool = trust {+pub fn push_objects(c: mut ref Client, repo: layout.Repo, ids: list[objid.Id]) bool = trust {     val rid: u32 = (time.unix_now_millis() & 0x7fffffff) as u32     if !wire.write_frame(c.s, wire.PUT_RECORDS, rid, bytes.empty()) {+        set_error(c, bytes.bytes(c"connection closed while starting push", 37))         return false     }     val st: store.Store = store.open_all(repo)@@ -728,6 +868,7 @@     for id in ids {         val kind: u8 = type_of(st.handles, id)         if kind == 0 {+            set_error_object(c, bytes.bytes(c"local object is missing from the store:", 39), id)             good = false             break         }@@ -736,6 +877,7 @@         if body_opt is some b {             body = b         } else {+            set_error_object(c, bytes.bytes(c"cannot read local object from the store:", 40), id)             good = false             break         }@@ -742,6 +884,8 @@         val rec: bytes.Bytes = make_record(id, kind, body)         b3.hasher_update(hash, rec.ptr, rec.len)         if !wire.write_frame(c.s, wire.RECORDS, rid, rec) {+            set_error_object(c, bytes.bytes(c"connection closed while sending object:", 39), id)+            files_free(body)             good = false             break         }@@ -750,7 +894,10 @@     val digest: ref u8 = @alloc(32) as ref u8     b3.hasher_final(hash, digest)     if good {-        wire.write_frame(c.s, wire.END, rid, bytes.bytes(digest, 32))+        if !wire.write_frame(c.s, wire.END, rid, bytes.bytes(digest, 32)) {+            set_error(c, bytes.bytes(c"connection closed while finishing push", 39))+            good = false+        }     } else {         bytes.free_ptr(digest)     }@@ -760,11 +907,35 @@         return false     }     val ack = wire.read_frame(c.s)-    if ack is some f {-        val passed: bool = f.kind == wire.END+    if ack is none {+        set_error(c, bytes.bytes(c"server closed the connection before acknowledging the push", 58))+        return false+    }+    val f: mut wire.Frame = wire.Frame{+        kind: 0,+        request_id: 0,+        payload: bytes.empty(),+        base: bytes.empty()+    }+    match ack {+        some x => {+            f = x+        }+        none => {+            return false+        }+    }+    if f.kind == wire.END {         wire.free_frame(f)-        return passed+        return true+    }+    if f.kind == wire.ERROR {+        set_error_frame(c, f)+        wire.free_frame(f)+        return false     }+    set_error_kind(c, bytes.bytes(c"unexpected response to object push", 34), f.kind)+    wire.free_frame(f)     return false } @@ -774,13 +945,17 @@     } } -pub fn update_refs(c: ref Client, updates: list[msg.UpdateRef]) bool = trust {+pub fn update_refs(c: mut ref Client, updates: list[msg.UpdateRef]) bool = trust {     val body: bytes.Bytes = msg.encode_update_refs(updates)     val f_opt = request(c, wire.UPDATE_REFS, body)     if f_opt is some f {-        val passed: bool = f.kind == wire.UPDATE_REFS_OK+        if f.kind == wire.UPDATE_REFS_OK {+            wire.free_frame(f)+            return true+        }+        set_error_kind(c, bytes.bytes(c"unexpected response to ref update", 33), f.kind)         wire.free_frame(f)-        return passed+        return false     }     return false }@@ -852,4 +1027,31 @@         bytes.free_ptr(u.path.ptr)     }     return out+}++test "client formats server error frames" {+    trust {+        val c: mut ref Client = @alloc(@sizeof(Client)) as ref Client+        c.last_error = bytes.empty()+        val b: mut buf.Buf = buf.new(64)+        buf.Buf.put_u8(b, wire.ERR_FORBIDDEN)+        buf.Buf.put_length_prefixed(b, bytes.bytes(c"write role required", 19))+        val payload: bytes.Bytes = bytes.Bytes.dup(buf.Buf.view(b))+        val f: wire.Frame = wire.Frame{+            kind: wire.ERROR,+            request_id: 1,+            payload: payload,+            base: bytes.empty()+        }+        set_error_frame(c, f)+        val expected: bytes.Bytes = bytes.bytes(+            c"server error: permission denied: write role required",+            52+        )+        assert bytes.Bytes.eql(c.last_error, expected)+        clear_error(c)+        bytes.free_owned(payload)+        buf.Buf.free(b)+        @free(c as ref void)+    } }--- a/veins/src/protocol/wire.sx+++ b/veins/src/protocol/wire.sx@@ -180,6 +180,27 @@         return ERR_INTERNAL     }     return bytes.Bytes.at(payload, 0)+}++/// Decode the human-readable part of an ERROR payload. The returned bytes are+/// owned by the caller.+pub fn error_message(payload: bytes.Bytes) bytes.Bytes = trust {+    if payload.len < 2 {+        return bytes.empty()+    }+    val r: mut reader.Reader = reader.new(+        @ptradd(payload.ptr, 1) as ref u8,+        payload.len - 1+    )+    val decoded = reader.Reader.read_bytes(r, 4096)+    match decoded {+        ok v => {+            return bytes.Bytes.dup(v)+        }+        err _ => {+            return bytes.empty()+        }+    } }  pub fn put_bytes_list(b: mut buf.Buf, xs: list[bytes.Bytes]) void = trust {@@ -239,4 +260,19 @@         }     }     @listfree(xs)+}++test "error payloads expose code and message" {+    trust {+        val b: mut buf.Buf = buf.new(64)+        buf.Buf.put_u8(b, ERR_FORBIDDEN)+        buf.Buf.put_length_prefixed(b, bytes.bytes(c"write role required", 19))+        val payload: bytes.Bytes = bytes.Bytes.dup(buf.Buf.view(b))+        assert read_error(payload) == ERR_FORBIDDEN+        val message: bytes.Bytes = error_message(payload)+        assert bytes.Bytes.eql(message, bytes.bytes(c"write role required", 19))+        bytes.free_owned(message)+        bytes.free_owned(payload)+        buf.Buf.free(b)+    } }