MCPcopy
hub / github.com/nats-io/nats.go / TestJetStreamErrors

Function TestJetStreamErrors

test/js_test.go:86–249  ·  test/js_test.go::TestJetStreamErrors
(t *testing.T)

Source from the content-addressed store, hash-verified

84}
85
86func TestJetStreamErrors(t *testing.T) {
87 t.Run("API error", func(t *testing.T) {
88 conf := createConfFile(t, []byte(`
89 listen: 127.0.0.1:-1
90 no_auth_user: rip
91 jetstream: {max_mem_store: 64GB, max_file_store: 10TB}
92 accounts: {
93 JS: {
94 jetstream: enabled
95 users: [ {user: dlc, password: foo} ]
96 },
97 IU: {
98 users: [ {user: rip, password: bar} ]
99 },
100 }
101 `))
102 defer os.Remove(conf)
103
104 s, _ := RunServerWithConfig(conf)
105 defer shutdownJSServerAndRemoveStorage(t, s)
106
107 nc, js := jsClient(t, s)
108 defer nc.Close()
109
110 _, err := js.AccountInfo()
111 // check directly to var (backwards compatible)
112 if err != nats.ErrJetStreamNotEnabledForAccount {
113 t.Fatalf("Did not get the proper error, got %v", err)
114 }
115
116 // matching via errors.Is
117 if ok := errors.Is(err, nats.ErrJetStreamNotEnabledForAccount); !ok {
118 t.Fatal("Expected ErrJetStreamNotEnabledForAccount")
119 }
120
121 // matching wrapped via error.Is
122 err2 := fmt.Errorf("custom error: %w", nats.ErrJetStreamNotEnabledForAccount)
123 if ok := errors.Is(err2, nats.ErrJetStreamNotEnabledForAccount); !ok {
124 t.Fatal("Expected wrapped ErrJetStreamNotEnabled")
125 }
126
127 // via classic type assertion.
128 jserr, ok := err.(nats.JetStreamError)
129 if !ok {
130 t.Fatal("Expected a JetStreamError")
131 }
132 expected := nats.JSErrCodeJetStreamNotEnabledForAccount
133 if jserr.APIError().ErrorCode != expected {
134 t.Fatalf("Expected: %v, got: %v", expected, jserr.APIError().ErrorCode)
135 }
136 if jserr.APIError() == nil {
137 t.Fatal("Expected APIError")
138 }
139
140 // matching to interface via errors.As(...)
141 var apierr nats.JetStreamError
142 ok = errors.As(err, &apierr)
143 if !ok {

Callers

nothing calls this directly

Calls 15

APIErrorMethod · 0.95
ErrorMethod · 0.95
FatalfMethod · 0.80
ErrorfMethod · 0.80
createConfFileFunction · 0.70
RunServerWithConfigFunction · 0.70
jsClientFunction · 0.70
RunBasicJetStreamServerFunction · 0.70
AccountInfoMethod · 0.65
APIErrorMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected