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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "skyflow-js",
"preferGlobal": true,
"analyze": false,
"version": "2.6.0",
"version": "2.6.0-dev.189f371",
"author": "Skyflow",
"description": "Skyflow JavaScript SDK",
"homepage": "https://github.com/skyflowapi/skyflow-js",
Expand Down
73 changes: 64 additions & 9 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getMetaObject,
} from '../utils/helpers';
import { ClientMetadata } from '../core/internal/internal-types';
import { ErrorMessages, ErrorType } from '../utils/common';

export interface IClientRequest {
body?: Document | XMLHttpRequestBodyInit | null;
Expand Down Expand Up @@ -42,11 +43,27 @@ class Client {

#metaData: ClientMetadata;

errorMessagesList: Partial<ErrorMessages> = {};

constructor(config: ISkyflow, metadata: ClientMetadata) {
this.config = config;
this.#metaData = metadata;
}

setErrorMessages(messages: ErrorMessages) {
this.errorMessagesList = {
...messages,
};
}

// eslint-disable-next-line class-methods-use-this
#getErrorTypeKey(value: number): ErrorType | string {
const valStr = String(value);
const key = (Object.keys(ErrorType) as Array<keyof typeof ErrorType>)
.find((keys) => ErrorType[keys] === valStr);
return key ? ErrorType[key] : String(value);
}

toJSON(): ClientToJSON {
return {
config: this.config,
Expand Down Expand Up @@ -102,24 +119,39 @@ class Client {
const contentType = headerMap['content-type'];
const requestId = headerMap['x-request-id'];
if (httpRequest.status < 200 || httpRequest.status >= 400) {
const overrideCodes = [400, 401, 403, 404, 429, 500, 502, 503];
if (contentType && contentType.includes('application/json')) {
let description = JSON.parse(httpRequest.response);
if (description?.error?.message) {
description = requestId ? `${description?.error?.message} - requestId: ${requestId}` : description?.error?.message;
}
if (overrideCodes.includes(httpRequest.status)) {
description = this.errorMessagesList[httpRequest.status] ?? description;
}
reject(new SkyflowError({
code: httpRequest.status,
description,
type: this.#getErrorTypeKey(httpRequest.status),
}, [], true));
} else if (contentType && contentType.includes('text/plain')) {
let description = requestId ? `${httpRequest.response} - requestId: ${requestId}` : httpRequest.response;
if (overrideCodes.includes(httpRequest.status)) {
description = this.errorMessagesList[httpRequest.status] ?? description;
}
reject(new SkyflowError({
code: httpRequest.status,
description: requestId ? `${httpRequest.response} - requestId: ${requestId}` : httpRequest.response,
description,
type: this.#getErrorTypeKey(httpRequest.status),
}, [], true));
} else {
let description = requestId ? `${logs.errorLogs.ERROR_OCCURED} - requestId: ${requestId}` : logs.errorLogs.ERROR_OCCURED;
if (overrideCodes.includes(httpRequest.status)) {
description = this.errorMessagesList[httpRequest.status] ?? description;
}
reject(new SkyflowError({
code: httpRequest.status,
description: requestId ? `${logs.errorLogs.ERROR_OCCURED} - requestId: ${requestId}` : logs.errorLogs.ERROR_OCCURED,
description,
type: this.#getErrorTypeKey(httpRequest.status),
}, [], true));
}
}
Expand All @@ -132,24 +164,47 @@ class Client {
httpRequest.onerror = () => {
const isOffline = typeof navigator !== 'undefined' && !navigator.onLine;
if (isOffline) {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.OFFLINE_ERROR, [], true));
reject(new SkyflowError({
code: httpRequest.status,
description: this.errorMessagesList.OFFLINE
?? SKYFLOW_ERROR_CODE.OFFLINE_ERROR.description,
type: ErrorType.OFFLINE,
}, [], true));
return;
}

if (httpRequest.status === 0) {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.GENERIC_ERROR, [], true));
reject(new SkyflowError({
code: httpRequest.status,
description: this.errorMessagesList.NETWORK_GENERIC
?? SKYFLOW_ERROR_CODE.GENERIC_ERROR.description,
type: ErrorType.NETWORK_GENERIC,
}, [], true));
return;
}

reject(new SkyflowError(SKYFLOW_ERROR_CODE.GENERIC_ERROR, [], true));
reject(new SkyflowError({
code: httpRequest.status,
description: this.errorMessagesList.NETWORK_GENERIC
?? SKYFLOW_ERROR_CODE.GENERIC_ERROR.description,
type: ErrorType.NETWORK_GENERIC,
}, [], true));
};

httpRequest.ontimeout = () => {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.TIMEOUT_ERROR, [], true));
reject(new SkyflowError({
code: httpRequest.status,
description: this.errorMessagesList.TIMEOUT
?? SKYFLOW_ERROR_CODE.TIMEOUT_ERROR.description,
type: ErrorType.TIMEOUT,
}, [], true));
};

httpRequest.onabort = () => {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.ABORT_ERROR, [], true));
reject(new SkyflowError({
code: httpRequest.status,
description: this.errorMessagesList.ABORT
?? SKYFLOW_ERROR_CODE.ABORT_ERROR.description,
type: ErrorType.ABORT,
}, [], true));
};
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/core-utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export const updateRecordsBySkyflowID = async (
error: {
code: rejectedResult?.error?.code,
description: rejectedResult?.error?.description,
type: rejectedResult?.error?.type,
},
};
}
Expand Down Expand Up @@ -342,6 +343,7 @@ export const updateRecordsBySkyflowIDComposable = async (
error: {
code: rejectedResult?.error?.code,
description: rejectedResult?.error?.description,
type: rejectedResult?.error?.type,
},
};
}
Expand Down Expand Up @@ -409,6 +411,7 @@ export const insertDataInCollect = async (
error: {
code: error?.error?.code,
description: error?.error?.description,
type: error?.error?.type,
},
},
],
Expand Down Expand Up @@ -453,6 +456,7 @@ export const insertDataInMultipleFiles = async (
error: {
code: error?.error?.code,
description: error?.error?.description,
type: error?.error?.type,
},
},
],
Expand Down
33 changes: 24 additions & 9 deletions src/core-utils/reveal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,33 @@ const formatForPureJsSuccess = (response: IApiSuccessResponse) => {
{ token: record.token, value: record.value, valueType: record.valueType }));
};

const formatForPureJsFailure = (cause, tokenId:string) => ({
token: tokenId,
...new SkyflowError({
code: cause?.error?.code,
description: cause?.error?.description,
}, [], true),
});
const formatForPureJsFailure = (cause, tokenId:string, purejs: boolean) => {
if (purejs) {
return {
token: tokenId,
error: {
code: cause?.error?.code,
description: cause?.error?.description,
},
};
}
return ({
token: tokenId,
...new SkyflowError({
code: cause?.error?.code,
description: cause?.error?.description,
type: cause?.error?.type,
}, [], true),
});
};

const formatForRenderFileFailure = (cause, skyflowID:string, column: string) => ({
skyflowId: skyflowID,
column,
error: {
code: cause?.error?.code,
description: cause?.error?.description,
type: cause?.error?.type,
},
});

Expand Down Expand Up @@ -207,6 +221,7 @@ export const getFileURLFromVaultBySkyflowIDComposable = (
export const fetchRecordsByTokenId = (
tokenIdRecords: IRevealRecord[],
client: Client,
purejs: boolean,
): Promise<IRevealResponseType> => new Promise((rootResolve, rootReject) => {
const clientId = client.toJSON()?.metaData?.uuid || '';
getAccessToken(clientId).then((authToken) => {
Expand All @@ -223,7 +238,7 @@ export const fetchRecordsByTokenId = (
apiResponse.push(...fieldsData);
},
(cause: any) => {
const errorData = formatForPureJsFailure(cause, tokenRecord.token as string);
const errorData = formatForPureJsFailure(cause, tokenRecord.token as string, purejs);
printLog(errorData.error?.description || '', MessageType.ERROR, LogLevel.ERROR);
apiResponse.push(errorData);
},
Expand Down Expand Up @@ -278,7 +293,7 @@ export const fetchRecordsByTokenIdComposable = (
});
},
(cause: any) => {
const errorData = formatForPureJsFailure(cause, tokenRecord?.token ?? '');
const errorData = formatForPureJsFailure(cause, tokenRecord?.token ?? '', false);
printLog(errorData?.error?.description ?? '', MessageType.ERROR, LogLevel.ERROR);
apiResponse?.push({
...errorData,
Expand Down
2 changes: 1 addition & 1 deletion src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const DOMAIN = 'US2';
export const CORALOGIX_DOMAIN = 'https://cdn.rum-ingress-coralogix.com/coralogix/browser/latest/coralogix-browser-sdk.js';
export const FRAME_ELEMENT = 'element';
export const COMPOSABLE_REVEAL = 'reveal-composable';

export const CUSTOM_ERROR_MESSAGES = 'CUSTOM_ERROR_MESSAGES';
export const ELEMENT_TYPES = {
COLLECT: 'COLLECT',
REVEAL: 'REVEAL',
Expand Down
11 changes: 11 additions & 0 deletions src/core/external/collect/collect-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ICollectOptions,
UploadFilesResponse,
ContainerOptions,
ErrorType,
} from '../../../utils/common';
import SKYFLOW_ERROR_CODE from '../../../utils/constants';
import logs from '../../../utils/logs';
Expand Down Expand Up @@ -88,6 +89,8 @@ class CollectContainer extends Container {

#isSkyflowFrameReady: boolean = false;

#customErrorMessages: Partial<Record<ErrorType, string>> = {};

constructor(
metaData: Metadata,
skyflowElements: Array<SkyflowElementProps>,
Expand Down Expand Up @@ -153,6 +156,10 @@ class CollectContainer extends Container {
return this.#createMultipleElement(elementGroup, true);
};

setError(errors: Partial<Record<ErrorType, string>>) {
this.#customErrorMessages = errors;
}

#createMultipleElement = (
multipleElements: ElementGroup,
isSingleElementAPI: boolean = false,
Expand Down Expand Up @@ -314,6 +321,7 @@ class CollectContainer extends Container {
tokens: options?.tokens !== undefined ? options.tokens : true,
elementIds,
containerId: this.#containerId,
errorMessages: this.#customErrorMessages,
},
(data: any) => {
if (!data || data?.error) {
Expand Down Expand Up @@ -374,6 +382,7 @@ class CollectContainer extends Container {
tokens: options?.tokens !== undefined ? options.tokens : true,
elementIds,
containerId: this.#containerId,
errorMessages: this.#customErrorMessages,
},
(data: any) => {
if (!data || data?.error) {
Expand Down Expand Up @@ -423,6 +432,7 @@ class CollectContainer extends Container {
...options,
elementIds,
containerId: this.#containerId,
errorMessages: this.#customErrorMessages,
},
(data: any) => {
if (!data || data?.error) {
Expand Down Expand Up @@ -473,6 +483,7 @@ class CollectContainer extends Container {
...options,
elementIds,
containerId: this.#containerId,
errorMessages: this.#customErrorMessages,
},
(data: any) => {
if (!data || data?.error) {
Expand Down
14 changes: 12 additions & 2 deletions src/core/external/collect/compose-collect-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
ErrorTextStyles,
ContainerOptions,
UploadFilesResponse,
ErrorMessages,
ErrorType,
} from '../../../utils/common';
import SKYFLOW_ERROR_CODE from '../../../utils/constants';
import logs from '../../../utils/logs';
Expand All @@ -36,7 +38,7 @@ import {
import {
COLLECT_FRAME_CONTROLLER,
CONTROLLER_STYLES, ELEMENT_EVENTS_TO_IFRAME,
ELEMENTS, FRAME_ELEMENT, ELEMENT_EVENTS_TO_CLIENT, ELEMENT_EVENTS_TO_CONTAINER,
ELEMENTS, FRAME_ELEMENT, ELEMENT_EVENTS_TO_CLIENT,
COLLECT_TYPES,
} from '../../constants';
import Container from '../common/container';
Expand All @@ -45,7 +47,6 @@ import ComposableElement from './compose-collect-element';
import { ElementGroup, ElementGroupItem } from './collect-container';
import { Metadata, SkyflowElementProps } from '../../internal/internal-types';
import Client from '../../../client';
import { getAccessToken } from '../../../utils/bus-events';

export interface ComposableElementGroup extends ElementGroup {
styles: InputStyles;
Expand Down Expand Up @@ -92,6 +93,8 @@ class ComposableContainer extends Container {

#getSkyflowBearerToken: () => Promise<string> | undefined;

#customErrorMessages: Partial<Record<ErrorType, string>> = {};

constructor(
metaData: Metadata,
skyflowElements: Array<SkyflowElementProps>,
Expand Down Expand Up @@ -177,6 +180,10 @@ class ComposableContainer extends Container {
);
};

setError(errors: Partial<Record<ErrorType, string>>) {
this.#customErrorMessages = errors;
}

#createMultipleElement = (
multipleElements: ComposableElementGroup,
isSingleElementAPI: boolean = false,
Expand Down Expand Up @@ -362,6 +369,7 @@ class ComposableContainer extends Container {
options: {
...data?.options,
},
errorMessages: this.#customErrorMessages,
},
);
}).catch((err:any) => {
Expand Down Expand Up @@ -446,6 +454,7 @@ class ComposableContainer extends Container {
vaultID: this.#metaData.clientJSON.config.vaultID,
authToken,
},
errorMessages: this.#customErrorMessages,
});
}).catch((err:any) => {
printLog(`${err.message}`, MessageType.ERROR, this.#context.logLevel);
Expand Down Expand Up @@ -534,6 +543,7 @@ class ComposableContainer extends Container {
vaultID: this.#metaData.clientJSON.config.vaultID,
authToken,
},
errorMessages: this.#customErrorMessages,
});
window.addEventListener('message', (event) => {
if (event.data?.type
Expand Down
Loading