diff options
| author | 2026-05-24 08:13:02 +0000 | |
|---|---|---|
| committer | 2026-05-24 08:13:54 +0000 | |
| commit | d34e15c50b957bf037663c7611c6d686a09ad241 (patch) | |
| tree | 5a39744fefdf4f9be8cd8d5aba139db78c877f5d | |
| parent | object: Name parameters (diff) | |
| signature | No signature | |
object: Fix error handling
| -rw-r--r-- | object/parse.go | 26 |
1 files changed, 21 insertions, 5 deletions
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 } } |
