From d34e15c50b957bf037663c7611c6d686a09ad241 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 24 May 2026 08:13:02 +0000 Subject: object: Fix error handling --- object/parse.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'object/parse.go') diff --git a/object/parse.go b/object/parse.go index 3a2b5eaa..a4d0d350 100644 --- a/object/parse.go +++ b/object/parse.go @@ -12,7 +12,23 @@ import ( // "codeberg.org/lindenii/furgit/object/tree" ) -// ParseWithHeader parses a loose object in "type size\x00body" format. +// SizeMismatchError indicates a mismatch +// between the size expected from the object header +// and the size of the object. +type SizeMismatchError struct { + Expected int + Got int +} + +func (sizeMismatchError SizeMismatchError) Error() string { + return fmt.Sprintf( + "object: size mismatch: header says %d bytes, but got %d from body", + sizeMismatchError.Expected, sizeMismatchError.Got, + ) +} + +// ParseWithHeader parses a loose object +// in "type size\x00body" format. // //nolint:ireturn func ParseWithHeader(raw []byte, algo id.Algorithm) (Object, error) { @@ -23,7 +39,7 @@ func ParseWithHeader(raw []byte, algo id.Algorithm) (Object, error) { body := raw[headerLen:] if uint64(len(body)) != size { - return nil, fmt.Errorf("object: size mismatch: header says %d bytes, body has %d", size, len(body)) + return nil, SizeMismatchError{Expected: int(size), Got: len(body)} } return ParseWithoutHeader(ty, body, algo) @@ -35,14 +51,14 @@ func ParseWithHeader(raw []byte, algo id.Algorithm) (Object, error) { func ParseWithoutHeader(ty typ.Type, body []byte, algo id.Algorithm) (Object, error) { switch ty { case typ.TypeBlob: - return blob.Parse(body) + return blob.Parse(body) //nolint:wrapcheck case typ.TypeTree: panic("TODO") case typ.TypeCommit: - return commit.Parse(body, algo) + return commit.Parse(body, algo) //nolint:wrapcheck case typ.TypeTag: panic("TODO") default: - return nil, fmt.Errorf("object: unsupported object type %d", ty) + return nil, typ.ErrInvalidType } } -- cgit v1.3.1-10-gc9f91