@@ -107,7 +107,7 @@ is working.
107
107
108
108
``` typescript
109
109
// import the connect function from a transport
110
- import {
connect }
from " jsr:@nats-io/nats- [email protected] 4 " ;
110
+ import {
connect }
from " jsr:@nats-io/[email protected] 7 " ;
111
111
112
112
const servers = [
113
113
{},
@@ -179,7 +179,7 @@ the server.
179
179
180
180
``` typescript
181
181
// import the connect function from a transport
182
- import {
connect }
from " jsr:@nats-io/nats- [email protected] 4 " ;
182
+ import {
connect }
from " jsr:@nats-io/[email protected] 7 " ;
183
183
184
184
// to create a connection to a nats-server:
185
185
const nc = await connect ({ servers: " demo.nats.io:4222" });
@@ -241,8 +241,8 @@ All subscriptions are independent. If two different subscriptions match a
241
241
subject, both will get to process the message:
242
242
243
243
``` typescript
244
- import {
connect }
from " jsr:@nats-io/nats- [email protected] 4 " ;
245
- import type {
Subscription }
from " jsr:@nats-io/nats- [email protected] 4 " ;
244
+ import {
connect }
from " jsr:@nats-io/[email protected] 7 " ;
245
+ import type {
Subscription }
from " jsr:@nats-io/[email protected] 7 " ;
246
246
const nc = await connect ({ servers: " demo.nats.io:4222" });
247
247
248
248
// subscriptions can have wildcard subjects
@@ -418,11 +418,11 @@ independent unit. Note that non-queue subscriptions are also independent of
418
418
subscriptions in a queue group.
419
419
420
420
``` typescript
421
- import {
connect }
from " jsr:@nats-io/nats- [email protected] 5 " ;
421
+ import {
connect }
from " jsr:@nats-io/[email protected] 7 " ;
422
422
import type {
423
423
NatsConnection ,
424
424
Subscription ,
425
- }
from " jsr:@nats-io/nats- [email protected] 4 " ;
425
+ }
from " jsr:@nats-io/[email protected] 7 " ;
426
426
427
427
async function createService(
428
428
name : string ,
@@ -541,29 +541,33 @@ If you send a request for which there's no interest, the request will be
541
541
immediately rejected:
542
542
543
543
``` typescript
544
- import {
connect ,
ErrorCode }
from " jsr:@nats-io/[email protected] " ;
545
- import type {
NatsError }
from " jsr:@nats-io/[email protected] " ;
546
-
547
- const nc = await connect (
548
- {
549
- servers: ` demo.nats.io ` ,
550
- },
551
- );
544
+ import {
connect }
from " jsr:@nats-io/[email protected] " ;
545
+ import {
546
+ NoRespondersError ,
547
+ RequestError ,
548
+ TimeoutError ,
549
+ }
from " jsr:@nats-io/[email protected] " ;
550
+
551
+ const nc = await connect ({
552
+ servers: ` demo.nats.io ` ,
553
+ });
552
554
553
555
try {
554
556
const m = await nc .request (" hello.world" );
555
557
console .log (m .data );
556
558
} catch (err ) {
557
- const nerr = err as NatsError ;
558
- switch (nerr .code ) {
559
- case ErrorCode .NoResponders :
560
- console .log (" no one is listening to 'hello.world'" );
561
- break ;
562
- case ErrorCode .Timeout :
559
+ if (err instanceof RequestError ) {
560
+ if (err .cause instanceof TimeoutError ) {
563
561
console .log (" someone is listening but didn't respond" );
564
- break ;
565
- default :
566
- console .log (" request failed" , err );
562
+ } else if (err .cause instanceof NoRespondersError ) {
563
+ console .log (" no one is listening to 'hello.world'" );
564
+ } else {
565
+ console .log (
566
+ ` failed due to unknown error: ${(err .cause as Error )?.message } ` ,
567
+ );
568
+ }
569
+ } else {
570
+ console .log (` request failed: ${(err as Error ).message } ` );
567
571
}
568
572
}
569
573
@@ -591,7 +595,7 @@ Setting the `user`/`pass` or `token` options, simply initializes an
591
595
``` typescript
592
596
// if the connection requires authentication, provide `user` and `pass` or
593
597
// `token` options in the NatsConnectionOptions
594
- import {
connect }
from " jsr:@nats-io/nats- [email protected] " ;
598
+ import {
connect }
from " jsr:@nats-io/[email protected] " ;
595
599
596
600
const nc1 = await connect ({
597
601
servers: " 127.0.0.1:4222" ,
@@ -680,8 +684,8 @@ You can specify several options when creating a subscription:
680
684
- ` timeout ` : how long to wait for the first message
681
685
- ` queue ` : the [ queue group] ( #queue-groups ) name the subscriber belongs to
682
686
- ` callback ` : a function with the signature
683
- ` (err: NatsError |null, msg: Msg) => void; ` that should be used for handling
684
- the message. Subscriptions with callbacks are NOT iterators.
687
+ ` (err: Error |null, msg: Msg) => void; ` that should be used for handling the
688
+ message. Subscriptions with callbacks are NOT iterators.
685
689
686
690
#### Auto Unsubscribe
687
691
@@ -701,7 +705,7 @@ const sub = nc.subscribe("hello", { timeout: 1000 });
701
705
// handle the messages
702
706
}
703
707
})().catch ((err ) => {
704
- if (err . code === ErrorCode . Timeout ) {
708
+ if (err instanceof TimeoutError ) {
705
709
console .log (` sub timed out!` );
706
710
} else {
707
711
console .log (` sub iterator got an error!` );
0 commit comments