Skip to content

Commit

Permalink
[feat] stored context for requests so that if there's an error, the s…
Browse files Browse the repository at this point in the history
…tack will be helpful.
  • Loading branch information
aricart committed Sep 20, 2021
1 parent c560da3 commit cdb36ee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
7 changes: 7 additions & 0 deletions nats-base-client/nats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,24 @@ export class NatsConnectionImpl implements NatsConnection {
? opts.reply
: createInbox(this.options.inboxPrefix);
const d = deferred<Msg>();
const errCtx = new Error();
this.subscribe(
inbox,
{
max: 1,
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);
Expand Down
3 changes: 3 additions & 0 deletions nats-base-client/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Request {
received: number;
deferred: Deferred<Msg>;
timer: Timeout<Msg>;
ctx: Error;
private mux: MuxSubscription;

constructor(
Expand All @@ -35,13 +36,15 @@ export class Request {
this.token = nuid.next();
extend(this, opts);
this.timer = timeout<Msg>(opts.timeout);
this.ctx = new Error();
}

resolver(err: Error | null, msg: Msg): void {
if (this.timer) {
this.timer.cancel();
}
if (err) {
err.stack += `\n\n${this.ctx.stack}`;
this.deferred.reject(err);
} else {
this.deferred.resolve(msg);
Expand Down
46 changes: 46 additions & 0 deletions tests/timeout_test.ts
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit cdb36ee

Please sign in to comment.