From 50f423933ab14775bc9d1abc546e9872410805d2 Mon Sep 17 00:00:00 2001 From: nikita-keyvalue Date: Fri, 7 Jul 2023 14:07:59 +0530 Subject: [PATCH] feat: Add error handling --- src/index.tsx | 8 +++++++- src/utils/checks.ts | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index c590b8f..f2fbb11 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,6 @@ import { NativeModules } from 'react-native'; import { ERROR_CODES, validateImageUrl } from './utils'; +import { validateImageExtension } from './utils/checks'; const { ImageProcessingSDK } = NativeModules; export type CreatePdfOptions = { @@ -11,7 +12,12 @@ export type CreatePdfOptions = { export function isImageBlurred(imageUrl: string): Promise { return new Promise((resolve, reject) => { if (validateImageUrl(imageUrl)) { - resolve(ImageProcessingSDK.isImageBlurred(imageUrl.slice(8))); + if(validateImageExtension(imageUrl)){ + resolve(ImageProcessingSDK.isImageBlurred(imageUrl.slice(8))); + } + else{ + reject(new Error(JSON.stringify(ERROR_CODES.ERR002))); + } } else { reject(new Error(JSON.stringify(ERROR_CODES.ERR001))); } diff --git a/src/utils/checks.ts b/src/utils/checks.ts index 5c1301d..058b257 100644 --- a/src/utils/checks.ts +++ b/src/utils/checks.ts @@ -1,4 +1,16 @@ -export const validateImageUrl = (value: string) => { - console.log('value', value); +export const validateImageUrl = (value: string): boolean => { + if (value === null || value.trim() === '') { + return false; // Null or empty string + } return true; }; + +export const validateImageExtension = (value: string): boolean => { +// Check if the file has an image extension +const imageExtensions = ['.jpg', '.jpeg', '.png']; +const fileExtension = value.substring(value.lastIndexOf('.')).toLowerCase(); +if (!imageExtensions.includes(fileExtension)) { + return false; // Invalid image file extension +} + return true; +}; \ No newline at end of file