diff --git a/src/handlers/chatCompletionsHandler.ts b/src/handlers/chatCompletionsHandler.ts index 59331960..b3dab64f 100644 --- a/src/handlers/chatCompletionsHandler.ts +++ b/src/handlers/chatCompletionsHandler.ts @@ -26,14 +26,6 @@ export async function chatCompletionsHandler(c: Context): Promise { "config" ); - if (!tryTargetsResponse) { - return new Response(errors[errors.length - 1].errorObj, { - status: errors[errors.length - 1].status, - headers: { - "content-type": "application/json" - } - }); - } return tryTargetsResponse; } catch (err: any) { console.log("chatCompletion error", err.message); diff --git a/src/handlers/completionsHandler.ts b/src/handlers/completionsHandler.ts index 2111bd45..11828b0f 100644 --- a/src/handlers/completionsHandler.ts +++ b/src/handlers/completionsHandler.ts @@ -27,14 +27,6 @@ export async function completionsHandler(c: Context): Promise { "config" ); - if (!tryTargetsResponse) { - return new Response(errors[errors.length - 1].errorObj, { - status: errors[errors.length - 1].status, - headers: { - "content-type": "application/json" - } - }); - } return tryTargetsResponse; } catch (err: any) { console.log("completion error", err.message); diff --git a/src/handlers/embeddingsHandler.ts b/src/handlers/embeddingsHandler.ts index ccbb37ec..46e66241 100644 --- a/src/handlers/embeddingsHandler.ts +++ b/src/handlers/embeddingsHandler.ts @@ -28,14 +28,6 @@ export async function embeddingsHandler(c: Context): Promise { "config" ); - if (!tryTargetsResponse) { - return new Response(errors[errors.length - 1].errorObj, { - status: errors[errors.length - 1].status, - headers: { - "content-type": "application/json" - } - }); - } return tryTargetsResponse; } catch (err: any) { console.log("completion error", err.message); diff --git a/src/handlers/handlerUtils.ts b/src/handlers/handlerUtils.ts index 38baa547..0939028a 100644 --- a/src/handlers/handlerUtils.ts +++ b/src/handlers/handlerUtils.ts @@ -429,8 +429,9 @@ export async function tryPost(c: Context, providerOption:Options, inputParams: P // If the response was not ok, throw an error if (!response.ok) { // Check if this request needs to be retried - const errorObj: any = new Error(await mappedResponse.text()); + const errorObj: any = new Error(await mappedResponse.clone().text()); errorObj.status = mappedResponse.status; + errorObj.response = mappedResponse throw errorObj; } @@ -502,7 +503,7 @@ export async function tryTargetsRecursively( errors: any, jsonPath: string, inheritedConfig: Record = {} -): Promise { +): Promise { let currentTarget: any = {...targetGroup}; let currentJsonPath = jsonPath; const strategyMode = currentTarget.strategy?.mode; @@ -614,6 +615,7 @@ export async function tryTargetsRecursively( currentJsonPath ); } catch (error: any) { + response = error.response; errors.push({ provider: targetGroup.provider, errorObj: error.message,