-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
151 lines (130 loc) · 4 KB
/
errors.go
File metadata and controls
151 lines (130 loc) · 4 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package database
import (
"fmt"
ierrors "github.com/xraph/forge/errors"
)
// Error codes for database operations.
const (
CodeDatabaseError = "DATABASE_ERROR"
CodeDatabaseNotFound = "DATABASE_NOT_FOUND"
CodeDatabaseExists = "DATABASE_ALREADY_EXISTS"
CodeDatabaseNotOpened = "DATABASE_NOT_OPENED"
CodeDatabaseInvalidType = "DATABASE_INVALID_TYPE"
CodeDatabaseConnection = "DATABASE_CONNECTION_ERROR"
CodeDatabaseQuery = "DATABASE_QUERY_ERROR"
CodeDatabaseTransaction = "DATABASE_TRANSACTION_ERROR"
CodeDatabasePanic = "DATABASE_PANIC_RECOVERED"
CodeDatabaseConfig = "DATABASE_CONFIG_ERROR"
)
// DatabaseError wraps database-specific errors with context.
type DatabaseError struct {
DBName string
DBType DatabaseType
Operation string
Code string
Err error
}
func (e *DatabaseError) Error() string {
if e.DBName != "" {
return fmt.Sprintf("database %s (%s): %s: %v", e.DBName, e.DBType, e.Operation, e.Err)
}
return fmt.Sprintf("database (%s): %s: %v", e.DBType, e.Operation, e.Err)
}
func (e *DatabaseError) Unwrap() error {
return e.Err
}
// NewDatabaseError creates a new database error with context.
func NewDatabaseError(dbName string, dbType DatabaseType, operation string, err error) *DatabaseError {
return &DatabaseError{
DBName: dbName,
DBType: dbType,
Operation: operation,
Code: CodeDatabaseError,
Err: err,
}
}
// Error constructors for common database errors.
func ErrNoDatabasesConfigured() error {
return ierrors.ErrConfigError("no databases configured", nil)
}
func ErrInvalidDatabaseName(name string) error {
return ierrors.ErrValidationError("database.name", fmt.Errorf("invalid database name: %s", name))
}
func ErrInvalidDatabaseType(dbType string) error {
return ierrors.ErrValidationError("database.type", fmt.Errorf("invalid database type: %s", dbType))
}
func ErrInvalidDSN(dsn string) error {
return ierrors.ErrValidationError("database.dsn", ierrors.New("invalid or empty DSN"))
}
func ErrInvalidPoolConfig(reason string) error {
return ierrors.ErrConfigError("invalid connection pool configuration: "+reason, nil)
}
func ErrDatabaseNotFound(name string) error {
return &DatabaseError{
DBName: name,
Operation: "get",
Code: CodeDatabaseNotFound,
Err: fmt.Errorf("database %s not found", name),
}
}
func ErrDatabaseAlreadyExists(name string) error {
return &DatabaseError{
DBName: name,
Operation: "register",
Code: CodeDatabaseExists,
Err: fmt.Errorf("database %s already exists", name),
}
}
func ErrDatabaseNotOpened(name string) error {
return &DatabaseError{
DBName: name,
Operation: "operation",
Code: CodeDatabaseNotOpened,
Err: fmt.Errorf("database %s not opened", name),
}
}
func ErrInvalidDatabaseTypeOp(name string, expectedType, actualType DatabaseType) error {
return &DatabaseError{
DBName: name,
DBType: actualType,
Operation: "type_check",
Code: CodeDatabaseInvalidType,
Err: fmt.Errorf("expected %s database, got %s", expectedType, actualType),
}
}
func ErrConnectionFailed(dbName string, dbType DatabaseType, cause error) error {
return &DatabaseError{
DBName: dbName,
DBType: dbType,
Operation: "connect",
Code: CodeDatabaseConnection,
Err: cause,
}
}
func ErrQueryFailed(dbName string, dbType DatabaseType, cause error) error {
return &DatabaseError{
DBName: dbName,
DBType: dbType,
Operation: "query",
Code: CodeDatabaseQuery,
Err: cause,
}
}
func ErrTransactionFailed(dbName string, dbType DatabaseType, cause error) error {
return &DatabaseError{
DBName: dbName,
DBType: dbType,
Operation: "transaction",
Code: CodeDatabaseTransaction,
Err: cause,
}
}
func ErrPanicRecovered(dbName string, dbType DatabaseType, panicValue any) error {
return &DatabaseError{
DBName: dbName,
DBType: dbType,
Operation: "panic_recovery",
Code: CodeDatabasePanic,
Err: fmt.Errorf("panic recovered: %v", panicValue),
}
}