Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
88 lines (78 loc) · 1.98 KB
/
Copy patherrors.go
File metadata and controls
88 lines (78 loc) · 1.98 KB
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
package runtime
import "fmt"
// SessionErrorKind classifies the category of a session error.
type SessionErrorKind int
const (
// SessionErrIO indicates a filesystem I/O error.
SessionErrIO SessionErrorKind = iota
// SessionErrJSON indicates a JSON serialization/deserialization error.
SessionErrJSON
// SessionErrInvalidFormat indicates the session data has an invalid or unexpected format.
SessionErrInvalidFormat
)
func (k SessionErrorKind) String() string {
switch k {
case SessionErrIO:
return "io"
case SessionErrJSON:
return "json"
case SessionErrInvalidFormat:
return "invalid_format"
default:
return "unknown"
}
}
// SessionError is a structured error for session operations.
type SessionError struct {
Kind SessionErrorKind
Op string
Path string
Message string
Cause error
}
// Error implements the error interface.
func (e *SessionError) Error() string {
if e.Path != "" {
return fmt.Sprintf("session %s [%s]: %s", e.Op, e.Path, e.Message)
}
return fmt.Sprintf("session %s: %s", e.Op, e.Message)
}
// Unwrap returns the underlying cause, enabling errors.Is and errors.As.
func (e *SessionError) Unwrap() error {
return e.Cause
}
// NewSessionIOError creates a SessionError for I/O failures.
func NewSessionIOError(op, path string, cause error) *SessionError {
msg := "i/o error"
if cause != nil {
msg = cause.Error()
}
return &SessionError{
Kind: SessionErrIO,
Op: op,
Path: path,
Message: msg,
Cause: cause,
}
}
// NewSessionJSONError creates a SessionError for JSON failures.
func NewSessionJSONError(op string, cause error) *SessionError {
msg := "json error"
if cause != nil {
msg = cause.Error()
}
return &SessionError{
Kind: SessionErrJSON,
Op: op,
Message: msg,
Cause: cause,
}
}
// NewSessionFormatError creates a SessionError for invalid format.
func NewSessionFormatError(msg string) *SessionError {
return &SessionError{
Kind: SessionErrInvalidFormat,
Op: "parse",
Message: msg,
}
}