File: errors.go

package info (click to toggle)
fq 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 106,624 kB
  • sloc: xml: 2,835; makefile: 250; sh: 241; exp: 57; ansic: 21
file content (90 lines) | stat: -rw-r--r-- 1,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package decode

import (
	"fmt"
	"strings"

	"github.com/wader/fq/internal/mathex"
	"github.com/wader/fq/internal/recoverfn"
)

type RecoverableErrorer interface {
	IsRecoverableError() bool
}

type FormatError struct {
	Err        error
	Format     *Format
	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()
}

func (fe FormatError) Value() any {
	var st []any
	for _, f := range fe.Stacktrace.Frames() {
		st = append(st, f.Function)
	}

	return map[string]any{
		"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",
		prefix, mathex.Bits(e.Pos).StringByteBits(10), mathex.Bits(e.ReadSize).StringByteBits(10), mathex.Bits(e.SeekPos).StringByteBits(10), e.Err)
}
func (e IOError) Unwrap() error { return e.Err }

func (IOError) IsRecoverableError() bool { return true }

type DecoderError struct {
	Reason string
	Pos    int64
}

func (e DecoderError) Error() string {
	return fmt.Sprintf("error at position %s: %s", mathex.Bits(e.Pos).StringByteBits(16), e.Reason)
}

func (DecoderError) IsRecoverableError() bool { return true }