Skip to content

Commit ce7c4ad

Browse files
authored
feat!: errorCode as enum, reason as string (#244)
- makes errorCode an enum - makes reason a string - adds errorMessage to resolution/evaluation details
1 parent 1f6eb21 commit ce7c4ad

19 files changed

+196
-82
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![codecov](https://codecov.io/gh/open-feature/js-sdk/branch/main/graph/badge.svg?token=3DC5XOEHMY)](https://codecov.io/gh/open-feature/js-sdk)
55
[![npm version](https://badge.fury.io/js/@openfeature%2Fjs-sdk.svg)](https://badge.fury.io/js/@openfeature%2Fjs-sdk)
66
[![Known Vulnerabilities](https://snyk.io/test/github/open-feature/js-sdk/badge.svg)](https://snyk.io/test/github/open-feature/js-sdk)
7-
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.4.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.4.0)
7+
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.5.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.0)
88

99
This is the JavaScript implementation of [OpenFeature](https://openfeature.dev), a vendor-agnostic abstraction library for evaluating feature flags.
1010

src/client.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { ERROR_REASON, GENERAL_ERROR } from './constants';
2-
import { OpenFeature } from './open-feature';
1+
import { OpenFeatureError } from './errors';
32
import { SafeLogger } from './logger';
3+
import { OpenFeature } from './open-feature';
44
import {
55
Client,
66
ClientMetadata,
7+
ErrorCode,
78
EvaluationContext,
89
EvaluationDetails,
910
FlagEvaluationOptions,
@@ -15,6 +16,7 @@ import {
1516
Logger,
1617
Provider,
1718
ResolutionDetails,
19+
StandardResolutionReasons,
1820
} from './types';
1921

2022
type OpenFeatureClientOptions = {
@@ -47,7 +49,7 @@ export class OpenFeatureClient implements Client {
4749
this._clientLogger = new SafeLogger(logger);
4850
return this;
4951
}
50-
52+
5153
setContext(context: EvaluationContext): OpenFeatureClient {
5254
this._context = context;
5355
return this;
@@ -223,14 +225,16 @@ export class OpenFeatureClient implements Client {
223225

224226
return evaluationDetails;
225227
} catch (err: unknown) {
226-
const errorCode = (!!err && (err as { code: string }).code) || GENERAL_ERROR;
228+
const errorMessage: string = (err as Error)?.message;
229+
const errorCode: ErrorCode = (err as OpenFeatureError)?.code || ErrorCode.GENERAL;
227230

228231
await this.errorHooks(allHooksReversed, hookContext, err, options);
229232

230233
return {
231234
errorCode,
235+
errorMessage,
232236
value: defaultValue,
233-
reason: ERROR_REASON,
237+
reason: StandardResolutionReasons.ERROR,
234238
flagKey,
235239
};
236240
} finally {

src/constants.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/errors/codes.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/errors/flag-not-found-error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { OpenFeatureError } from './error-abstract';
2-
import { ErrorCode } from './codes';
1+
import { ErrorCode } from '../types';
2+
import { OpenFeatureError } from './open-feature-error-abstract';
33

44
export class FlagNotFoundError extends OpenFeatureError {
55
code: ErrorCode;

src/errors/general-error.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ErrorCode } from '../types';
2+
import { OpenFeatureError } from './open-feature-error-abstract';
3+
4+
export class GeneralError extends OpenFeatureError {
5+
code: ErrorCode;
6+
constructor(message?: string) {
7+
super(message);
8+
Object.setPrototypeOf(this, GeneralError.prototype);
9+
this.name = 'GeneralError';
10+
this.code = ErrorCode.GENERAL;
11+
}
12+
}

src/errors/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
export * from './codes';
1+
export * from './general-error';
22
export * from './flag-not-found-error';
33
export * from './parse-error';
44
export * from './type-mismatch-error';
5-
export * from './error-abstract';
5+
export * from './targeting-key-missing-error';
6+
export * from './invalid-context-error';
7+
export * from './open-feature-error-abstract';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ErrorCode } from '../types';
2+
import { OpenFeatureError } from './open-feature-error-abstract';
3+
4+
export class InvalidContextError extends OpenFeatureError {
5+
code: ErrorCode;
6+
constructor(message?: string) {
7+
super(message);
8+
Object.setPrototypeOf(this, InvalidContextError.prototype);
9+
this.name = 'InvalidContextError';
10+
this.code = ErrorCode.INVALID_CONTEXT;
11+
}
12+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ErrorCode } from './codes';
1+
import { ErrorCode } from '../types';
22

33
export abstract class OpenFeatureError extends Error {
44
abstract code: ErrorCode;

src/errors/parse-error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { OpenFeatureError } from './error-abstract';
2-
import { ErrorCode } from './codes';
1+
import { ErrorCode } from '../types';
2+
import { OpenFeatureError } from './open-feature-error-abstract';
33

44
export class ParseError extends OpenFeatureError {
55
code: ErrorCode;

0 commit comments

Comments
 (0)