2025-01-21 13:18:17 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCheckErrorCode(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
codeString string
|
|
|
|
|
expectCode codes.Code
|
|
|
|
|
expectSuccess bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
"old format, code matches what is looked for",
|
|
|
|
|
`{Code:5,"details":[],"message":"Error: The bucket etc."}`,
|
|
|
|
|
codes.Code(5),
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"old format, code doesn't match what is looked for",
|
|
|
|
|
`{Code:55,"details":[],"message":"Error: The bucket etc."}`,
|
|
|
|
|
codes.Code(5),
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"new format, code matches what is looked for",
|
|
|
|
|
`{"code":5,"details":[],"message":"Error: The bucket etc."}`,
|
|
|
|
|
codes.Code(5),
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"new format, code doesn't match what is looked for",
|
|
|
|
|
`{"code":55,"details":[],"message":"Error: The bucket etc."}`,
|
|
|
|
|
codes.Code(5),
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"bad format, should always be false",
|
|
|
|
|
`"ceod":55`,
|
|
|
|
|
codes.Code(5),
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2025-11-18 05:19:03 -05:00
|
|
|
found := CheckErrorCode(fmt.Errorf("%s", tt.codeString), tt.expectCode)
|
2025-01-21 13:18:17 -05:00
|
|
|
if found != tt.expectSuccess {
|
|
|
|
|
t.Errorf("check error code returned %t, expected %t", found, tt.expectSuccess)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|