Skip to content

Commit 8da9097

Browse files
fix: make coverage reports genhtml-compatible (#803)
* fix: adjusting errors setup so it doesn't break coverage * fix: change setup to fix coverage exports
1 parent 7b027a0 commit 8da9097

3 files changed

Lines changed: 63 additions & 17 deletions

File tree

src/consumer.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ export class Consumer extends TypedEventEmitter {
9393
this.alwaysAcknowledge = options.alwaysAcknowledge ?? false;
9494
this.extendedAWSErrors = options.extendedAWSErrors ?? false;
9595
this.strictReturn = options.strictReturn ?? false;
96-
this.sqs =
97-
options.sqs ||
98-
new SQSClient({
99-
useQueueUrlAsEndpoint: options.useQueueUrlAsEndpoint ?? true,
100-
region: options.region || process.env.AWS_REGION || "eu-west-1",
101-
});
96+
if (options.sqs) {
97+
this.sqs = options.sqs;
98+
} else {
99+
const useQueueUrlAsEndpoint = options.useQueueUrlAsEndpoint ?? true;
100+
const region = options.region || process.env.AWS_REGION || "eu-west-1";
101+
this.sqs = new SQSClient({ useQueueUrlAsEndpoint, region });
102+
}
102103
}
103104

104105
/**
@@ -131,10 +132,12 @@ export class Consumer extends TypedEventEmitter {
131132
* A reusable options object for sqs.send that's used to avoid duplication.
132133
*/
133134
private get sqsSendOptions(): { abortSignal: AbortSignal } {
135+
const abortSignal = this.abortController?.signal || new AbortController().signal;
136+
134137
return {
135138
// return the current abortController signal or a fresh signal that has not been aborted.
136139
// This effectively defaults the signal sent to the AWS SDK to not aborted
137-
abortSignal: this.abortController?.signal || new AbortController().signal,
140+
abortSignal,
138141
};
139142
}
140143

@@ -261,13 +264,15 @@ export class Consumer extends TypedEventEmitter {
261264
try {
262265
this.emitError(err);
263266
} catch (listenerErr) {
264-
logger.warn(
265-
`An error event listener threw an error: ${listenerErr instanceof Error ? listenerErr.message : String(listenerErr)}`,
266-
);
267+
const listenerErrorMessage =
268+
listenerErr instanceof Error ? listenerErr.message : String(listenerErr);
269+
logger.warn(`An error event listener threw an error: ${listenerErrorMessage}`);
267270
}
268271
if (isConnectionError(err)) {
272+
const errorCode = err.code || "Unknown";
273+
269274
logger.debug("authentication_error", {
270-
code: err.code || "Unknown",
275+
code: errorCode,
271276
detail: "There was an authentication error. Pausing before retrying.",
272277
});
273278
currentPollingTimeout = this.authenticationErrorTimeout;

src/errors.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { Message } from "@aws-sdk/client-sqs";
22

33
import type { AWSError } from "./types.js";
44

5+
const DEFAULT_TIMEOUT_ERROR_MESSAGE = "Operation timed out.";
6+
const DEFAULT_STANDARD_ERROR_MESSAGE = "An unexpected error occurred:";
7+
58
class SQSError extends Error {
69
code: string;
710
cause: AWSError;
@@ -26,9 +29,11 @@ class TimeoutError extends Error {
2629
cause: Error;
2730
time: Date;
2831

29-
constructor(message = "Operation timed out.") {
30-
super(message);
31-
this.message = message;
32+
constructor(message?: string) {
33+
const errorMessage = message === undefined ? DEFAULT_TIMEOUT_ERROR_MESSAGE : message;
34+
35+
super(errorMessage);
36+
this.message = errorMessage;
3237
this.name = "TimeoutError";
3338
this.messageIds = [];
3439
}
@@ -39,9 +44,11 @@ class StandardError extends Error {
3944
cause: Error;
4045
time: Date;
4146

42-
constructor(message = "An unexpected error occurred:") {
43-
super(message);
44-
this.message = message;
47+
constructor(message?: string) {
48+
const errorMessage = message === undefined ? DEFAULT_STANDARD_ERROR_MESSAGE : message;
49+
50+
super(errorMessage);
51+
this.message = errorMessage;
4552
this.name = "StandardError";
4653
this.messageIds = [];
4754
}

test/tests/errors.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { assert } from "chai";
2+
import { describe, it } from "vitest";
3+
4+
import { StandardError, TimeoutError } from "../../src/errors.js";
5+
6+
describe("errors", () => {
7+
describe("TimeoutError", () => {
8+
it("uses the default message when none is provided", () => {
9+
const error = new TimeoutError();
10+
11+
assert.equal(error.message, "Operation timed out.");
12+
});
13+
14+
it("uses the provided message", () => {
15+
const error = new TimeoutError("Custom timeout.");
16+
17+
assert.equal(error.message, "Custom timeout.");
18+
});
19+
});
20+
21+
describe("StandardError", () => {
22+
it("uses the default message when none is provided", () => {
23+
const error = new StandardError();
24+
25+
assert.equal(error.message, "An unexpected error occurred:");
26+
});
27+
28+
it("uses the provided message", () => {
29+
const error = new StandardError("Custom failure.");
30+
31+
assert.equal(error.message, "Custom failure.");
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)