Skip to content

Commit

Permalink
Move out other errors, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
bennetthardwick committed Oct 11, 2024
1 parent 8286263 commit 0f35709
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 91 deletions.
24 changes: 0 additions & 24 deletions go/mcap/error.go

This file was deleted.

90 changes: 90 additions & 0 deletions go/mcap/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package mcap

import (
"errors"
"fmt"
"io"
)

var ErrBadOffset = errors.New("invalid offset")
var ErrMetadataNotFound = errors.New("metadata not found")

// ErrNestedChunk indicates the lexer has detected a nested chunk.
var ErrNestedChunk = errors.New("detected nested chunk")
var ErrChunkTooLarge = errors.New("chunk exceeds configured maximum size")
var ErrRecordTooLarge = errors.New("record exceeds configured maximum size")
var ErrInvalidZeroOpcode = errors.New("invalid zero opcode")

// ErrUnknownSchema is returned when a schema ID is not known to the writer.
var ErrUnknownSchema = errors.New("unknown schema")

// ErrAttachmentDataSizeIncorrect is returned when the length of a written
// attachment does not match the length supplied.
var ErrAttachmentDataSizeIncorrect = errors.New("attachment content length incorrect")

var ErrLengthOutOfRange = errors.New("length out of int32 range")

// ErrUnexpectedToken indicated when an unexpected token was found in an MCAP file.
type ErrUnexpectedToken struct {
err error
}

func NewErrUnexpectedToken(err error) error {
return &ErrUnexpectedToken{err}
}

func (e *ErrUnexpectedToken) Error() string {
return e.err.Error()
}

func (e *ErrUnexpectedToken) Is(target error) bool {
var err *ErrUnexpectedToken
if errors.As(target, &err) {
return true
}
return errors.Is(e.err, target)
}

// ErrBadMagic indicates invalid magic bytes were detected.
type ErrBadMagic struct {
location magicLocation
actual []byte
}

func (e *ErrBadMagic) Error() string {
return fmt.Sprintf("Invalid magic at %s of file, found: %v", e.location, e.actual)
}

func (e *ErrBadMagic) Is(err error) bool {
_, ok := err.(*ErrBadMagic)
return ok
}

// ErrTruncatedRecord indicates not enough data was available to parse a certain record.
type ErrTruncatedRecord struct {
opcode OpCode
actualLen int
expectedLen uint64
}

func (e *ErrTruncatedRecord) Error() string {
if e.expectedLen == 0 {
return fmt.Sprintf(
"MCAP truncated in record length field after %s opcode (%x), received %d bytes",
e.opcode.String(),
byte(e.opcode),
e.actualLen,
)
}
return fmt.Sprintf(
"MCAP truncated in %s (0x%x) record content with expected length %d, data ended after %d bytes",
e.opcode.String(),
byte(e.opcode),
e.expectedLen,
e.actualLen,
)
}

func (e *ErrTruncatedRecord) Unwrap() error {
return io.ErrUnexpectedEOF
}
3 changes: 0 additions & 3 deletions go/mcap/indexed_message_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math/bits"
Expand All @@ -17,8 +16,6 @@ import (
"github.com/pierrec/lz4/v4"
)

var ErrBadOffset = errors.New("invalid offset")

const (
chunkBufferGrowthMultiple = 1.2
)
Expand Down
49 changes: 0 additions & 49 deletions go/mcap/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
"github.com/pierrec/lz4/v4"
)

// ErrNestedChunk indicates the lexer has detected a nested chunk.
var ErrNestedChunk = errors.New("detected nested chunk")
var ErrChunkTooLarge = errors.New("chunk exceeds configured maximum size")
var ErrRecordTooLarge = errors.New("record exceeds configured maximum size")
var ErrInvalidZeroOpcode = errors.New("invalid zero opcode")

type errInvalidChunkCrc struct {
expected uint32
actual uint32
Expand All @@ -27,34 +21,6 @@ func (e *errInvalidChunkCrc) Error() string {
return fmt.Sprintf("invalid chunk CRC: %x != %x", e.actual, e.expected)
}

type ErrTruncatedRecord struct {
opcode OpCode
actualLen int
expectedLen uint64
}

func (e *ErrTruncatedRecord) Error() string {
if e.expectedLen == 0 {
return fmt.Sprintf(
"MCAP truncated in record length field after %s opcode (%x), received %d bytes",
e.opcode.String(),
byte(e.opcode),
e.actualLen,
)
}
return fmt.Sprintf(
"MCAP truncated in %s (0x%x) record content with expected length %d, data ended after %d bytes",
e.opcode.String(),
byte(e.opcode),
e.expectedLen,
e.actualLen,
)
}

func (e *ErrTruncatedRecord) Unwrap() error {
return io.ErrUnexpectedEOF
}

type magicLocation int

const (
Expand All @@ -73,21 +39,6 @@ func (m magicLocation) String() string {
}
}

// ErrBadMagic indicates invalid magic bytes were detected.
type ErrBadMagic struct {
location magicLocation
actual []byte
}

func (e *ErrBadMagic) Error() string {
return fmt.Sprintf("Invalid magic at %s of file, found: %v", e.location, e.actual)
}

func (e *ErrBadMagic) Is(err error) bool {
_, ok := err.(*ErrBadMagic)
return ok
}

const (
// TokenHeader represents a header token.
TokenHeader TokenType = iota
Expand Down
3 changes: 0 additions & 3 deletions go/mcap/mcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mcap

import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
Expand All @@ -11,8 +10,6 @@ import (
// Magic is the magic number for an MCAP file.
var Magic = []byte{0x89, 'M', 'C', 'A', 'P', 0x30, '\r', '\n'}

var ErrLengthOutOfRange = errors.New("length out of int32 range")

const (
// CompressionZSTD represents zstd compression.
CompressionZSTD CompressionFormat = "zstd"
Expand Down
6 changes: 2 additions & 4 deletions go/mcap/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"math"
)

var ErrMetadataNotFound = errors.New("metadata not found")

func getPrefixedString(data []byte, offset int) (s string, newoffset int, err error) {
if len(data[offset:]) < 4 {
return "", 0, io.ErrShortBuffer
Expand Down Expand Up @@ -231,7 +229,7 @@ func (r *Reader) GetMetadata(offset uint64) (*Metadata, error) {
return nil, err
}
if token != TokenMetadata {
return nil, NewUnexpectedTokenError(fmt.Errorf("expected metadata record, found %v", data))
return nil, NewErrUnexpectedToken(fmt.Errorf("expected metadata record, found %v", data))
}
metadata, err := ParseMetadata(data)
if err != nil {
Expand Down Expand Up @@ -262,7 +260,7 @@ func NewReader(r io.Reader) (*Reader, error) {
return nil, fmt.Errorf("could not read MCAP header when opening reader: %w", err)
}
if token != TokenHeader {
return nil, NewUnexpectedTokenError(fmt.Errorf("expected first record in MCAP to be a Header, found %v", headerData))
return nil, NewErrUnexpectedToken(fmt.Errorf("expected first record in MCAP to be a Header, found %v", headerData))
}
header, err := ParseHeader(headerData)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go/mcap/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mcap

// Version of the MCAP library.
var Version = "v1.6.0"
var Version = "v1.7.0"
7 changes: 0 additions & 7 deletions go/mcap/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ import (
"github.com/pierrec/lz4/v4"
)

// ErrUnknownSchema is returned when a schema ID is not known to the writer.
var ErrUnknownSchema = errors.New("unknown schema")

// ErrAttachmentDataSizeIncorrect is returned when the length of a written
// attachment does not match the length supplied.
var ErrAttachmentDataSizeIncorrect = errors.New("attachment content length incorrect")

// Writer is a writer for the MCAP format.
type Writer struct {
// Statistics collected over the course of the recording.
Expand Down

0 comments on commit 0f35709

Please sign in to comment.