forked from gsn/predictor
81 lines
2 KiB
Go
81 lines
2 KiB
Go
package errcodes
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestSpecificErrorTypes(t *testing.T) {
|
|
// Test Redis lock error
|
|
err := ErrRedisLockAlreadyLocked
|
|
if !IsErr(err) {
|
|
t.Error("Expected IsErr to return true for ErrorCode")
|
|
}
|
|
|
|
errcode, ok := AsErr(err)
|
|
if !ok {
|
|
t.Error("Expected AsErr to return true for ErrorCode")
|
|
}
|
|
if errcode != ErrRedisLockAlreadyLocked {
|
|
t.Error("Expected AsErr to return the same error")
|
|
}
|
|
|
|
// Test Redis cache miss error
|
|
cacheErr := ErrRedisCacheMiss
|
|
if !IsErr(cacheErr) {
|
|
t.Error("Expected IsErr to return true for cache miss error")
|
|
}
|
|
|
|
// Test configuration error
|
|
configErr := ErrConfigInvalidEnv
|
|
if !IsErr(configErr) {
|
|
t.Error("Expected IsErr to return true for config error")
|
|
}
|
|
|
|
// Test scheduler error
|
|
schedulerErr := ErrSchedulerTimeoutTooLong
|
|
if !IsErr(schedulerErr) {
|
|
t.Error("Expected IsErr to return true for scheduler error")
|
|
}
|
|
}
|
|
|
|
func TestErrorChecking(t *testing.T) {
|
|
// Example of how to check for specific errors in practice
|
|
err := ErrRedisLockAlreadyLocked
|
|
|
|
// Check if it's a specific error type
|
|
if errcode, ok := AsErr(err); ok {
|
|
switch errcode {
|
|
case ErrRedisLockAlreadyLocked:
|
|
// Handle lock already locked case
|
|
t.Log("Handling lock already locked error")
|
|
case ErrRedisCacheMiss:
|
|
// Handle cache miss case
|
|
t.Log("Handling cache miss error")
|
|
case ErrRedisCacheCorrupted:
|
|
// Handle corrupted cache case
|
|
t.Log("Handling corrupted cache error")
|
|
default:
|
|
// Handle other error types
|
|
t.Log("Handling other error type")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWrapFunction(t *testing.T) {
|
|
originalErr := ErrRedisCacheMiss
|
|
wrappedErr := Wrap(originalErr, "additional context")
|
|
|
|
if !IsErr(wrappedErr) {
|
|
t.Error("Expected wrapped error to be an ErrorCode")
|
|
}
|
|
|
|
errcode, ok := AsErr(wrappedErr)
|
|
if !ok {
|
|
t.Error("Expected AsErr to work with wrapped error")
|
|
}
|
|
|
|
// The wrapped error should have the same status code as the original
|
|
if errcode.StatusCode != ErrRedisCacheMiss.StatusCode {
|
|
t.Errorf("Expected status code %d, got %d", ErrRedisCacheMiss.StatusCode, errcode.StatusCode)
|
|
}
|
|
}
|