2021-11-01 19:35:37 +03:00
|
|
|
package decode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/mathex"
|
2021-11-01 19:35:37 +03:00
|
|
|
"github.com/wader/fq/internal/recoverfn"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RecoverableErrorer interface {
|
|
|
|
IsRecoverableError() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type FormatError struct {
|
|
|
|
Err error
|
2021-11-17 18:46:10 +03:00
|
|
|
Format Format
|
2021-11-01 19:35:37 +03:00
|
|
|
Stacktrace recoverfn.Raw
|
|
|
|
}
|
|
|
|
|
|
|
|
type FormatsError struct {
|
|
|
|
Errs []FormatError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe FormatsError) Error() string {
|
|
|
|
var errs []string
|
|
|
|
for _, err := range fe.Errs {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
}
|
|
|
|
return strings.Join(errs, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fe FormatError) Error() string {
|
|
|
|
// var fns []string
|
|
|
|
// for _, f := range fe.Stacktrace.Frames() {
|
|
|
|
// fns = append(fns, fmt.Sprintf("%s:%d:%s", f.File, f.Line, f.Function))
|
|
|
|
// }
|
|
|
|
|
|
|
|
return fe.Err.Error()
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (fe FormatError) Value() any {
|
|
|
|
var st []any
|
2021-11-01 19:35:37 +03:00
|
|
|
for _, f := range fe.Stacktrace.Frames() {
|
|
|
|
st = append(st, f.Function)
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
return map[string]any{
|
2021-11-01 19:35:37 +03:00
|
|
|
"format": fe.Format.Name,
|
|
|
|
"error": fe.Err.Error(),
|
|
|
|
"stacktrace": st,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (FormatsError) IsRecoverableError() bool { return true }
|
|
|
|
|
|
|
|
type IOError struct {
|
|
|
|
Err error
|
|
|
|
Name string
|
|
|
|
Op string
|
|
|
|
ReadSize int64
|
|
|
|
SeekPos int64
|
|
|
|
Pos int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e IOError) Error() string {
|
|
|
|
var prefix string
|
|
|
|
if e.Name != "" {
|
|
|
|
prefix = e.Op + "(" + e.Name + ")"
|
|
|
|
} else {
|
|
|
|
prefix = e.Op
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s: failed at position %s (read size %s seek pos %s): %s",
|
2022-08-12 16:27:51 +03:00
|
|
|
prefix, mathex.Bits(e.Pos).StringByteBits(10), mathex.Bits(e.ReadSize).StringByteBits(10), mathex.Bits(e.SeekPos).StringByteBits(10), e.Err)
|
2021-11-01 19:35:37 +03:00
|
|
|
}
|
|
|
|
func (e IOError) Unwrap() error { return e.Err }
|
|
|
|
|
|
|
|
func (IOError) IsRecoverableError() bool { return true }
|
|
|
|
|
2021-11-17 18:26:13 +03:00
|
|
|
type DecoderError struct {
|
2021-11-01 19:35:37 +03:00
|
|
|
Reason string
|
|
|
|
Pos int64
|
|
|
|
}
|
|
|
|
|
2021-11-17 18:26:13 +03:00
|
|
|
func (e DecoderError) Error() string {
|
2022-08-12 16:27:51 +03:00
|
|
|
return fmt.Sprintf("error at position %s: %s", mathex.Bits(e.Pos).StringByteBits(16), e.Reason)
|
2021-11-01 19:35:37 +03:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:26:13 +03:00
|
|
|
func (DecoderError) IsRecoverableError() bool { return true }
|