Skip to content

Commit c2e7f6f

Browse files
authored
chore(property-provider): extend CredentialsProviderError from ProviderError (#3438)
1 parent c7cceb5 commit c2e7f6f

File tree

6 files changed

+60
-44
lines changed

6 files changed

+60
-44
lines changed

packages/credential-provider-node/src/defaultProvider.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe(defaultProvider.name, () => {
6363
await errorFn();
6464
fail(`expected ${expectedError}`);
6565
} catch (error) {
66-
expect(error).toStrictEqual(expectedError);
66+
expect(error.toString()).toStrictEqual(expectedError.toString());
6767
}
6868

6969
expect(memoize).toHaveBeenCalledWith(mockChainFn, expect.any(Function), expect.any(Function));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CredentialsProviderError } from "./CredentialsProviderError";
2+
3+
describe(CredentialsProviderError.name, () => {
4+
it("should be named as CredentialsProviderError", () => {
5+
expect(new CredentialsProviderError("PANIC").name).toBe("CredentialsProviderError");
6+
});
7+
8+
describe("should be instanceof CredentialsProviderError", () => {
9+
it("when created using constructor", () => {
10+
expect(new CredentialsProviderError("PANIC")).toBeInstanceOf(CredentialsProviderError);
11+
});
12+
13+
it("when created using from()", () => {
14+
// ToDo: set instanceof to be CredentialsProviderError
15+
expect(CredentialsProviderError.from(new Error("PANIC"))).toBeInstanceOf(Error);
16+
});
17+
});
18+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ProviderError } from "./ProviderError";
2+
3+
/**
4+
* An error representing a failure of an individual credential provider.
5+
*
6+
* This error class has special meaning to the {@link chain} method. If a
7+
* provider in the chain is rejected with an error, the chain will only proceed
8+
* to the next provider if the value of the `tryNextLink` property on the error
9+
* is truthy. This allows individual providers to halt the chain and also
10+
* ensures the chain will stop if an entirely unexpected error is encountered.
11+
*/
12+
export class CredentialsProviderError extends ProviderError {
13+
name = "CredentialsProviderError";
14+
}
Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
import { CredentialsProviderError } from "./ProviderError";
1+
import { ProviderError } from "./ProviderError";
22

3-
describe("ProviderError", () => {
4-
it("should be named as CredentialsProviderError", () => {
5-
expect(new CredentialsProviderError("PANIC").name).toBe("CredentialsProviderError");
3+
describe(ProviderError.name, () => {
4+
it("should be named as ProviderError", () => {
5+
expect(new ProviderError("PANIC").name).toBe("ProviderError");
66
});
77

88
it("should direct the chain to proceed to the next link by default", () => {
9-
expect(new CredentialsProviderError("PANIC").tryNextLink).toBe(true);
9+
expect(new ProviderError("PANIC").tryNextLink).toBe(true);
1010
});
1111

1212
it("should allow errors to halt the chain", () => {
13-
expect(new CredentialsProviderError("PANIC", false).tryNextLink).toBe(false);
13+
expect(new ProviderError("PANIC", false).tryNextLink).toBe(false);
1414
});
1515

16-
describe("from()", () => {
17-
it("should create ProviderError from existing error", () => {
18-
const error = new Error("PANIC");
19-
// @ts-expect-error Property 'someValue' does not exist on type 'Error'.
20-
error.someValue = "foo";
21-
const providerError = CredentialsProviderError.from(error);
22-
// @ts-expect-error Property 'someValue' does not exist on type 'ProviderError'.
23-
expect(providerError.someValue).toBe("foo");
24-
expect(providerError.tryNextLink).toBe(true);
16+
describe("should be instanceof ProviderError", () => {
17+
it("when created using constructor", () => {
18+
expect(new ProviderError("PANIC")).toBeInstanceOf(ProviderError);
2519
});
20+
21+
it("when created using from()", () => {
22+
// ToDo: set instanceof to be CredentialsProviderError
23+
expect(ProviderError.from(new Error("PANIC"))).toBeInstanceOf(Error);
24+
});
25+
});
26+
27+
it("should create ProviderError from existing error", () => {
28+
const error = new Error("PANIC");
29+
// @ts-expect-error Property 'someValue' does not exist on type 'Error'.
30+
error.someValue = "foo";
31+
const providerError = ProviderError.from(error);
32+
// @ts-expect-error Property 'someValue' does not exist on type 'ProviderError'.
33+
expect(providerError.someValue).toBe("foo");
34+
expect(providerError.tryNextLink).toBe(true);
2635
});
2736
});
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/**
2-
* An error representing a failure of an individual credential provider.
2+
* An error representing a failure of an individual provider.
33
*
44
* This error class has special meaning to the {@link chain} method. If a
55
* provider in the chain is rejected with an error, the chain will only proceed
66
* to the next provider if the value of the `tryNextLink` property on the error
77
* is truthy. This allows individual providers to halt the chain and also
88
* ensures the chain will stop if an entirely unexpected error is encountered.
9-
*
10-
* @deprecated
119
*/
1210
export class ProviderError extends Error {
11+
name = "ProviderError";
1312
constructor(message: string, public readonly tryNextLink: boolean = true) {
1413
super(message);
1514
}
@@ -23,28 +22,3 @@ export class ProviderError extends Error {
2322
return error as ProviderError;
2423
}
2524
}
26-
27-
/**
28-
* An error representing a failure of an individual credential provider.
29-
*
30-
* This error class has special meaning to the {@link chain} method. If a
31-
* provider in the chain is rejected with an error, the chain will only proceed
32-
* to the next provider if the value of the `tryNextLink` property on the error
33-
* is truthy. This allows individual providers to halt the chain and also
34-
* ensures the chain will stop if an entirely unexpected error is encountered.
35-
*/
36-
export class CredentialsProviderError extends Error {
37-
readonly name = "CredentialsProviderError";
38-
constructor(message: string, public readonly tryNextLink: boolean = true) {
39-
super(message);
40-
}
41-
static from(error: Error, tryNextLink = true): CredentialsProviderError {
42-
Object.defineProperty(error, "tryNextLink", {
43-
value: tryNextLink,
44-
configurable: false,
45-
enumerable: false,
46-
writable: false,
47-
});
48-
return error as CredentialsProviderError;
49-
}
50-
}

packages/property-provider/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./CredentialsProviderError";
12
export * from "./ProviderError";
23
export * from "./chain";
34
export * from "./fromStatic";

0 commit comments

Comments
 (0)