Skip to content

Commit

Permalink
errors - wip
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Oct 29, 2024
1 parent 54c5dc8 commit 1f55822
Show file tree
Hide file tree
Showing 126 changed files with 1,886 additions and 1,616 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/consistency_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]
module: [
core,
jetstream,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]
node-version: [22.x]
permissions:
contents: read
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deno_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]
module: [core, jetstream, kv, obj, services, transport-deno]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jetstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]

steps:
- name: Git Checkout Core
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/kv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]

steps:
- name: Git Checkout Core
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/node_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]
node-version: [22.x]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/obj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]

steps:
- name: Git Checkout Core
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest-4-cores
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]

steps:
- name: Git Checkout Core
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/transport-node-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
test:
strategy:
matrix:
deno-version: [1.45.x]
deno-version: [2.0.x]
node-version: [22.x]

name: test node transport with local dependencies
Expand Down
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
- headers_only is needed in Consumer

- add a test for next/fetch/consume where message size smaller than availablle

- doc
58 changes: 31 additions & 27 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{},
Expand Down Expand Up @@ -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" });
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}`);
}
}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand All @@ -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!`);
Expand Down
2 changes: 1 addition & 1 deletion core/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nats-io/nats-core",
"version": "3.0.0-30",
"version": "3.0.0-31",
"exports": {
".": "./src/mod.ts",
"./internal": "./src/internal_mod.ts"
Expand Down
4 changes: 2 additions & 2 deletions core/examples/snippets/autounsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
2 changes: 1 addition & 1 deletion core/examples/snippets/basics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
2 changes: 1 addition & 1 deletion core/examples/snippets/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{},
Expand Down
2 changes: 1 addition & 1 deletion core/examples/snippets/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
4 changes: 2 additions & 2 deletions core/examples/snippets/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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);
}
})();

Expand Down
43 changes: 32 additions & 11 deletions core/examples/snippets/no_responders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
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 {
NoRespondersError,
RequestError,
TimeoutError,
} from "jsr:@nats-io/[email protected]";

const nc = await connect(
{
Expand All @@ -11,16 +30,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 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}`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/examples/snippets/queuegroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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,
Expand Down
4 changes: 2 additions & 2 deletions core/examples/snippets/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
2 changes: 1 addition & 1 deletion core/examples/snippets/service_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

// import the connect function from a transport
import { connect, Empty } from "jsr:@nats-io/nats-[email protected]5";
import { connect, Empty } from "jsr:@nats-io/[email protected]7";

// create a connection
const nc = await connect({ servers: "demo.nats.io:4222" });
Expand Down
2 changes: 1 addition & 1 deletion core/examples/snippets/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
4 changes: 2 additions & 2 deletions core/examples/snippets/sub_timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

// import the connect function from a transport
import { connect, ErrorCode } from "jsr:@nats-io/nats-[email protected]5";
import { connect, TimeoutError } from "jsr:@nats-io/[email protected]7";

// to create a connection to a nats-server:
const nc = await connect({ servers: "demo.nats.io:4222" });
Expand All @@ -27,7 +27,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!`);
Expand Down
Loading

0 comments on commit 1f55822

Please sign in to comment.