-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
78 lines (64 loc) · 1.84 KB
/
errors.go
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
// SPDX-FileCopyrightText: 2021 Henry Bubert
//
// SPDX-License-Identifier: MIT
package refs
import (
"encoding/json"
"errors"
"fmt"
)
// Common errors for invalid references
var (
ErrInvalidRef = errors.New("ssb: Invalid Ref")
ErrInvalidRefType = errors.New("ssb: Invalid Ref Type")
ErrInvalidRefAlgo = errors.New("ssb: Invalid Ref Algo")
ErrInvalidSig = errors.New("ssb: Invalid Signature")
ErrInvalidHash = errors.New("ssb: Invalid Hash")
ErrUnuspportedFormat = errors.New("ssb: unsupported format")
)
// ErrRefLen is returned when a parsed reference was too short.
type ErrRefLen struct {
algo RefAlgo
n int
}
func (e ErrRefLen) Error() string {
return fmt.Sprintf("ssb: Invalid reference len for %s: %d", e.algo, e.n)
}
// NewFeedRefLenError returns a new ErrRefLen error for a feed
func newFeedRefLenError(n int) error {
return ErrRefLen{algo: RefAlgoFeedSSB1, n: n}
}
func newHashLenError(n int) error {
return ErrRefLen{algo: RefAlgoMessageSSB1, n: n}
}
// IsMessageUnusable checks if an error is ErrWrongType, ErrMalfromedMsg or *json.SyntaxError
func IsMessageUnusable(err error) bool {
var errWt ErrWrongType
if errors.As(err, &errWt) {
return true
}
var errMalMsg ErrMalfromedMsg
if errors.As(err, &errMalMsg) {
return true
}
return errors.Is(err, &json.SyntaxError{})
}
// ErrMalfromedMsg is returned if a message has invalid values
type ErrMalfromedMsg struct {
reason string
m map[string]interface{}
}
func (emm ErrMalfromedMsg) Error() string {
s := "ErrMalfromedMsg: " + emm.reason
if emm.m != nil {
s += fmt.Sprintf(" %+v", emm.m)
}
return s
}
// ErrWrongType is returned if a certain type:value was expected on a message.
type ErrWrongType struct {
has, want string
}
func (ewt ErrWrongType) Error() string {
return fmt.Sprintf("ErrWrongType: want: %s has: %s", ewt.want, ewt.has)
}