diff --git a/src/backend/proxy/providerPreload.ts b/src/backend/proxy/providerPreload.ts index 8588c18e3..295cb83f0 100644 --- a/src/backend/proxy/providerPreload.ts +++ b/src/backend/proxy/providerPreload.ts @@ -42,8 +42,33 @@ const listenToRendererCalls = ( }) } +function extractJSONAfterError(input: string): object | null { + try { + // Match the pattern where "error=" precedes a JSON string + const errorPattern = /error=(\{[^{}]*\})/ + + const match = input.match(errorPattern) + + if (match && match[1]) { + // Parse the JSON string captured by the regex + return JSON.parse(match[1]) + } + + // Return null if no valid JSON is found + return null + } catch (error) { + console.error('Error parsing JSON:', error) + return null + } +} + const provRequest = async (args: RequestArguments) => { const x = await ipcRenderer.invoke('providerRequest', args) + const jsonErrorInString = extractJSONAfterError(`${x}`) + if (jsonErrorInString !== null) { + throw jsonErrorInString + } + if ( x !== null && x !== 'undefined' &&