| 1 |
package proto |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// Typed Redis errors for better error handling with wrapping support. |
| 9 |
// These errors maintain backward compatibility by keeping the same error messages. |
| 10 |
|
| 11 |
// LoadingError is returned when Redis is loading the dataset in memory. |
| 12 |
type LoadingError struct { |
| 13 |
msg string |
| 14 |
} |
| 15 |
|
| 16 |
func (e *LoadingError) Error() string { |
| 17 |
return e.msg |
| 18 |
} |
| 19 |
|
| 20 |
func (e *LoadingError) RedisError() {} |
| 21 |
|
| 22 |
// NewLoadingError creates a new LoadingError with the given message. |
| 23 |
func NewLoadingError(msg string) *LoadingError { |
| 24 |
return &LoadingError{msg: msg} |
| 25 |
} |
| 26 |
|
| 27 |
// ReadOnlyError is returned when trying to write to a read-only replica. |
| 28 |
type ReadOnlyError struct { |
| 29 |
msg string |
| 30 |
} |
| 31 |
|
| 32 |
func (e *ReadOnlyError) Error() string { |
| 33 |
return e.msg |
| 34 |
} |
| 35 |
|
| 36 |
func (e *ReadOnlyError) RedisError() {} |
| 37 |
|
| 38 |
// NewReadOnlyError creates a new ReadOnlyError with the given message. |
| 39 |
func NewReadOnlyError(msg string) *ReadOnlyError { |
| 40 |
return &ReadOnlyError{msg: msg} |
| 41 |
} |
| 42 |
|
| 43 |
// MovedError is returned when a key has been moved to a different node in a cluster. |
| 44 |
type MovedError struct { |
| 45 |
msg string |
| 46 |
addr string |
| 47 |
} |
| 48 |
|
| 49 |
func (e *MovedError) Error() string { |
| 50 |
return e.msg |
| 51 |
} |
| 52 |
|
| 53 |
func (e *MovedError) RedisError() {} |
| 54 |
|
| 55 |
// Addr returns the address of the node where the key has been moved. |
| 56 |
func (e *MovedError) Addr() string { |
| 57 |
return e.addr |
| 58 |
} |
| 59 |
|
| 60 |
// NewMovedError creates a new MovedError with the given message and address. |
| 61 |
func NewMovedError(msg string, addr string) *MovedError { |
| 62 |
return &MovedError{msg: msg, addr: addr} |
| 63 |
} |
| 64 |
|
| 65 |
// AskError is returned when a key is being migrated and the client should ask another node. |
| 66 |
type AskError struct { |
| 67 |
msg string |
| 68 |
addr string |
| 69 |
} |
| 70 |
|
| 71 |
func (e *AskError) Error() string { |
| 72 |
return e.msg |
| 73 |
} |
| 74 |
|
| 75 |
func (e *AskError) RedisError() {} |
| 76 |
|
| 77 |
// Addr returns the address of the node to ask. |
| 78 |
func (e *AskError) Addr() string { |
| 79 |
return e.addr |
| 80 |
} |
| 81 |
|
| 82 |
// NewAskError creates a new AskError with the given message and address. |
| 83 |
func NewAskError(msg string, addr string) *AskError { |
| 84 |
return &AskError{msg: msg, addr: addr} |
| 85 |
} |
| 86 |
|
| 87 |
// ClusterDownError is returned when the cluster is down. |
| 88 |
type ClusterDownError struct { |
| 89 |
msg string |
| 90 |
} |
| 91 |
|
| 92 |
func (e *ClusterDownError) Error() string { |
| 93 |
return e.msg |
| 94 |
} |
| 95 |
|
| 96 |
func (e *ClusterDownError) RedisError() {} |
| 97 |
|
| 98 |
// NewClusterDownError creates a new ClusterDownError with the given message. |
| 99 |
func NewClusterDownError(msg string) *ClusterDownError { |
| 100 |
return &ClusterDownError{msg: msg} |
| 101 |
} |
| 102 |
|
| 103 |
// TryAgainError is returned when a command cannot be processed and should be retried. |
| 104 |
type TryAgainError struct { |
| 105 |
msg string |
| 106 |
} |
| 107 |
|
| 108 |
func (e *TryAgainError) Error() string { |
| 109 |
return e.msg |
| 110 |
} |
| 111 |
|
| 112 |
func (e *TryAgainError) RedisError() {} |
| 113 |
|
| 114 |
// NewTryAgainError creates a new TryAgainError with the given message. |
| 115 |
func NewTryAgainError(msg string) *TryAgainError { |
| 116 |
return &TryAgainError{msg: msg} |
| 117 |
} |
| 118 |
|
| 119 |
// MasterDownError is returned when the master is down. |
| 120 |
type MasterDownError struct { |
| 121 |
msg string |
| 122 |
} |
| 123 |
|
| 124 |
func (e *MasterDownError) Error() string { |
| 125 |
return e.msg |
| 126 |
} |
| 127 |
|
| 128 |
func (e *MasterDownError) RedisError() {} |
| 129 |
|
| 130 |
// NewMasterDownError creates a new MasterDownError with the given message. |
| 131 |
func NewMasterDownError(msg string) *MasterDownError { |
| 132 |
return &MasterDownError{msg: msg} |
| 133 |
} |
| 134 |
|
| 135 |
// MaxClientsError is returned when the maximum number of clients has been reached. |
| 136 |
type MaxClientsError struct { |
| 137 |
msg string |
| 138 |
} |
| 139 |
|
| 140 |
func (e *MaxClientsError) Error() string { |
| 141 |
return e.msg |
| 142 |
} |
| 143 |
|
| 144 |
func (e *MaxClientsError) RedisError() {} |
| 145 |
|
| 146 |
// NewMaxClientsError creates a new MaxClientsError with the given message. |
| 147 |
func NewMaxClientsError(msg string) *MaxClientsError { |
| 148 |
return &MaxClientsError{msg: msg} |
| 149 |
} |
| 150 |
|
| 151 |
// AuthError is returned when authentication fails. |
| 152 |
type AuthError struct { |
| 153 |
msg string |
| 154 |
} |
| 155 |
|
| 156 |
func (e *AuthError) Error() string { |
| 157 |
return e.msg |
| 158 |
} |
| 159 |
|
| 160 |
func (e *AuthError) RedisError() {} |
| 161 |
|
| 162 |
// NewAuthError creates a new AuthError with the given message. |
| 163 |
func NewAuthError(msg string) *AuthError { |
| 164 |
return &AuthError{msg: msg} |
| 165 |
} |
| 166 |
|
| 167 |
// PermissionError is returned when a user lacks required permissions. |
| 168 |
type PermissionError struct { |
| 169 |
msg string |
| 170 |
} |
| 171 |
|
| 172 |
func (e *PermissionError) Error() string { |
| 173 |
return e.msg |
| 174 |
} |
| 175 |
|
| 176 |
func (e *PermissionError) RedisError() {} |
| 177 |
|
| 178 |
// NewPermissionError creates a new PermissionError with the given message. |
| 179 |
func NewPermissionError(msg string) *PermissionError { |
| 180 |
return &PermissionError{msg: msg} |
| 181 |
} |
| 182 |
|
| 183 |
// ExecAbortError is returned when a transaction is aborted. |
| 184 |
type ExecAbortError struct { |
| 185 |
msg string |
| 186 |
} |
| 187 |
|
| 188 |
func (e *ExecAbortError) Error() string { |
| 189 |
return e.msg |
| 190 |
} |
| 191 |
|
| 192 |
func (e *ExecAbortError) RedisError() {} |
| 193 |
|
| 194 |
// NewExecAbortError creates a new ExecAbortError with the given message. |
| 195 |
func NewExecAbortError(msg string) *ExecAbortError { |
| 196 |
return &ExecAbortError{msg: msg} |
| 197 |
} |
| 198 |
|
| 199 |
// OOMError is returned when Redis is out of memory. |
| 200 |
type OOMError struct { |
| 201 |
msg string |
| 202 |
} |
| 203 |
|
| 204 |
func (e *OOMError) Error() string { |
| 205 |
return e.msg |
| 206 |
} |
| 207 |
|
| 208 |
func (e *OOMError) RedisError() {} |
| 209 |
|
| 210 |
// NewOOMError creates a new OOMError with the given message. |
| 211 |
func NewOOMError(msg string) *OOMError { |
| 212 |
return &OOMError{msg: msg} |
| 213 |
} |
| 214 |
|
| 215 |
// parseTypedRedisError parses a Redis error message and returns a typed error if applicable. |
| 216 |
// This function maintains backward compatibility by keeping the same error messages. |
| 217 |
func parseTypedRedisError(msg string) error { |
| 218 |
// Check for specific error patterns and return typed errors |
| 219 |
switch { |
| 220 |
case strings.HasPrefix(msg, "LOADING "): |
| 221 |
return NewLoadingError(msg) |
| 222 |
case strings.HasPrefix(msg, "READONLY "): |
| 223 |
return NewReadOnlyError(msg) |
| 224 |
case strings.HasPrefix(msg, "MOVED "): |
| 225 |
// Extract address from "MOVED <slot> <addr>" |
| 226 |
addr := extractAddr(msg) |
| 227 |
return NewMovedError(msg, addr) |
| 228 |
case strings.HasPrefix(msg, "ASK "): |
| 229 |
// Extract address from "ASK <slot> <addr>" |
| 230 |
addr := extractAddr(msg) |
| 231 |
return NewAskError(msg, addr) |
| 232 |
case strings.HasPrefix(msg, "CLUSTERDOWN "): |
| 233 |
return NewClusterDownError(msg) |
| 234 |
case strings.HasPrefix(msg, "TRYAGAIN "): |
| 235 |
return NewTryAgainError(msg) |
| 236 |
case strings.HasPrefix(msg, "MASTERDOWN "): |
| 237 |
return NewMasterDownError(msg) |
| 238 |
case msg == "ERR max number of clients reached": |
| 239 |
return NewMaxClientsError(msg) |
| 240 |
case strings.HasPrefix(msg, "NOAUTH "), strings.HasPrefix(msg, "WRONGPASS "), strings.Contains(msg, "unauthenticated"): |
| 241 |
return NewAuthError(msg) |
| 242 |
case strings.HasPrefix(msg, "NOPERM "): |
| 243 |
return NewPermissionError(msg) |
| 244 |
case strings.HasPrefix(msg, "EXECABORT "): |
| 245 |
return NewExecAbortError(msg) |
| 246 |
case strings.HasPrefix(msg, "OOM "): |
| 247 |
return NewOOMError(msg) |
| 248 |
default: |
| 249 |
// Return generic RedisError for unknown error types |
| 250 |
return RedisError(msg) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// extractAddr extracts the address from MOVED/ASK error messages. |
| 255 |
// Format: "MOVED <slot> <addr>" or "ASK <slot> <addr>" |
| 256 |
func extractAddr(msg string) string { |
| 257 |
ind := strings.LastIndex(msg, " ") |
| 258 |
if ind == -1 { |
| 259 |
return "" |
| 260 |
} |
| 261 |
return msg[ind+1:] |
| 262 |
} |
| 263 |
|
| 264 |
// IsLoadingError checks if an error is a LoadingError, even if wrapped. |
| 265 |
func IsLoadingError(err error) bool { |
| 266 |
if err == nil { |
| 267 |
return false |
| 268 |
} |
| 269 |
var loadingErr *LoadingError |
| 270 |
if errors.As(err, &loadingErr) { |
| 271 |
return true |
| 272 |
} |
| 273 |
// Check if wrapped error is a RedisError with LOADING prefix |
| 274 |
var redisErr RedisError |
| 275 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "LOADING ") { |
| 276 |
return true |
| 277 |
} |
| 278 |
// Fallback to string checking for backward compatibility |
| 279 |
return strings.HasPrefix(err.Error(), "LOADING ") |
| 280 |
} |
| 281 |
|
| 282 |
// IsReadOnlyError checks if an error is a ReadOnlyError, even if wrapped. |
| 283 |
func IsReadOnlyError(err error) bool { |
| 284 |
if err == nil { |
| 285 |
return false |
| 286 |
} |
| 287 |
var readOnlyErr *ReadOnlyError |
| 288 |
if errors.As(err, &readOnlyErr) { |
| 289 |
return true |
| 290 |
} |
| 291 |
// Check if wrapped error is a RedisError with READONLY prefix |
| 292 |
var redisErr RedisError |
| 293 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "READONLY ") { |
| 294 |
return true |
| 295 |
} |
| 296 |
// Fallback to string checking for backward compatibility |
| 297 |
return strings.HasPrefix(err.Error(), "READONLY ") |
| 298 |
} |
| 299 |
|
| 300 |
// IsMovedError checks if an error is a MovedError, even if wrapped. |
| 301 |
// Returns the error and a boolean indicating if it's a MovedError. |
| 302 |
func IsMovedError(err error) (*MovedError, bool) { |
| 303 |
if err == nil { |
| 304 |
return nil, false |
| 305 |
} |
| 306 |
var movedErr *MovedError |
| 307 |
if errors.As(err, &movedErr) { |
| 308 |
return movedErr, true |
| 309 |
} |
| 310 |
// Fallback to string checking for backward compatibility |
| 311 |
s := err.Error() |
| 312 |
if strings.HasPrefix(s, "MOVED ") { |
| 313 |
// Parse: MOVED 3999 127.0.0.1:6381 |
| 314 |
parts := strings.Split(s, " ") |
| 315 |
if len(parts) == 3 { |
| 316 |
return &MovedError{msg: s, addr: parts[2]}, true |
| 317 |
} |
| 318 |
} |
| 319 |
return nil, false |
| 320 |
} |
| 321 |
|
| 322 |
// IsAskError checks if an error is an AskError, even if wrapped. |
| 323 |
// Returns the error and a boolean indicating if it's an AskError. |
| 324 |
func IsAskError(err error) (*AskError, bool) { |
| 325 |
if err == nil { |
| 326 |
return nil, false |
| 327 |
} |
| 328 |
var askErr *AskError |
| 329 |
if errors.As(err, &askErr) { |
| 330 |
return askErr, true |
| 331 |
} |
| 332 |
// Fallback to string checking for backward compatibility |
| 333 |
s := err.Error() |
| 334 |
if strings.HasPrefix(s, "ASK ") { |
| 335 |
// Parse: ASK 3999 127.0.0.1:6381 |
| 336 |
parts := strings.Split(s, " ") |
| 337 |
if len(parts) == 3 { |
| 338 |
return &AskError{msg: s, addr: parts[2]}, true |
| 339 |
} |
| 340 |
} |
| 341 |
return nil, false |
| 342 |
} |
| 343 |
|
| 344 |
// IsClusterDownError checks if an error is a ClusterDownError, even if wrapped. |
| 345 |
func IsClusterDownError(err error) bool { |
| 346 |
if err == nil { |
| 347 |
return false |
| 348 |
} |
| 349 |
var clusterDownErr *ClusterDownError |
| 350 |
if errors.As(err, &clusterDownErr) { |
| 351 |
return true |
| 352 |
} |
| 353 |
// Check if wrapped error is a RedisError with CLUSTERDOWN prefix |
| 354 |
var redisErr RedisError |
| 355 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "CLUSTERDOWN ") { |
| 356 |
return true |
| 357 |
} |
| 358 |
// Fallback to string checking for backward compatibility |
| 359 |
return strings.HasPrefix(err.Error(), "CLUSTERDOWN ") |
| 360 |
} |
| 361 |
|
| 362 |
// IsTryAgainError checks if an error is a TryAgainError, even if wrapped. |
| 363 |
func IsTryAgainError(err error) bool { |
| 364 |
if err == nil { |
| 365 |
return false |
| 366 |
} |
| 367 |
var tryAgainErr *TryAgainError |
| 368 |
if errors.As(err, &tryAgainErr) { |
| 369 |
return true |
| 370 |
} |
| 371 |
// Check if wrapped error is a RedisError with TRYAGAIN prefix |
| 372 |
var redisErr RedisError |
| 373 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "TRYAGAIN ") { |
| 374 |
return true |
| 375 |
} |
| 376 |
// Fallback to string checking for backward compatibility |
| 377 |
return strings.HasPrefix(err.Error(), "TRYAGAIN ") |
| 378 |
} |
| 379 |
|
| 380 |
// IsMasterDownError checks if an error is a MasterDownError, even if wrapped. |
| 381 |
func IsMasterDownError(err error) bool { |
| 382 |
if err == nil { |
| 383 |
return false |
| 384 |
} |
| 385 |
var masterDownErr *MasterDownError |
| 386 |
if errors.As(err, &masterDownErr) { |
| 387 |
return true |
| 388 |
} |
| 389 |
// Check if wrapped error is a RedisError with MASTERDOWN prefix |
| 390 |
var redisErr RedisError |
| 391 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "MASTERDOWN ") { |
| 392 |
return true |
| 393 |
} |
| 394 |
// Fallback to string checking for backward compatibility |
| 395 |
return strings.HasPrefix(err.Error(), "MASTERDOWN ") |
| 396 |
} |
| 397 |
|
| 398 |
// IsMaxClientsError checks if an error is a MaxClientsError, even if wrapped. |
| 399 |
func IsMaxClientsError(err error) bool { |
| 400 |
if err == nil { |
| 401 |
return false |
| 402 |
} |
| 403 |
var maxClientsErr *MaxClientsError |
| 404 |
if errors.As(err, &maxClientsErr) { |
| 405 |
return true |
| 406 |
} |
| 407 |
// Check if wrapped error is a RedisError with max clients prefix |
| 408 |
var redisErr RedisError |
| 409 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "ERR max number of clients reached") { |
| 410 |
return true |
| 411 |
} |
| 412 |
// Fallback to string checking for backward compatibility |
| 413 |
return strings.HasPrefix(err.Error(), "ERR max number of clients reached") |
| 414 |
} |
| 415 |
|
| 416 |
// IsAuthError checks if an error is an AuthError, even if wrapped. |
| 417 |
func IsAuthError(err error) bool { |
| 418 |
if err == nil { |
| 419 |
return false |
| 420 |
} |
| 421 |
var authErr *AuthError |
| 422 |
if errors.As(err, &authErr) { |
| 423 |
return true |
| 424 |
} |
| 425 |
// Check if wrapped error is a RedisError with auth error prefix |
| 426 |
var redisErr RedisError |
| 427 |
if errors.As(err, &redisErr) { |
| 428 |
s := redisErr.Error() |
| 429 |
return strings.HasPrefix(s, "NOAUTH ") || strings.HasPrefix(s, "WRONGPASS ") || strings.Contains(s, "unauthenticated") |
| 430 |
} |
| 431 |
// Fallback to string checking for backward compatibility |
| 432 |
s := err.Error() |
| 433 |
return strings.HasPrefix(s, "NOAUTH ") || strings.HasPrefix(s, "WRONGPASS ") || strings.Contains(s, "unauthenticated") |
| 434 |
} |
| 435 |
|
| 436 |
// IsPermissionError checks if an error is a PermissionError, even if wrapped. |
| 437 |
func IsPermissionError(err error) bool { |
| 438 |
if err == nil { |
| 439 |
return false |
| 440 |
} |
| 441 |
var permErr *PermissionError |
| 442 |
if errors.As(err, &permErr) { |
| 443 |
return true |
| 444 |
} |
| 445 |
// Check if wrapped error is a RedisError with NOPERM prefix |
| 446 |
var redisErr RedisError |
| 447 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "NOPERM ") { |
| 448 |
return true |
| 449 |
} |
| 450 |
// Fallback to string checking for backward compatibility |
| 451 |
return strings.HasPrefix(err.Error(), "NOPERM ") |
| 452 |
} |
| 453 |
|
| 454 |
// IsExecAbortError checks if an error is an ExecAbortError, even if wrapped. |
| 455 |
func IsExecAbortError(err error) bool { |
| 456 |
if err == nil { |
| 457 |
return false |
| 458 |
} |
| 459 |
var execAbortErr *ExecAbortError |
| 460 |
if errors.As(err, &execAbortErr) { |
| 461 |
return true |
| 462 |
} |
| 463 |
// Check if wrapped error is a RedisError with EXECABORT prefix |
| 464 |
var redisErr RedisError |
| 465 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "EXECABORT ") { |
| 466 |
return true |
| 467 |
} |
| 468 |
// Fallback to string checking for backward compatibility |
| 469 |
return strings.HasPrefix(err.Error(), "EXECABORT ") |
| 470 |
} |
| 471 |
|
| 472 |
// IsOOMError checks if an error is an OOMError, even if wrapped. |
| 473 |
func IsOOMError(err error) bool { |
| 474 |
if err == nil { |
| 475 |
return false |
| 476 |
} |
| 477 |
var oomErr *OOMError |
| 478 |
if errors.As(err, &oomErr) { |
| 479 |
return true |
| 480 |
} |
| 481 |
// Check if wrapped error is a RedisError with OOM prefix |
| 482 |
var redisErr RedisError |
| 483 |
if errors.As(err, &redisErr) && strings.HasPrefix(redisErr.Error(), "OOM ") { |
| 484 |
return true |
| 485 |
} |
| 486 |
// Fallback to string checking for backward compatibility |
| 487 |
return strings.HasPrefix(err.Error(), "OOM ") |
| 488 |
} |