From cdb36ee4d71a357b187ef8294bf68feb68f73a2e Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Mon, 20 Sep 2021 09:20:17 -0500 Subject: [PATCH 1/2] [feat] stored context for requests so that if there's an error, the stack will be helpful. --- Makefile | 3 +++ nats-base-client/nats.ts | 7 ++++++ nats-base-client/request.ts | 3 +++ tests/timeout_test.ts | 46 +++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 tests/timeout_test.ts diff --git a/Makefile b/Makefile index e52e67a0..b6213d53 100644 --- a/Makefile +++ b/Makefile @@ -16,3 +16,6 @@ clean: bundle: deno bundle --log-level info --unstable src/mod.ts ./nats.js + +fmt: + deno fmt src/ doc/ bin/ nats-base-client/ examples/ tests/ diff --git a/nats-base-client/nats.ts b/nats-base-client/nats.ts index b57ba06b..4e2a6d44 100644 --- a/nats-base-client/nats.ts +++ b/nats-base-client/nats.ts @@ -154,6 +154,7 @@ export class NatsConnectionImpl implements NatsConnection { ? opts.reply : createInbox(this.options.inboxPrefix); const d = deferred(); + const errCtx = new Error(); this.subscribe( inbox, { @@ -161,10 +162,16 @@ export class NatsConnectionImpl implements NatsConnection { timeout: opts.timeout, callback: (err, msg) => { if (err) { + // timeouts from `timeout()` will have the proper stack + if (err.code !== ErrorCode.Timeout) { + err.stack += `\n\n${errCtx.stack}`; + } d.reject(err); } else { err = isRequestError(msg); if (err) { + // if we failed here, help the developer by showing what failed + err.stack += `\n\n${errCtx.stack}`; d.reject(err); } else { d.resolve(msg); diff --git a/nats-base-client/request.ts b/nats-base-client/request.ts index 3799ee44..4d7ebb82 100644 --- a/nats-base-client/request.ts +++ b/nats-base-client/request.ts @@ -23,6 +23,7 @@ export class Request { received: number; deferred: Deferred; timer: Timeout; + ctx: Error; private mux: MuxSubscription; constructor( @@ -35,6 +36,7 @@ export class Request { this.token = nuid.next(); extend(this, opts); this.timer = timeout(opts.timeout); + this.ctx = new Error(); } resolver(err: Error | null, msg: Msg): void { @@ -42,6 +44,7 @@ export class Request { this.timer.cancel(); } if (err) { + err.stack += `\n\n${this.ctx.stack}`; this.deferred.reject(err); } else { this.deferred.resolve(msg); diff --git a/tests/timeout_test.ts b/tests/timeout_test.ts new file mode 100644 index 00000000..424d7951 --- /dev/null +++ b/tests/timeout_test.ts @@ -0,0 +1,46 @@ +/* + * Copyright 2021 The NATS Authors + * 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 { + assertStringIncludes, + fail, +} from "https://deno.land/std@0.95.0/testing/asserts.ts"; +import { connect } from "../src/connect.ts"; +import { createInbox, Empty } from "../nats-base-client/mod.ts"; + +Deno.test("timeout - request noMux stack is useful", async () => { + const nc = await connect({ servers: "demo.nats.io" }); + const subj = createInbox(); + try { + await nc.request(subj, Empty, { noMux: true, timeout: 250 }); + fail("request should have failed!"); + } catch (err) { + assertStringIncludes(err.stack, "timeout_test"); + } + await nc.close(); +}); + +Deno.test("timeout - request stack is useful", async () => { + const nc = await connect({ servers: "demo.nats.io" }); + const subj = createInbox(); + try { + await nc.request(subj, Empty, { timeout: 250 }); + fail("request should have failed!"); + } catch (err) { + assertStringIncludes(err.stack, "timeout_test"); + console.log(err.stack); + } + await nc.close(); +}); From bc2a40586ff272d684c674a67e22b4df9afaccee Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Mon, 20 Sep 2021 12:45:26 -0500 Subject: [PATCH 2/2] disabling coveralls - service is currently down --- .github/workflows/test.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 89190d9e..2cf07675 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -61,11 +61,11 @@ jobs: - name: Generate lcov run: deno coverage --unstable --lcov ./cov > cov.lcov - - name: Upload coverage - uses: coverallsapp/github-action@v1.1.2 - with: - github-token: ${{ secrets.github_token }} - path-to-lcov: ./cov.lcov +# - name: Upload coverage +# uses: coverallsapp/github-action@v1.1.2 +# with: +# github-token: ${{ secrets.github_token }} +# path-to-lcov: ./cov.lcov - name: Release uses: softprops/action-gh-release@v1