Skip to content

Commit 26d4097

Browse files
committed
feat: added-cause-to-flagsmith-provider-error
Signed-off-by: wadii <[email protected]>
1 parent 491dc60 commit 26d4097

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

libs/providers/flagsmith/src/lib/exceptions.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import { ErrorCode } from '@openfeature/core';
22

3+
interface ErrorOptions {
4+
cause?: Error;
5+
}
6+
37
class FlagsmithProviderError extends Error {
4-
constructor(message: string, public code: ErrorCode) {
8+
public cause?: Error;
9+
constructor(
10+
message: string,
11+
public code: ErrorCode,
12+
options?: ErrorOptions,
13+
) {
514
super(message);
6-
this.name = "FlagsmithProviderError";
15+
this.name = 'FlagsmithProviderError';
16+
this.cause = options?.cause;
717
}
818
}
919

libs/providers/flagsmith/src/lib/flagsmith-provider.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ export default class FlagsmithOpenFeatureProvider implements Provider {
7979
}
8080

8181
private getFlags(evaluationContext: EvaluationContext): Promise<Flags> {
82+
const traits =
83+
evaluationContext.traits && typeof evaluationContext.traits === 'object'
84+
? (evaluationContext.traits as FlagsmithTrait)
85+
: {};
8286
if (evaluationContext?.targetingKey) {
83-
return this.client.getIdentityFlags(
84-
evaluationContext.targetingKey,
85-
(evaluationContext.traits as FlagsmithTrait | undefined) || {},
86-
);
87+
return this.client.getIdentityFlags(evaluationContext.targetingKey, traits);
8788
}
8889
return this.client.getEnvironmentFlags();
8990
}
@@ -110,7 +111,9 @@ export default class FlagsmithOpenFeatureProvider implements Provider {
110111
const flags = await this.getFlags(evaluationContext);
111112
flag = flags.getFlag(flagKey);
112113
} catch (error) {
113-
throw new FlagsmithProviderError('An error occurred retrieving flags from Flagsmith client.', ErrorCode.GENERAL);
114+
throw new FlagsmithProviderError('An error occurred retrieving flags from Flagsmith client.', ErrorCode.GENERAL, {
115+
cause: error as Error,
116+
});
114117
}
115118

116119
if (!flag || (!this.useFlagsmithDefaults && flag.isDefault)) {

0 commit comments

Comments
 (0)