-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked errors - removed NatsError --------- Signed-off-by: Alberto Ricart <[email protected]>
- Loading branch information
Showing
142 changed files
with
2,237 additions
and
1,886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,7 +107,7 @@ is working. | |
|
||
```typescript | ||
// import the connect function from a transport | ||
import { connect } from "jsr:@nats-io/nats-[email protected]4"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
|
||
const servers = [ | ||
{}, | ||
|
@@ -179,7 +179,7 @@ the server. | |
|
||
```typescript | ||
// import the connect function from a transport | ||
import { connect } from "jsr:@nats-io/nats-[email protected]4"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
|
||
// to create a connection to a nats-server: | ||
const nc = await connect({ servers: "demo.nats.io:4222" }); | ||
|
@@ -241,8 +241,8 @@ All subscriptions are independent. If two different subscriptions match a | |
subject, both will get to process the message: | ||
|
||
```typescript | ||
import { connect } from "jsr:@nats-io/nats-[email protected]4"; | ||
import type { Subscription } from "jsr:@nats-io/nats-[email protected]4"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
import type { Subscription } from "jsr:@nats-io/[email protected]7"; | ||
const nc = await connect({ servers: "demo.nats.io:4222" }); | ||
|
||
// subscriptions can have wildcard subjects | ||
|
@@ -418,11 +418,11 @@ independent unit. Note that non-queue subscriptions are also independent of | |
subscriptions in a queue group. | ||
|
||
```typescript | ||
import { connect } from "jsr:@nats-io/nats-[email protected]5"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
import type { | ||
NatsConnection, | ||
Subscription, | ||
} from "jsr:@nats-io/nats-[email protected]4"; | ||
} from "jsr:@nats-io/[email protected]7"; | ||
|
||
async function createService( | ||
name: string, | ||
|
@@ -541,29 +541,33 @@ If you send a request for which there's no interest, the request will be | |
immediately rejected: | ||
|
||
```typescript | ||
import { connect, ErrorCode } from "jsr:@nats-io/[email protected]"; | ||
import type { NatsError } from "jsr:@nats-io/[email protected]"; | ||
|
||
const nc = await connect( | ||
{ | ||
servers: `demo.nats.io`, | ||
}, | ||
); | ||
import { connect } from "jsr:@nats-io/[email protected]"; | ||
import { | ||
NoRespondersError, | ||
RequestError, | ||
TimeoutError, | ||
} from "jsr:@nats-io/[email protected]"; | ||
|
||
const nc = await connect({ | ||
servers: `demo.nats.io`, | ||
}); | ||
|
||
try { | ||
const m = await nc.request("hello.world"); | ||
console.log(m.data); | ||
} catch (err) { | ||
const nerr = err as NatsError; | ||
switch (nerr.code) { | ||
case ErrorCode.NoResponders: | ||
console.log("no one is listening to 'hello.world'"); | ||
break; | ||
case ErrorCode.Timeout: | ||
if (err instanceof RequestError) { | ||
if (err.cause instanceof TimeoutError) { | ||
console.log("someone is listening but didn't respond"); | ||
break; | ||
default: | ||
console.log("request failed", err); | ||
} else if (err.cause instanceof NoRespondersError) { | ||
console.log("no one is listening to 'hello.world'"); | ||
} else { | ||
console.log( | ||
`failed due to unknown error: ${(err.cause as Error)?.message}`, | ||
); | ||
} | ||
} else { | ||
console.log(`request failed: ${(err as Error).message}`); | ||
} | ||
} | ||
|
||
|
@@ -591,7 +595,7 @@ Setting the `user`/`pass` or `token` options, simply initializes an | |
```typescript | ||
// if the connection requires authentication, provide `user` and `pass` or | ||
// `token` options in the NatsConnectionOptions | ||
import { connect } from "jsr:@nats-io/nats-[email protected]"; | ||
import { connect } from "jsr:@nats-io/[email protected]"; | ||
|
||
const nc1 = await connect({ | ||
servers: "127.0.0.1:4222", | ||
|
@@ -680,8 +684,8 @@ You can specify several options when creating a subscription: | |
- `timeout`: how long to wait for the first message | ||
- `queue`: the [queue group](#queue-groups) name the subscriber belongs to | ||
- `callback`: a function with the signature | ||
`(err: NatsError|null, msg: Msg) => void;` that should be used for handling | ||
the message. Subscriptions with callbacks are NOT iterators. | ||
`(err: Error|null, msg: Msg) => void;` that should be used for handling the | ||
message. Subscriptions with callbacks are NOT iterators. | ||
|
||
#### Auto Unsubscribe | ||
|
||
|
@@ -701,7 +705,7 @@ const sub = nc.subscribe("hello", { timeout: 1000 }); | |
// handle the messages | ||
} | ||
})().catch((err) => { | ||
if (err.code === ErrorCode.Timeout) { | ||
if (err instanceof TimeoutError) { | ||
console.log(`sub timed out!`); | ||
} else { | ||
console.log(`sub iterator got an error!`); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
*/ | ||
|
||
// import the connect function from a transport | ||
import { connect } from "jsr:@nats-io/nats-[email protected]5"; | ||
import type { Subscription } from "jsr:@nats-io/nats-[email protected]5"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
import type { Subscription } from "jsr:@nats-io/[email protected]7"; | ||
|
||
// create a connection | ||
const nc = await connect({ servers: "demo.nats.io:4222" }); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
*/ | ||
|
||
// import the connect function from a transport | ||
import { connect } from "jsr:@nats-io/nats-[email protected]5"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
|
||
// to create a connection to a nats-server: | ||
const nc = await connect({ servers: "demo.nats.io:4222" }); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
*/ | ||
|
||
// import the connect function from a transport | ||
import { connect } from "jsr:@nats-io/nats-[email protected]5"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
|
||
const servers = [ | ||
{}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ import { | |
Empty, | ||
headers, | ||
nuid, | ||
} from "jsr:@nats-io/nats-[email protected]5"; | ||
} from "jsr:@nats-io/[email protected]7"; | ||
|
||
const nc = await connect( | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
*/ | ||
|
||
// import the connect function from a transport | ||
import { connect } from "jsr:@nats-io/nats-[email protected]5"; | ||
import { connect } from "jsr:@nats-io/[email protected]7"; | ||
|
||
// to create a connection to a nats-server: | ||
const nc = await connect({ servers: "demo.nats.io:4222" }); | ||
|
@@ -30,7 +30,7 @@ const sub = nc.subscribe("people"); | |
for await (const m of sub) { | ||
// typescript will see this as a Person | ||
const p = m.json<Person>(); | ||
console.log(`[${sub.getProcessed()}]: ${p.name}`); | ||
console.log(p); | ||
} | ||
})(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import { connect, ErrorCode } from "jsr:@nats-io/[email protected]"; | ||
import type { NatsError } from "jsr:@nats-io/[email protected]"; | ||
/* | ||
* Copyright 2024 Synadia Communications, Inc | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { connect } from "jsr:@nats-io/[email protected]"; | ||
import { errors } from "jsr:@nats-io/[email protected]"; | ||
|
||
const nc = await connect( | ||
{ | ||
|
@@ -11,16 +26,18 @@ try { | |
const m = await nc.request("hello.world"); | ||
console.log(m.data); | ||
} catch (err) { | ||
const nerr = err as NatsError; | ||
switch (nerr.code) { | ||
case ErrorCode.NoResponders: | ||
console.log("no one is listening to 'hello.world'"); | ||
break; | ||
case ErrorCode.Timeout: | ||
if (err instanceof Error) { | ||
if (err.cause instanceof errors.TimeoutError) { | ||
console.log("someone is listening but didn't respond"); | ||
break; | ||
default: | ||
console.log("request failed", err); | ||
} else if (err.cause instanceof errors.NoRespondersError) { | ||
console.log("no one is listening to 'hello.world'"); | ||
} else { | ||
console.log( | ||
`failed due to unknown error: ${(err.cause as Error)?.message}`, | ||
); | ||
} | ||
} else { | ||
console.log(`request failed: ${err}`); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.