Skip to content
Closed
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
Prev Previous commit
Next Next commit
refactor(web,server): throw open feature errors to simplify get flag …
…logic
  • Loading branch information
Billlynch committed Nov 21, 2023
commit 8fbf7ef4bbf122242e83e2e74a35130efde58de1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorCode, Logger, ProviderStatus } from '@openfeature/web-sdk';
import { FlagNotFoundError, Logger, ParseError, ProviderStatus, TypeMismatchError } from '@openfeature/js-sdk';
import { ConfidenceClient, Configuration } from '@spotify-confidence/client-http';
import { ConfidenceServerProvider } from './ConfidenceServerProvider';

Expand Down Expand Up @@ -125,33 +125,21 @@ describe('ConfidenceServerProvider', () => {
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveBooleanEvaluation('notARealFlag.bool', false, {}, dummyConsole);

expect(actual).toEqual({
value: false,
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveBooleanEvaluation('notARealFlag.bool', false, {}, dummyConsole),
).rejects.toThrow(new FlagNotFoundError('Flag "notARealFlag" was not found'));
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveBooleanEvaluation('testFlag.str', false, {}, dummyConsole);

expect(actual).toEqual({
value: false,
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveBooleanEvaluation('testFlag.str', false, {}, dummyConsole)).rejects.toThrow(
TypeMismatchError,
);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveBooleanEvaluation('testFlag.404', false, {}, dummyConsole);

expect(actual).toEqual({
value: false,
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveBooleanEvaluation('testFlag.404', false, {}, dummyConsole)).rejects.toThrow(
ParseError,
);
});
});

Expand Down Expand Up @@ -201,33 +189,21 @@ describe('ConfidenceServerProvider', () => {
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveNumberEvaluation('notARealFlag.int', 1, {}, dummyConsole);

expect(actual).toEqual({
value: 1,
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveNumberEvaluation('notARealFlag.int', 1, {}, dummyConsole)).rejects.toThrow(
FlagNotFoundError,
);
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveNumberEvaluation('testFlag.str', 1, {}, dummyConsole);

expect(actual).toEqual({
value: 1,
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveNumberEvaluation('testFlag.str', 1, {}, dummyConsole)).rejects.toThrow(
TypeMismatchError,
);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveNumberEvaluation('testFlag.404', 1, {}, dummyConsole);

expect(actual).toEqual({
value: 1,
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveNumberEvaluation('testFlag.404', 1, {}, dummyConsole)).rejects.toThrow(
ParseError,
);
});
});

Expand Down Expand Up @@ -255,43 +231,27 @@ describe('ConfidenceServerProvider', () => {
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('notARealFlag.str', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('notARealFlag.str', 'default', {}, dummyConsole),
).rejects.toThrow(new FlagNotFoundError('Flag "notARealFlag" was not found'));
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('testFlag.int', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('testFlag.int', 'default', {}, dummyConsole),
).rejects.toThrow(TypeMismatchError);
});

it('should return default if the flag requested is the wrong type from nested obj', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('testFlag.obj.int', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('testFlag.obj.int', 'default', {}, dummyConsole),
).rejects.toThrow(TypeMismatchError);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveStringEvaluation('testFlag.404', 'default', {}, dummyConsole);

expect(actual).toEqual({
value: 'default',
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() =>
instanceUnderTest.resolveStringEvaluation('testFlag.404', 'default', {}, dummyConsole),
).rejects.toThrow(ParseError);
});
});

Expand Down Expand Up @@ -338,52 +298,34 @@ describe('ConfidenceServerProvider', () => {
});

it('should resolve a full object with type mismatch default', async () => {
expect(
await instanceUnderTest.resolveObjectEvaluation(
expect(() =>
instanceUnderTest.resolveObjectEvaluation(
'testFlag.obj',
{
testBool: false,
},
{},
dummyConsole,
),
).toEqual({
errorCode: 'TYPE_MISMATCH',
reason: 'ERROR',
value: {
testBool: false,
},
});
).rejects.toThrow(TypeMismatchError);
});

it('should return default if the flag is not found', async () => {
const actual = await instanceUnderTest.resolveObjectEvaluation('notARealFlag.obj', {}, {}, dummyConsole);

expect(actual).toEqual({
value: {},
errorCode: ErrorCode.FLAG_NOT_FOUND,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveObjectEvaluation('notARealFlag.obj', {}, {}, dummyConsole)).rejects.toThrow(
new FlagNotFoundError('Flag "notARealFlag" was not found'),
);
});

it('should return default if the flag requested is the wrong type', async () => {
const actual = await instanceUnderTest.resolveObjectEvaluation('testFlag.str', {}, {}, dummyConsole);

expect(actual).toEqual({
value: {},
errorCode: ErrorCode.TYPE_MISMATCH,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveObjectEvaluation('testFlag.str', {}, {}, dummyConsole)).rejects.toThrow(
TypeMismatchError,
);
});

it('should return default if the value requested is not in the flag schema', async () => {
const actual = await instanceUnderTest.resolveObjectEvaluation('testFlag.404', {}, {}, dummyConsole);

expect(actual).toEqual({
value: {},
errorCode: ErrorCode.PARSE_ERROR,
reason: 'ERROR',
});
expect(() => instanceUnderTest.resolveObjectEvaluation('testFlag.404', {}, {}, dummyConsole)).rejects.toThrow(
ParseError,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
ErrorCode,
EvaluationContext,
FlagNotFoundError,
JsonValue,
Logger,
ParseError,
Provider,
ProviderMetadata,
ProviderStatus,
ResolutionDetails,
ResolutionReason,
TypeMismatchError,
} from '@openfeature/js-sdk';

import { ConfidenceClient, ResolveContext, Configuration } from '@spotify-confidence/client-http';
Expand Down Expand Up @@ -38,7 +41,7 @@ export class ConfidenceServerProvider implements Provider {
configuration: Configuration,
flagKey: string,
defaultValue: T,
_logger: Logger,
logger: Logger,
): ResolutionDetails<T> {
if (!configuration) {
return {
Expand All @@ -49,63 +52,43 @@ export class ConfidenceServerProvider implements Provider {
}

const [flagName, ...pathParts] = flagKey.split('.');
try {
const flag = configuration.flags[flagName];

if (!flag) {
return {
errorCode: ErrorCode.FLAG_NOT_FOUND,
value: defaultValue,
reason: 'ERROR',
};
}

if (Configuration.ResolveReason.NoSegmentMatch === flag.reason) {
return {
value: defaultValue,
reason: 'DEFAULT',
};
}

let flagValue: Configuration.FlagValue;
try {
flagValue = Configuration.FlagValue.traverse(flag, pathParts.join('.'));
} catch (e) {
return {
errorCode: 'PARSE_ERROR' as ErrorCode,
value: defaultValue,
reason: 'ERROR',
};
}
if (flagValue.value === null) {
return {
value: defaultValue,
reason: mapConfidenceReason(flag.reason),
};
}
if (!Configuration.FlagValue.matches(flagValue, defaultValue)) {
return {
errorCode: 'TYPE_MISMATCH' as ErrorCode,
value: defaultValue,
reason: 'ERROR',
};
}

const flag = configuration.flags[flagName];

if (!flag) {
logger.warn('Flag "%s" was not found', flagName);
throw new FlagNotFoundError(`Flag "${flagName}" was not found`);
}

if (Configuration.ResolveReason.NoSegmentMatch === flag.reason) {
return {
value: flagValue.value as T,
reason: mapConfidenceReason(flag.reason),
variant: flag.variant,
flagMetadata: {
resolveToken: configuration.resolveToken || '',
},
};
} catch (e) {
return {
errorCode: ErrorCode.GENERAL,
value: defaultValue,
reason: 'ERROR',
reason: 'DEFAULT',
};
}

let flagValue: Configuration.FlagValue;
try {
flagValue = Configuration.FlagValue.traverse(flag, pathParts.join('.'));
} catch (e) {
logger.warn('Value with path "%s" was not found in flag "%s"', pathParts.join('.'), flagName);
throw new ParseError();
}

if (!Configuration.FlagValue.matches(flagValue, defaultValue)) {
logger.warn('Value for "%s" is of incorrect type', flagKey);
throw new TypeMismatchError();
}

logger.info('Value for "%s" successfully evaluated', flagKey);
return {
value: flagValue.value === null ? defaultValue : (flagValue.value as T),
reason: mapConfidenceReason(flag.reason),
variant: flag.variant,
flagMetadata: {
resolveToken: configuration.resolveToken || '',
},
};
}

private async fetchFlag<T>(
Expand Down
Loading