Skip to content

Commit

Permalink
wip errors
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Oct 30, 2024
1 parent 54c5dc8 commit 2bba432
Show file tree
Hide file tree
Showing 139 changed files with 2,065 additions and 1,778 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
36 changes: 30 additions & 6 deletions bin/check-dep-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type PackageJSON = {
name: string;
version: string;
dependencies: Record<string, string>;
devDependencies: Record<string, string>;
};
type DenoJSON = {
name: string;
Expand Down Expand Up @@ -207,6 +208,15 @@ class NodeModule extends BaseModule {
return null;
}

hasDev(module: string): SemVer | null {
if (this.data.devDependencies) {
return NodeModule.parseVersion(
this.data.devDependencies[module],
);
}
return null;
}

static parseVersion(v: string): SemVer | null {
if (v) {
return v.startsWith("^") || v.startsWith("~")
Expand All @@ -220,12 +230,26 @@ class NodeModule extends BaseModule {
if (this.data.dependencies) {
const have = this.has(module);
if (have && version.compare(have) !== 0) {
this.data.dependencies[module] = `~${version.string()}`;
let prefix = this.data.dependencies[module].charAt(0);
if (prefix !== "^" && prefix !== "~") {
prefix = "";
}
this.data.dependencies[module] = `${prefix}${version.string()}`;
this.changed = true;
return true;
}
}
return false;
if (this.data.devDependencies) {
const have = this.hasDev(module);
if (have && version.compare(have) !== 0) {
let prefix = this.data.devDependencies[module].charAt(0);
if (prefix !== "^" && prefix !== "~") {
prefix = "";
}
this.data.devDependencies[module] = `${prefix}${version.string()}`;
this.changed = true;
}
}
return this.changed;
}

store(dir: string): Promise<void> {
Expand Down Expand Up @@ -279,15 +303,15 @@ for (const dir of dirs) {
}
const nmm = await NodeModule.load(d);
if (nmm) {
if (nmm.has(moduleName)) {
if (nmm.has(moduleName) || nmm.hasDev(moduleName)) {
nmm.update(moduleName, v);
await nmm.store(d);
}
const onuid = nmm.has("@nats-io/nuid");
const onuid = nmm.has("@nats-io/nuid") || nmm.hasDev("@nats-io/nuid");
if (onuid) {
nuid = nuid.max(onuid);
}
const onkeys = nmm.has("@nats-io/nkeys");
const onkeys = nmm.has("@nats-io/nkeys") || nmm.hasDev("@nats-io/nkeys");
if (onkeys) {
nkeys = nkeys.max(onkeys);
}
Expand Down
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-32",
"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
39 changes: 28 additions & 11 deletions core/examples/snippets/no_responders.ts
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(
{
Expand All @@ -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}`);
}
}

Expand Down
Loading

0 comments on commit 2bba432

Please sign in to comment.