From 789639f6e65bb2794b29067360450bbd7f66f64d Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Tue, 29 Oct 2024 21:12:26 +0530 Subject: [PATCH 1/8] fix: format function refactor for better logs readability --- src/Utilities/Utils.res | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 7cdbd5a5..d18b9657 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -78,6 +78,8 @@ let getInt = (dict, key, default: int) => { ->Float.toInt } +let getJsonFromArrayOfJson = arr => arr->Dict.fromArray->JSON.Encode.object + let getFloatFromString = (str, default) => str->Float.fromString->Option.getOr(default) let getFloatFromJson = (json, default) => { @@ -864,9 +866,11 @@ let fetchApi = (uri, ~bodyStr: string="", ~headers=Dict.make(), ~method: Fetch.m }, ) ->catch(err => { + Js.log2("errerrerrerrerrerrerr", err) reject(err) }) ->then(resp => { + Js.log2("resprespresprespresp", resp) resolve(resp) }) }) @@ -877,9 +881,29 @@ let arrayJsonToCamelCase = arr => { item->transformKeys(CamelCase) }) } -let formatException = exc => { - exc->Identity.anyTypeToJson -} + +let formatException = exc => + switch exc { + | Exn.Error(obj) => + let (message, name, stack, fileName) = ( + Exn.message(obj), + Exn.name(obj), + Exn.stack(obj), + Exn.fileName(obj), + ) + [ + ("message", message->Identity.anyTypeToJson), + ("type", name->Identity.anyTypeToJson), + ("stack", stack->Identity.anyTypeToJson), + ("fileName", fileName->Identity.anyTypeToJson), + ] + | _ => [ + ("message", "Unknown error"->Identity.anyTypeToJson), + ("type", "Unknown"->Identity.anyTypeToJson), + ("stack", "Unknown"->Identity.anyTypeToJson), + ("fileName", "Unknown"->Identity.anyTypeToJson), + ] + }->getJsonFromArrayOfJson let getArrayValFromJsonDict = (dict, key, arrayKey) => { dict @@ -1300,8 +1324,6 @@ let getIsWalletElementPaymentType = (paymentType: CardThemeType.mode) => { let getUniqueArray = arr => arr->Array.map(item => (item, ""))->Dict.fromArray->Dict.keysToArray -let getJsonFromArrayOfJson = arr => arr->Dict.fromArray->JSON.Encode.object - let getStateNameFromStateCodeAndCountry = (list: JSON.t, stateCode: string, country: string) => { let options = list From 24ca6ff5a37e40e8b033c5a58cd0c16d689ac4e1 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Tue, 29 Oct 2024 21:16:16 +0530 Subject: [PATCH 2/8] fix: getJsonFromArrayOfJson added in necessary places --- src/Utilities/Utils.res | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index d18b9657..7452465e 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -20,8 +20,10 @@ type dateTimeFormat = {resolvedOptions: unit => options} open ErrorUtils +let getJsonFromArrayOfJson = arr => arr->Dict.fromArray->JSON.Encode.object + let messageWindow = (window, ~targetOrigin="*", messageArr) => { - window->postMessage(messageArr->Dict.fromArray->JSON.Encode.object, targetOrigin) + window->postMessage(messageArr->getJsonFromArrayOfJson, targetOrigin) } let messageTopWindow = (~targetOrigin="*", messageArr) => { @@ -78,8 +80,6 @@ let getInt = (dict, key, default: int) => { ->Float.toInt } -let getJsonFromArrayOfJson = arr => arr->Dict.fromArray->JSON.Encode.object - let getFloatFromString = (str, default) => str->Float.fromString->Option.getOr(default) let getFloatFromJson = (json, default) => { @@ -319,13 +319,12 @@ let getFailedSubmitResponse = (~errorType, ~message) => { [ ( "error", - [("type", errorType->JSON.Encode.string), ("message", message->JSON.Encode.string)] - ->Dict.fromArray - ->JSON.Encode.object, + [ + ("type", errorType->JSON.Encode.string), + ("message", message->JSON.Encode.string), + ]->getJsonFromArrayOfJson, ), - ] - ->Dict.fromArray - ->JSON.Encode.object + ]->getJsonFromArrayOfJson } let toCamelCase = str => { @@ -387,8 +386,7 @@ let rec transformKeys = (json: JSON.t, to: case) => { } x }) - ->Dict.fromArray - ->JSON.Encode.object + ->getJsonFromArrayOfJson } let getClientCountry = clientTimeZone => { @@ -703,7 +701,7 @@ let handlePostMessageEvents = ( ("elementType", "payment"->JSON.Encode.string), ("complete", complete->JSON.Encode.bool), ("empty", empty->JSON.Encode.bool), - ("value", [("type", paymentType->JSON.Encode.string)]->Dict.fromArray->JSON.Encode.object), + ("value", [("type", paymentType->JSON.Encode.string)]->getJsonFromArrayOfJson), ]) } @@ -1023,8 +1021,7 @@ let mergeTwoFlattenedJsonDicts = (dict1, dict2) => { dict1 ->Dict.toArray ->Array.concat(dict2->Dict.toArray) - ->Dict.fromArray - ->JSON.Encode.object + ->getJsonFromArrayOfJson ->unflattenObject } From db7d5518ebe714e1e9978e3f469e11500de7a908 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Tue, 29 Oct 2024 21:20:03 +0530 Subject: [PATCH 3/8] fix: console removed --- src/Utilities/Utils.res | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 7452465e..5f304b3b 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -864,11 +864,9 @@ let fetchApi = (uri, ~bodyStr: string="", ~headers=Dict.make(), ~method: Fetch.m }, ) ->catch(err => { - Js.log2("errerrerrerrerrerrerr", err) reject(err) }) ->then(resp => { - Js.log2("resprespresprespresp", resp) resolve(resp) }) }) From 4fd172d0dc4add99bb9752e6dfef1c913e71f25d Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Wed, 30 Oct 2024 11:02:00 +0530 Subject: [PATCH 4/8] fix: identity removed --- src/Utilities/Utils.res | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 5f304b3b..20267d55 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -888,16 +888,16 @@ let formatException = exc => Exn.fileName(obj), ) [ - ("message", message->Identity.anyTypeToJson), - ("type", name->Identity.anyTypeToJson), - ("stack", stack->Identity.anyTypeToJson), - ("fileName", fileName->Identity.anyTypeToJson), + ("message", message->Option.getOr("Unknown Error")->JSON.Encode.string), + ("type", name->Option.getOr("Unknown")->JSON.Encode.string), + ("stack", stack->Option.getOr("Unknown")->JSON.Encode.string), + ("fileName", fileName->Option.getOr("Unknown")->JSON.Encode.string), ] | _ => [ - ("message", "Unknown error"->Identity.anyTypeToJson), - ("type", "Unknown"->Identity.anyTypeToJson), - ("stack", "Unknown"->Identity.anyTypeToJson), - ("fileName", "Unknown"->Identity.anyTypeToJson), + ("message", "Unknown error"->JSON.Encode.string), + ("type", "Unknown"->JSON.Encode.string), + ("stack", "Unknown"->JSON.Encode.string), + ("fileName", "Unknown"->JSON.Encode.string), ] }->getJsonFromArrayOfJson From 635cec3f8f0ef3623095f2c448c276f585fc272f Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Wed, 30 Oct 2024 22:51:36 +0530 Subject: [PATCH 5/8] refactor: fetch api functionality --- src/Utilities/Utils.res | 69 +++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 20267d55..72e9e1cb 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -826,6 +826,7 @@ let delay = timeOut => { }, timeOut)->ignore }) } + let getHeaders = (~uri=?, ~token=?, ~headers=Dict.make()) => { let headerObj = [ @@ -848,30 +849,6 @@ let getHeaders = (~uri=?, ~token=?, ~headers=Dict.make()) => { }) Fetch.Headers.fromObject(headerObj->dictToObj) } -let fetchApi = (uri, ~bodyStr: string="", ~headers=Dict.make(), ~method: Fetch.method) => { - open Promise - let body = switch method { - | #GET => resolve(None) - | _ => resolve(Some(Fetch.Body.string(bodyStr))) - } - body->then(body => { - Fetch.fetch( - uri, - { - method, - ?body, - headers: getHeaders(~headers, ~uri), - }, - ) - ->catch(err => { - reject(err) - }) - ->then(resp => { - resolve(resp) - }) - }) -} - let arrayJsonToCamelCase = arr => { arr->Array.map(item => { item->transformKeys(CamelCase) @@ -901,6 +878,50 @@ let formatException = exc => ] }->getJsonFromArrayOfJson +let fetchApi = async (uri, ~bodyStr: string="", ~headers=Dict.make(), ~method: Fetch.method) => { + try { + let body = switch method { + | #GET => None + | _ => Some(Fetch.Body.string(bodyStr)) + } + + let response = await Fetch.fetch( + uri, + { + method, + ?body, + headers: getHeaders(~headers, ~uri), + }, + ) + + if !(response->Fetch.Response.ok) { + let errorText = await response->Fetch.Response.text + let status = response->Fetch.Response.status + let errorMessage = `FetchError: Failed to fetch (${status->Int.toString}): ${errorText}` + Error.raise(Error.make(errorMessage)) + } + + response + } catch { + | Exn.Error(error) => { + let errorMessage = switch Exn.message(error) { + | Some(msg) => msg + | None => "Unknown error occurred" + } + + let enhancedError = Error.make(`APIError: ${errorMessage} (URL: ${uri})`) + Error.raise(enhancedError) + } + | err => { + let exceptionVal = err->formatException->getDictFromJson + let errorMessage = exceptionVal->getString("message", "Unknown error") + let errorType = exceptionVal->getString("type", "Unknown") + let networkError = Error.make(`${errorType}: ${errorMessage} (URL: ${uri})`) + Error.raise(networkError) + } + } +} + let getArrayValFromJsonDict = (dict, key, arrayKey) => { dict ->Dict.get(key) From 19593f35eea993ff071ee0ad8cca4be74581a977 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Mon, 4 Nov 2024 14:56:14 +0530 Subject: [PATCH 6/8] fix: some changes --- src/Utilities/Utils.res | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 72e9e1cb..dd47b143 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -864,19 +864,23 @@ let formatException = exc => Exn.stack(obj), Exn.fileName(obj), ) - [ - ("message", message->Option.getOr("Unknown Error")->JSON.Encode.string), - ("type", name->Option.getOr("Unknown")->JSON.Encode.string), - ("stack", stack->Option.getOr("Unknown")->JSON.Encode.string), - ("fileName", fileName->Option.getOr("Unknown")->JSON.Encode.string), - ] - | _ => [ - ("message", "Unknown error"->JSON.Encode.string), - ("type", "Unknown"->JSON.Encode.string), - ("stack", "Unknown"->JSON.Encode.string), - ("fileName", "Unknown"->JSON.Encode.string), - ] - }->getJsonFromArrayOfJson + if ( + message->Option.isSome || + name->Option.isSome || + stack->Option.isSome || + fileName->Option.isSome + ) { + [ + ("message", message->Option.getOr("Unknown Error")->JSON.Encode.string), + ("type", name->Option.getOr("Unknown")->JSON.Encode.string), + ("stack", stack->Option.getOr("Unknown")->JSON.Encode.string), + ("fileName", fileName->Option.getOr("Unknown")->JSON.Encode.string), + ]->getJsonFromArrayOfJson + } else { + exc->Identity.anyTypeToJson + } + | _ => exc->Identity.anyTypeToJson + } let fetchApi = async (uri, ~bodyStr: string="", ~headers=Dict.make(), ~method: Fetch.method) => { try { From 5584032e0e03615e80c90d8b78d2799ed87546c6 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Mon, 4 Nov 2024 16:00:07 +0530 Subject: [PATCH 7/8] fix: comments addressed --- src/Utilities/Utils.res | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index dd47b143..6aeb04b3 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -858,12 +858,11 @@ let arrayJsonToCamelCase = arr => { let formatException = exc => switch exc { | Exn.Error(obj) => - let (message, name, stack, fileName) = ( - Exn.message(obj), - Exn.name(obj), - Exn.stack(obj), - Exn.fileName(obj), - ) + let message = Exn.message(obj) + let name = Exn.name(obj) + let stack = Exn.stack(obj) + let fileName = Exn.fileName(obj) + if ( message->Option.isSome || name->Option.isSome || From 3cbcc2d4b0e4f37c271fcfd3e4acdc91d51a53a3 Mon Sep 17 00:00:00 2001 From: Pritish Budhiraja Date: Mon, 4 Nov 2024 16:09:55 +0530 Subject: [PATCH 8/8] fix: comment added --- src/Utilities/Utils.res | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Utilities/Utils.res b/src/Utilities/Utils.res index 24062933..ebc79bcc 100644 --- a/src/Utilities/Utils.res +++ b/src/Utilities/Utils.res @@ -892,6 +892,7 @@ let fetchApi = async (uri, ~bodyStr: string="", ~headers=Dict.make(), ~method: F }, ) + //* Fetch.Response.ok - Response status code - 200-299 if !(response->Fetch.Response.ok) { let errorText = await response->Fetch.Response.text let status = response->Fetch.Response.status