perf: avoid using exceptions for flow control (#1074)
## This PR - avoids using error codes as flow control during an evaluation Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com>
This commit is contained in:
parent
418409e3fa
commit
26264d6d09
|
|
@ -294,26 +294,17 @@ export class OpenFeatureClient implements Client {
|
|||
};
|
||||
|
||||
if (evaluationDetails.errorCode) {
|
||||
throw instantiateErrorByErrorCode(evaluationDetails.errorCode);
|
||||
const err = instantiateErrorByErrorCode(evaluationDetails.errorCode);
|
||||
await this.errorHooks(allHooksReversed, hookContext, err, options);
|
||||
return this.getErrorEvaluationDetails(flagKey, defaultValue, err);
|
||||
}
|
||||
|
||||
await this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options);
|
||||
|
||||
return evaluationDetails;
|
||||
} catch (err: unknown) {
|
||||
const errorMessage: string = (err as Error)?.message;
|
||||
const errorCode: ErrorCode = (err as OpenFeatureError)?.code || ErrorCode.GENERAL;
|
||||
|
||||
await this.errorHooks(allHooksReversed, hookContext, err, options);
|
||||
|
||||
return {
|
||||
errorCode,
|
||||
errorMessage,
|
||||
value: defaultValue,
|
||||
reason: StandardResolutionReasons.ERROR,
|
||||
flagMetadata: Object.freeze({}),
|
||||
flagKey,
|
||||
};
|
||||
return this.getErrorEvaluationDetails(flagKey, defaultValue, err);
|
||||
} finally {
|
||||
await this.finallyHooks(allHooksReversed, hookContext, options);
|
||||
}
|
||||
|
|
@ -407,4 +398,22 @@ export class OpenFeatureClient implements Client {
|
|||
throw new ProviderFatalError('provider is in an irrecoverable error state');
|
||||
}
|
||||
}
|
||||
|
||||
private getErrorEvaluationDetails<T extends FlagValue>(
|
||||
flagKey: string,
|
||||
defaultValue: T,
|
||||
err: unknown,
|
||||
): EvaluationDetails<T> {
|
||||
const errorMessage: string = (err as Error)?.message;
|
||||
const errorCode: ErrorCode = (err as OpenFeatureError)?.code || ErrorCode.GENERAL;
|
||||
|
||||
return {
|
||||
errorCode,
|
||||
errorMessage,
|
||||
value: defaultValue,
|
||||
reason: StandardResolutionReasons.ERROR,
|
||||
flagMetadata: Object.freeze({}),
|
||||
flagKey,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -249,26 +249,17 @@ export class OpenFeatureClient implements Client {
|
|||
};
|
||||
|
||||
if (evaluationDetails.errorCode) {
|
||||
throw instantiateErrorByErrorCode(evaluationDetails.errorCode);
|
||||
const err = instantiateErrorByErrorCode(evaluationDetails.errorCode);
|
||||
this.errorHooks(allHooksReversed, hookContext, err, options);
|
||||
return this.getErrorEvaluationDetails(flagKey, defaultValue, err);
|
||||
}
|
||||
|
||||
this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options);
|
||||
|
||||
return evaluationDetails;
|
||||
} catch (err: unknown) {
|
||||
const errorMessage: string = (err as Error)?.message;
|
||||
const errorCode: ErrorCode = (err as OpenFeatureError)?.code || ErrorCode.GENERAL;
|
||||
|
||||
this.errorHooks(allHooksReversed, hookContext, err, options);
|
||||
|
||||
return {
|
||||
errorCode,
|
||||
errorMessage,
|
||||
value: defaultValue,
|
||||
reason: StandardResolutionReasons.ERROR,
|
||||
flagMetadata: Object.freeze({}),
|
||||
flagKey,
|
||||
};
|
||||
return this.getErrorEvaluationDetails(flagKey, defaultValue, err);
|
||||
} finally {
|
||||
this.finallyHooks(allHooksReversed, hookContext, options);
|
||||
}
|
||||
|
|
@ -341,4 +332,22 @@ export class OpenFeatureClient implements Client {
|
|||
throw new ProviderFatalError('provider is in an irrecoverable error state');
|
||||
}
|
||||
}
|
||||
|
||||
private getErrorEvaluationDetails<T extends FlagValue>(
|
||||
flagKey: string,
|
||||
defaultValue: T,
|
||||
err: unknown,
|
||||
): EvaluationDetails<T> {
|
||||
const errorMessage: string = (err as Error)?.message;
|
||||
const errorCode: ErrorCode = (err as OpenFeatureError)?.code || ErrorCode.GENERAL;
|
||||
|
||||
return {
|
||||
errorCode,
|
||||
errorMessage,
|
||||
value: defaultValue,
|
||||
reason: StandardResolutionReasons.ERROR,
|
||||
flagMetadata: Object.freeze({}),
|
||||
flagKey,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue