Skip to content

Commit

Permalink
Merge pull request #467 from skyflowapi/SK-1658-throwing-error-messages
Browse files Browse the repository at this point in the history
SK-1658: Fixed linting while throwing error codes
  • Loading branch information
saileshwar-skyflow authored Sep 11, 2024
2 parents c94318b + 4761523 commit 30bff46
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 54 deletions.
3 changes: 1 addition & 2 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ class Client {
};

httpRequest.onerror = () => {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.NETWORK_ERROR,
[], true));
reject(new SkyflowError(SKYFLOW_ERROR_CODE.NETWORK_ERROR, [], true));
};
});
}
Expand Down
9 changes: 3 additions & 6 deletions src/core/external/collect/collect-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,12 @@ class CollectContainer extends Container {
const collectElements = Object.values(this.#elements);
collectElements.forEach((element) => {
if (!element.isMounted()) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.ELEMENTS_NOT_MOUNTED,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.ELEMENTS_NOT_MOUNTED, [], true);
}
element.isValidElement();
});
if (Object.prototype.hasOwnProperty.call(options, 'tokens') && !validateBooleanOptions(options.tokens)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKENS_IN_COLLECT,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKENS_IN_COLLECT, [], true);
}
if (options?.additionalFields) {
validateAdditionalFieldsInCollect(options.additionalFields);
Expand Down Expand Up @@ -337,8 +335,7 @@ class CollectContainer extends Container {
const fileElements = Object.values(this.#elements);
fileElements.forEach((element) => {
if (!element.isMounted()) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.ELEMENTS_NOT_MOUNTED,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.ELEMENTS_NOT_MOUNTED, [], true);
}
element.isValidElement();
});
Expand Down
12 changes: 4 additions & 8 deletions src/core/external/collect/collect-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,13 @@ class CollectElement extends SkyflowElement {
// todo: off methods
on(eventName: string, handler) {
if (!Object.values(ELEMENT_EVENTS_TO_CLIENT).includes(eventName)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_EVENT_LISTENER,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_EVENT_LISTENER, [], true);
}
if (!handler) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_HANDLER_IN_EVENT_LISTENER,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_HANDLER_IN_EVENT_LISTENER, [], true);
}
if (typeof handler !== 'function') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_HANDLER_IN_EVENT_LISTENER,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_HANDLER_IN_EVENT_LISTENER, [], true);
}
this.#eventEmitter.on(eventName, (data) => {
if (data.value === undefined) {
Expand Down Expand Up @@ -516,8 +513,7 @@ class CollectElement extends SkyflowElement {
// break;

default:
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_EVENT_TYPE,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_EVENT_TYPE, [], true);
}
this.#states[index].isEmpty = data.value.isEmpty;
this.#states[index].isValid = data.value.isValid;
Expand Down
12 changes: 4 additions & 8 deletions src/core/external/collect/compose-collect-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ class ComposableContainer extends Container {

const { layout } = this.#options;
if (sum(layout) !== this.#elementsList.length) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISMATCH_ELEMENT_COUNT_LAYOUT_SUM,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISMATCH_ELEMENT_COUNT_LAYOUT_SUM, [], true);
}
let count = 0;
layout.forEach((rowCount, index) => {
Expand Down Expand Up @@ -359,15 +358,13 @@ class ComposableContainer extends Container {
try {
validateInitConfig(this.#metaData.clientJSON.config);
if (!this.#isMounted) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.COMPOSABLE_CONTAINER_NOT_MOUNTED,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.COMPOSABLE_CONTAINER_NOT_MOUNTED, [], true);
}

const containerElements = getElements(this.#tempElements);
containerElements.forEach((element:any) => {
if (!element?.isMounted) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.ELEMENTS_NOT_MOUNTED,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.ELEMENTS_NOT_MOUNTED, [], true);
}
});

Expand All @@ -377,8 +374,7 @@ class ComposableContainer extends Container {
});

if (options && options.tokens && typeof options.tokens !== 'boolean') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKENS_IN_COLLECT,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKENS_IN_COLLECT, [], true);
}
if (options?.additionalFields) {
validateAdditionalFieldsInCollect(options.additionalFields);
Expand Down
6 changes: 2 additions & 4 deletions src/core/external/common/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export default class IFrame {
if (typeof domElement === 'string') {
this.container = document.querySelector(domElement) || undefined;
if (!this.container) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ELEMENT_SELECTOR,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ELEMENT_SELECTOR, [], true);
}
} else if (domElement instanceof HTMLElement) {
this.container = domElement;
} else {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ELEMENT_SELECTOR,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ELEMENT_SELECTOR, [], true);
}
} catch (e: any) {
// eslint-disable-next-line no-console
Expand Down
5 changes: 1 addition & 4 deletions src/core/internal/iframe-form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,7 @@ export class IFrameForm {
};

tokenize = (options) => {
if (!this.client) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.CLIENT_CONNECTION,
[], true);
}
if (!this.client) throw new SkyflowError(SKYFLOW_ERROR_CODE.CLIENT_CONNECTION, [], true);
const insertResponseObject: any = {};
const updateResponseObject: any = {};
const formElements = Object.keys(this.iFrameFormElements);
Expand Down
18 changes: 6 additions & 12 deletions src/libs/element-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export function validateElementOptions(
newOptions: any = {},
) {
if (elementType !== 'group' && !Object.prototype.hasOwnProperty.call(ELEMENTS, elementType)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ELEMENT_TYPE,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ELEMENT_TYPE, [], true);
}

if (Object.prototype.hasOwnProperty.call(oldOptions, 'validations')) {
if (!Array.isArray(oldOptions.validations)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_VALIDATIONS_TYPE,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_VALIDATIONS_TYPE, [], true);
} else {
oldOptions.validations.forEach((validationRule: IValidationRule, index) => {
if (!Object.prototype.hasOwnProperty.call(validationRule, 'type')) {
Expand Down Expand Up @@ -309,15 +307,13 @@ export const formatOptions = (elementType, options, logLevel) => {
if (!(typeof formattedOptions.cardMetadata === 'object')
|| (Object.prototype.toString.call(formattedOptions.cardMetadata) !== '[object Object]')
) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_OPTION_CARD_METADATA,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_OPTION_CARD_METADATA, [], true);
}

if (Object.prototype.hasOwnProperty.call(formattedOptions.cardMetadata, 'scheme')) {
if (!(typeof formattedOptions.cardMetadata.scheme === 'object')
|| (Object.prototype.toString.call(formattedOptions.cardMetadata.scheme) !== '[object Array]')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_OPTION_CARD_SCHEME,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_OPTION_CARD_SCHEME, [], true);
}
}
}
Expand Down Expand Up @@ -388,12 +384,10 @@ export const formatOptions = (elementType, options, logLevel) => {
[], true);
}
if (options.allowedFileType.length <= 0) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_ALLOWED_OPTIONS_ARRAY,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_ALLOWED_OPTIONS_ARRAY, [], true);
}
if (!options.allowedFileType.every((item) => typeof item === 'string')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ALLOWED_FILETYPE_ARRAY,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ALLOWED_FILETYPE_ARRAY, [], true);
}
}
if (Object.prototype.hasOwnProperty.call(options, 'allowedFileType')) {
Expand Down
6 changes: 2 additions & 4 deletions src/skyflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,9 @@ class Skyflow {

default:
if (!type) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_CONTAINER_TYPE,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_CONTAINER_TYPE, [], true);
}
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTAINER_TYPE,
[type], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTAINER_TYPE, [type], true);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/utils/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,11 @@ const DANGEROUS_FILE_TYPE = ['application/zip', 'application/vnd.debian.binary-p
// Check file type and file size in KB
export const fileValidation = (value, required: Boolean = false, fileElement) => {
if (required && (value === undefined || value === '')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.NO_FILE_SELECTED,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.NO_FILE_SELECTED, [], true);
}

if (DANGEROUS_FILE_TYPE.includes(value.type)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_TYPE,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_TYPE, [], true);
}

if (Object.prototype.hasOwnProperty.call(fileElement, 'allowedFileType') && (value !== undefined && value !== '')) {
Expand All @@ -200,8 +198,7 @@ export const fileValidation = (value, required: Boolean = false, fileElement) =>
}

if (value.size > 32000000) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_SIZE,
[], true);
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_SIZE, [], true);
}

return true;
Expand Down

0 comments on commit 30bff46

Please sign in to comment.