github.com/nats-io/nats-server/v2@v2.11.0-preview.2/server/jetstream_errors_test.go (about) 1 package server 2 3 import ( 4 "errors" 5 "testing" 6 ) 7 8 func TestIsNatsErr(t *testing.T) { 9 if !IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSNotEnabledForAccountErr) { 10 t.Fatalf("Expected error match") 11 } 12 13 if IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSClusterNotActiveErr) { 14 t.Fatalf("Expected error mismatch") 15 } 16 17 if IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSClusterNotActiveErr, JSClusterNotAvailErr) { 18 t.Fatalf("Expected error mismatch") 19 } 20 21 if !IsNatsErr(ApiErrors[JSNotEnabledForAccountErr], JSClusterNotActiveErr, JSNotEnabledForAccountErr) { 22 t.Fatalf("Expected error match") 23 } 24 25 if !IsNatsErr(&ApiError{ErrCode: 10039}, 1, JSClusterNotActiveErr, JSNotEnabledForAccountErr) { 26 t.Fatalf("Expected error match") 27 } 28 29 if IsNatsErr(&ApiError{ErrCode: 10039}, 1, 2, JSClusterNotActiveErr) { 30 t.Fatalf("Expected error mismatch") 31 } 32 33 if IsNatsErr(nil, JSClusterNotActiveErr) { 34 t.Fatalf("Expected error mismatch") 35 } 36 37 if IsNatsErr(errors.New("x"), JSClusterNotActiveErr) { 38 t.Fatalf("Expected error mismatch") 39 } 40 } 41 42 func TestApiError_Error(t *testing.T) { 43 if es := ApiErrors[JSClusterNotActiveErr].Error(); es != "JetStream not in clustered mode (10006)" { 44 t.Fatalf("Expected 'JetStream not in clustered mode (10006)', got %q", es) 45 } 46 } 47 48 func TestApiError_NewWithTags(t *testing.T) { 49 ne := NewJSRestoreSubscribeFailedError(errors.New("failed error"), "the.subject") 50 if ne.Description != "JetStream unable to subscribe to restore snapshot the.subject: failed error" { 51 t.Fatalf("Expected 'JetStream unable to subscribe to restore snapshot the.subject: failed error' got %q", ne.Description) 52 } 53 54 if ne == ApiErrors[JSRestoreSubscribeFailedErrF] { 55 t.Fatalf("Expected a new instance") 56 } 57 } 58 59 func TestApiError_NewWithUnless(t *testing.T) { 60 if ne := NewJSStreamRestoreError(errors.New("failed error"), Unless(ApiErrors[JSNotEnabledForAccountErr])); !IsNatsErr(ne, JSNotEnabledForAccountErr) { 61 t.Fatalf("Expected JSNotEnabledForAccountErr got %s", ne) 62 } 63 64 if ne := NewJSStreamRestoreError(errors.New("failed error")); !IsNatsErr(ne, JSStreamRestoreErrF) { 65 t.Fatalf("Expected JSStreamRestoreErrF got %s", ne) 66 } 67 68 if ne := NewJSStreamRestoreError(errors.New("failed error"), Unless(errors.New("other error"))); !IsNatsErr(ne, JSStreamRestoreErrF) { 69 t.Fatalf("Expected JSStreamRestoreErrF got %s", ne) 70 } 71 72 if ne := NewJSPeerRemapError(Unless(ApiErrors[JSNotEnabledForAccountErr])); !IsNatsErr(ne, JSNotEnabledForAccountErr) { 73 t.Fatalf("Expected JSNotEnabledForAccountErr got %s", ne) 74 } 75 76 if ne := NewJSPeerRemapError(Unless(nil)); !IsNatsErr(ne, JSPeerRemapErr) { 77 t.Fatalf("Expected JSPeerRemapErr got %s", ne) 78 } 79 80 if ne := NewJSPeerRemapError(Unless(errors.New("other error"))); !IsNatsErr(ne, JSPeerRemapErr) { 81 t.Fatalf("Expected JSPeerRemapErr got %s", ne) 82 } 83 }