-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
52 lines (43 loc) · 1.36 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
package chess
import (
"errors"
"fmt"
)
// PGNError custom error types for different PGN errors.
type PGNError struct {
msg string
pos int // position where error occurred
}
func (e *PGNError) Error() string {
return e.msg
}
func (e *PGNError) Is(target error) bool {
var t *PGNError
ok := errors.As(target, &t)
if !ok {
return false
}
return e.msg == t.msg
}
// Custom error types for different PGN errors
//
//nolint:gochecknoglobals // this is a custom error type.
var (
ErrUnterminatedComment = func(pos int) error { return &PGNError{"unterminated comment", pos} }
ErrUnterminatedQuote = func(pos int) error { return &PGNError{"unterminated quote", pos} }
ErrInvalidCommand = func(pos int) error { return &PGNError{"invalid command in comment", pos} }
ErrInvalidPiece = func(pos int) error { return &PGNError{"invalid piece", pos} }
ErrInvalidSquare = func(pos int) error { return &PGNError{"invalid square", pos} }
ErrInvalidRank = func(pos int) error { return &PGNError{"invalid rank", pos} }
ErrNoGameFound = errors.New("no game found in PGN data")
)
type ParserError struct {
Message string
TokenValue string
TokenType TokenType
Position int
}
func (e *ParserError) Error() string {
return fmt.Sprintf("Parser error at position %d: %s (Token: %v, Value: %s)",
e.Position, e.Message, e.TokenType, e.TokenValue)
}