Skip to content

Conversation

VisargD
Copy link
Collaborator

@VisargD VisargD commented Jul 24, 2025

Description

logObject.log() method is not getting called for error responses because responseService.create() throws an error with response details for all non-2xx LLM error responses. This causes issues with keeping track of logObjects of failed requests.

It is redundant to throw the error from responseService, because it is simply getting caught and then the same value is used in the final response object of tryTargetsRecursively function.

With this change, tryTargetsRecursively catch block should only be used when there is an actual gateway exception. For all provider non-2xx responses, they will be treated as a valid response.

Motivation

Fix on top of #1121

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

@matter-code-review
Copy link
Contributor

Code Quality bug fix

Summary By MatterAI MatterAI logo

🔄 What Changed

This PR improves error handling in the gateway by enhancing error logging and response formatting when provider errors occur. The changes focus on better error reporting and standardizing error responses.

🔍 Impact of the Change

The changes improve error visibility and debugging by adding detailed error logging (message, cause, stack) when errors occur in the tryTargetsRecursively function. The PR also standardizes error responses by returning a consistent JSON structure with appropriate status codes and headers.

📁 Total Files Changed

  1. src/handlers/handlerUtils.ts - Enhanced error logging and standardized error response formatting
  2. src/handlers/services/responseService.ts - Removed unnecessary code

🧪 Test Added

N/A - No new tests were added in this PR.

🔒 Security Vulnerabilities

N/A - No security vulnerabilities were identified in this PR.

Tip

Quality Recommendations

  1. Add unit tests to verify the error handling behavior

  2. Consider adding more specific error types beyond just GatewayError for better error classification

  3. Add internationalization support for error messages

  4. Consider adding structured logging instead of console.error for better log analysis

Sequence Diagram

sequenceDiagram
    participant Client
    participant Gateway
    participant tryTargetsRecursively
    participant Provider
    
    Client->>Gateway: Request
    Gateway->>tryTargetsRecursively: Process request
    tryTargetsRecursively->>Provider: Forward request
    
    alt Provider Success
        Provider-->>tryTargetsRecursively: Success Response
        tryTargetsRecursively-->>Gateway: Return provider response
    else Provider Error
        Provider-->>tryTargetsRecursively: Error
        Note over tryTargetsRecursively: Enhanced Error Logging:
        Note over tryTargetsRecursively: console.error(error.message, error.cause, error.stack)
        
        alt GatewayError
            tryTargetsRecursively-->>Gateway: Response with error.message
        else Other Error
            tryTargetsRecursively-->>Gateway: Response with "Something went wrong"
        end
        
        Note over tryTargetsRecursively: Set status: 500
        Note over tryTargetsRecursively: Set header: x-portkey-gateway-exception: true
    end
    
    Gateway-->>Client: Final Response
Loading

Copy link
Contributor

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

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

This PR improves error handling by adding detailed logging and standardizing error responses. I have one suggestion to enhance the error logging format for better readability and analysis.


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

},
}
);
} 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.

@VisargD VisargD requested a review from b4s36t4 July 24, 2025 14:52
@VisargD VisargD assigned narengogi and unassigned narengogi Jul 24, 2025
@VisargD VisargD requested a review from narengogi July 24, 2025 14:52
@VisargD VisargD marked this pull request as ready for review July 25, 2025 10:45
@VisargD VisargD merged commit 654600c into main Jul 25, 2025
1 check passed
@VisargD VisargD deleted the fix/llm-error-response-log-addition branch July 25, 2025 11:18
@matter-code-review
Copy link
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants