/ internal / memory / errclass_test.go
errclass_test.go
 1  package memory
 2  
 3  import "testing"
 4  
 5  func TestClassifyHTTP(t *testing.T) {
 6  	cases := []struct {
 7  		name   string
 8  		status int
 9  		env    *ResponseEnvelope
10  		want   ErrorClass
11  	}{
12  		{"200 ok", 200, &ResponseEnvelope{Reason: "ok"}, ClassOK},
13  		{"200 no_data", 200, &ResponseEnvelope{Reason: "no_data"}, ClassOK},
14  		{"200 degraded", 200, &ResponseEnvelope{Reason: "degraded"}, ClassOK},
15  		{"401 auth", 401, &ResponseEnvelope{Error: &ErrorObject{Code: "auth", Details: map[string]any{"sub_code": "invalid_api_key"}}}, ClassPermanent},
16  		{"403 forbidden", 403, &ResponseEnvelope{Error: &ErrorObject{Code: "auth", Details: map[string]any{"sub_code": "token_expired"}}}, ClassPermanent},
17  		{"503 not_ready", 503, &ResponseEnvelope{Error: &ErrorObject{Code: "not_ready"}}, ClassUnavailable},
18  		{"503 incompatible", 503, &ResponseEnvelope{Error: &ErrorObject{Code: "incompatible_bundle", Details: map[string]any{"sub_code": "version_out_of_range"}}}, ClassPermanent},
19  		{"503 missing_artifact", 503, &ResponseEnvelope{Error: &ErrorObject{Code: "incompatible_bundle", Details: map[string]any{"sub_code": "missing_artifact"}}}, ClassPermanent},
20  		{"422 schema", 422, &ResponseEnvelope{Error: &ErrorObject{Code: "validation_error", Details: map[string]any{"sub_code": "schema_validation"}}}, ClassPermanent},
21  		{"400 unsupported_protocol", 400, &ResponseEnvelope{Error: &ErrorObject{Code: "validation_error", Details: map[string]any{"sub_code": "unsupported_protocol"}}}, ClassPermanent},
22  		{"409 reload_in_progress", 409, &ResponseEnvelope{Error: &ErrorObject{Code: "bundle_load_error", Details: map[string]any{"sub_code": "reload_in_progress"}}}, ClassRetryable},
23  		{"500 query_failed", 500, &ResponseEnvelope{Error: &ErrorObject{Code: "internal_error", Details: map[string]any{"sub_code": "query_failed"}}}, ClassRetryable},
24  		{"unknown 5xx no sub_code", 599, &ResponseEnvelope{Error: &ErrorObject{Code: "x"}}, ClassRetryable},
25  	}
26  	for _, tc := range cases {
27  		t.Run(tc.name, func(t *testing.T) {
28  			if got := ClassifyHTTP(tc.status, tc.env); got != tc.want {
29  				t.Fatalf("got %v want %v", got, tc.want)
30  			}
31  		})
32  	}
33  }
34  
35  func TestClassifyTransportError(t *testing.T) {
36  	if ClassifyTransportError(nil) != ClassOK {
37  		t.Fatal("nil should be OK")
38  	}
39  	if ClassifyTransportError(ErrTransport) != ClassUnavailable {
40  		t.Fatal("transport sentinel should be Unavailable")
41  	}
42  }