Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 24 additions & 42 deletions src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,53 +795,35 @@ export async function tryTargetsRecursively(
// tryPost always returns a Response.
// TypeError will check for all unhandled exceptions.
// GatewayError will check for all handled exceptions which cannot allow the request to proceed.
if (
error instanceof TypeError ||
error instanceof GatewayError ||
!error.response ||
(error.response && !(error.response instanceof Response))
) {
console.error(
'tryTargetsRecursively error: ',
error.message,
error.cause,
error.stack
);
const errorMessage =
error instanceof GatewayError
? error.message
: 'Something went wrong';
response = new Response(
JSON.stringify({
status: 'failure',
message: errorMessage,
}),
{
status: 500,
headers: {
'content-type': 'application/json',
// Add this header so that the fallback loop can be interrupted if its an exception.
'x-portkey-gateway-exception': 'true',
},
}
);
} else {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR removes the throw new Error() logic in tryPost for LLM failures, we no longer need to set it here. If the any error is caught, they will always be an Gateway exception.

response = error.response;
if (isHandlingCircuitBreaker) {
await c.get('recordCircuitBreakerFailure')?.(
env(c),
currentInheritedConfig.id,
currentTarget.cbConfig,
currentJsonPath,
response.status
);
console.error(
'tryTargetsRecursively error: ',
error.message,
error.cause,
error.stack
);
const errorMessage =
error instanceof GatewayError
? error.message
: 'Something went wrong';
response = new Response(
JSON.stringify({
status: 'failure',
message: errorMessage,
}),
{
status: 500,
headers: {
'content-type': 'application/json',
// Add this header so that the fallback loop can be interrupted if its an exception.
'x-portkey-gateway-exception': 'true',
},
}
}
);
}
break;
}

return response;
return response!;
}

export function constructConfigFromRequestHeaders(
Expand Down
7 changes: 0 additions & 7 deletions src/handlers/services/responseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ export class ResponseService {

this.updateHeaders(finalMappedResponse, cache.cacheStatus, retryAttempt);

if (!finalMappedResponse.ok) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing error, tryPost will return it just as a simple response as throwing error is redundant.

In tryTargetsRecursively, we are already checking for response.ok

const errorObj: any = new Error(await finalMappedResponse.clone().text());
errorObj.status = finalMappedResponse.status;
errorObj.response = finalMappedResponse;
throw errorObj;
}

return {
response: finalMappedResponse,
responseJson,
Expand Down