Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] stored context for requests so that if there's an error, the stack will be helpful #199

Merged
merged 2 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});