Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 3 additions & 11 deletions src/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Switcher } from './switcher';
import { SwitcherResult } from './lib/result';

/**
* Quick start with the following 3 steps.
Expand Down Expand Up @@ -118,15 +119,6 @@ export class Client {

}

/**
* ResultDetail is a detailed response from the API.
*/
export type ResultDetail = {
result: boolean;
reason: string | undefined;
metadata: object | undefined;
}

/**
* Criteria defines the condition(s) to evaluate the switcher when using Client.assume(key)
*/
Expand All @@ -149,7 +141,7 @@ export type Criteria = {
export type LoggerRecord = {
key: string;
input: string[][];
response: ResultDetail
response: SwitcherResult
}

/**
Expand Down Expand Up @@ -303,5 +295,5 @@ declare class Key {
/**
* Return key response
*/
getResponse(input?: string[][]): ResultDetail;
getResponse(input?: string[][]): SwitcherResult;
}
10 changes: 2 additions & 8 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import TimedMatch from './lib/utils/timed-match/index.js';
import ExecutionLogger from './lib/utils/executionLogger.js';
import SnapshotAutoUpdater from './lib/utils/snapshotAutoUpdater.js';
import { SnapshotNotFoundError } from './lib/exceptions/index.js';
import { ClientError } from './lib/exceptions/index.js';
import { loadDomain, validateSnapshot, checkSwitchersLocal } from './lib/snapshot.js';
import { Switcher } from './switcher.js';
import { Auth } from './lib/remoteAuth.js';
Expand Down Expand Up @@ -106,7 +106,7 @@ export class Client {

static async checkSnapshot() {
if (!GlobalSnapshot.snapshot) {
throw new SnapshotNotFoundError('Snapshot is not loaded. Use Client.loadSnapshot()');
throw new ClientError('Snapshot is not loaded. Use Client.loadSnapshot()');
}

if (Auth.isTokenExpired()) {
Expand Down Expand Up @@ -264,10 +264,4 @@ export class SwitcherOptions {
static build() {
return new SwitcherOptions();
}
}

export class ResultDetail {
static build() {
return new ResultDetail();
}
}
7 changes: 2 additions & 5 deletions src/lib/bypasser/key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SwitcherResult } from '../result.js';
import Criteria from './criteria.js';

/**
Expand Down Expand Up @@ -67,11 +68,7 @@ export default class Key {
result = this.#getResultBasedOnCriteria(this.#criteria, input);
}

return {
result,
reason: this.#reason,
metadata: this.#metadata,
};
return SwitcherResult.create(result, this.#reason, this.#metadata);
}

#getResultBasedOnCriteria(criteria, input) {
Expand Down
44 changes: 15 additions & 29 deletions src/lib/exceptions/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
export class AuthError extends Error {
constructor(message) {
super(`Something went wrong: ${message}`);
this.name = this.constructor.name;
}
export class ClientError extends Error {
constructor(message) {
super(`Something went wrong: ${message}`);
this.name = this.constructor.name;
}
}

export class CriteriaError extends Error {
constructor(message) {
super(`Something went wrong: ${message}`);
this.name = this.constructor.name;
}
export class RemoteError extends ClientError {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}

export class CheckSwitcherError extends Error {
constructor(notFound) {
super(`Something went wrong: [${notFound}] not found`);
this.name = this.constructor.name;
}
export class CheckSwitcherError extends ClientError {
constructor(notFound) {
super(`[${notFound}] not found`);
this.name = this.constructor.name;
}
}

export class SnapshotServiceError extends Error {
constructor(message) {
super(`Something went wrong: ${message}`);
this.name = this.constructor.name;
}
}

export class SnapshotNotFoundError extends Error {
constructor(message) {
super(`Something went wrong: ${message}`);
this.name = this.constructor.name;
}
}
38 changes: 22 additions & 16 deletions src/lib/remote.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import fs from 'node:fs';
import { Agent } from 'node:https';

import { AuthError, CheckSwitcherError, CriteriaError, SnapshotServiceError } from './exceptions/index.js';
import { CheckSwitcherError, ClientError, RemoteError } from './exceptions/index.js';
import FetchFacade from './utils/fetchFacade.js';
import * as util from './utils/index.js';
import { GlobalAuth } from './globals/globalAuth.js';

let httpClient;

const getConnectivityError = (code) => `Connection has been refused - ${code}`;

const getHeader = (token) => {
return {
'Authorization': `Bearer ${token}`,
Expand All @@ -19,7 +17,7 @@ const getHeader = (token) => {

export function setCerts(certPath) {
if (!fs.existsSync(certPath)) {
throw new Error(`Invalid certificate path '${certPath}'`);
throw new RemoteError(`Invalid certificate path '${certPath}'`);
}

httpClient = new Agent({
Expand All @@ -36,7 +34,7 @@ export function getEntry(input) {
return undefined;
}

let entry = [];
const entry = [];
for (const inputValues of input) {
entry.push({
strategy: inputValues[0],
Expand Down Expand Up @@ -67,9 +65,9 @@ export async function auth(context) {
return response.json();
}

throw new Error(`[auth] failed with status ${response.status}`);
throw new RemoteError(`[auth] failed with status ${response.status}`);
} catch (e) {
throw new AuthError(e.errno ? getConnectivityError(e.errno) : e.message);
throw errorHandler(e);
}
}

Expand All @@ -95,10 +93,10 @@ export async function checkCriteria(key, input, showDetail = false) {
if (response.status == 200) {
return response.json();
}

throw new Error(`[checkCriteria] failed with status ${response.status}`);
throw new RemoteError(`[checkCriteria] failed with status ${response.status}`);
} catch (e) {
throw new CriteriaError(e.errno ? getConnectivityError(e.errno) : e.message);
throw errorHandler(e);
}
}

Expand All @@ -112,15 +110,15 @@ export async function checkSwitchers(switcherKeys) {
});

if (response.status != 200) {
throw new Error(`[checkSwitchers] failed with status ${response.status}`);
throw new RemoteError(`[checkSwitchers] failed with status ${response.status}`);
}

const json = response.json();
if (json.not_found.length) {
throw new CheckSwitcherError(json.not_found);
}
} catch (e) {
throw new CriteriaError(e.errno ? getConnectivityError(e.errno) : e.message);
throw errorHandler(e);
}
}

Expand All @@ -136,9 +134,9 @@ export async function checkSnapshotVersion(version) {
return response.json();
}

throw new Error(`[checkSnapshotVersion] failed with status ${response.status}`);
throw new RemoteError(`[checkSnapshotVersion] failed with status ${response.status}`);
} catch (e) {
throw new SnapshotServiceError(e.errno ? getConnectivityError(e.errno) : e.message);
throw errorHandler(e);
}
}

Expand Down Expand Up @@ -171,8 +169,16 @@ export async function resolveSnapshot(domain, environment, component) {
return JSON.stringify(response.json(), null, 4);
}

throw new Error(`[resolveSnapshot] failed with status ${response.status}`);
throw new RemoteError(`[resolveSnapshot] failed with status ${response.status}`);
} catch (e) {
throw new SnapshotServiceError(e.errno ? getConnectivityError(e.errno) : e.message);
throw errorHandler(e);
}
}

function errorHandler(e) {
if (!(e instanceof ClientError)) {
throw new RemoteError(e.errno ? `Connection has been refused - ${e.errno}` : e.message);
}

throw e;
}
Loading