Skip to content

fix: return error response for webhook non-timeout error#1167

Merged
VisargD merged 3 commits into
Portkey-AI:mainfrom
b4s36t4:fix/webhook-plugin-errors
Jun 25, 2025
Merged

fix: return error response for webhook non-timeout error#1167
VisargD merged 3 commits into
Portkey-AI:mainfrom
b4s36t4:fix/webhook-plugin-errors

Conversation

@b4s36t4

@b4s36t4 b4s36t4 commented Jun 20, 2025

Copy link
Copy Markdown
Contributor

Description

Motivation

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

@matterai-app

matterai-app Bot commented Jun 20, 2025

Copy link
Copy Markdown
Contributor

Code Quality bug fix new feature

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

  • Webhook Error Handling: The plugins/default/webhook.ts file has been updated to provide more comprehensive error responses for webhook failures. It now distinguishes between TimeoutError and other HTTP errors. For non-timeout errors, if the response body is available and its Content-Type is application/json, the parsed response body is included in the error data. The webhook request timeout is now configurable via parameters.timeout.
  • Error Utility Enhancements: In plugins/utils.ts, the TimeoutError class is now exported, making it accessible for type checking. The ErrorResponse interface has been extended to include an optional headers property, and the post utility now passes the response headers when throwing an HttpError.
  • Guardrail Check Verdict Logic: A new fail_on_error boolean parameter has been introduced for guardrail checks in src/middlewares/hooks/index.ts and src/middlewares/hooks/types.ts. This parameter allows for more granular control over the overall hook verdict: if a check encounters an error but fail_on_error is set to false, that specific error will not cause the entire hook's verdict to be false.

🔍 Impact of the Change

  • Improved Debuggability: Developers will now receive more detailed error information for webhook failures, including the actual response body from the external service, which significantly aids in debugging non-timeout issues.
  • Enhanced Configurability: The ability to configure webhook timeouts and control how guardrail check errors affect the final verdict provides greater flexibility and allows for more resilient system behavior, enabling "soft failures" where appropriate.
  • Increased Robustness: Explicitly handling TimeoutError separately from other HTTP errors allows for more precise error management and better user feedback.

📁 Total Files Changed

4 files changed.

🧪 Test Added

No explicit tests are mentioned in the PR description. Manual testing or existing integration tests would be required to verify the new error handling and fail_on_error logic.

🔒Security Vulnerabilities

No new security vulnerabilities were detected in the provided code changes.

Motivation

This PR aims to improve the error handling for webhook calls by returning a more informative error response for non-timeout errors, including the response body when available. Additionally, it introduces a mechanism to control how errors in individual guardrail checks affect the overall hook verdict.

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)

N/A

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

N/A

Tip

Quality Recommendations

  1. Add a try-catch block around JSON.parse(responseData) in plugins/default/webhook.ts to robustly handle cases where the Content-Type is application/json but the responseData is malformed or not valid JSON, preventing potential crashes.

  2. Consider adding more detailed logging of the original error object (e) in the catch block of plugins/default/webhook.ts before delete error.stack. This would provide more context for debugging non-timeout errors, especially when the response body is crucial.

  3. The verdict logic in src/middlewares/hooks/index.ts (result.verdict || (result.error && !result.fail_on_error)) is functionally correct but could benefit from a clarifying comment explaining the intended behavior of 'soft failure' or 'ignoring error' when fail_on_error is false.

Sequence Diagram

sequenceDiagram
    participant HM as HooksManager
    participant WH as WebhookHandler (PluginHandler)
    participant UT as Utils (post function)
    participant EXT as External Webhook Service

    HM->>WH: executeCheck(check.parameters)
    activate WH
    WH->>UT: post(webhookURL, headers, timeout)
    activate UT
    UT->>EXT: HTTP POST Request
    activate EXT
    alt Successful Response
        EXT-->>UT: HTTP 2xx Response
        UT-->>WH: Response Data
    else HTTP Error (Non-Timeout)
        EXT-->>UT: HTTP 4xx/5xx Response (with body)
        UT->>UT: Create ErrorResponse (status, body, headers)
        UT--xWH: Throw HttpError(ErrorResponse)
    else Timeout Error
        EXT--xUT: No Response (Timeout)
        UT--xWH: Throw TimeoutError(url, timeout, method)
    end
    deactivate EXT

    alt Error Caught in WebhookHandler
        WH->>WH: Check if error is TimeoutError
        alt If TimeoutError
            WH->>WH: Prepare data with explanation, webhookUrl, requestContext
            WH->>HM: Return result with error data (no responseData)
        else If HttpError (Non-Timeout)
            WH->>WH: Extract responseData, responseDataContentType from error.response
            WH->>WH: Prepare data with explanation, webhookUrl, requestContext
            WH->>WH: Conditionally add parsed responseData if JSON
            WH->>HM: Return result with error data (including responseData)
        end
    end
    deactivate WH

    HM->>HM: Process checkResults
    HM->>HM: Retrieve fail_on_error from check.parameters
    HM->>HM: Determine final verdict based on result.verdict, result.error, and fail_on_error
    HM-->>Client: Return HookResult
Loading

@matterai-app matterai-app Bot left a comment

Copy link
Copy Markdown
Contributor

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 for webhook responses by distinguishing between timeout errors and other HTTP errors, and returning response data when available. I've identified a few minor improvements that could enhance the implementation.

Comment thread plugins/default/webhook.ts
Comment thread plugins/default/webhook.ts
@b4s36t4 b4s36t4 force-pushed the fix/webhook-plugin-errors branch from 25a441f to b5d2963 Compare June 20, 2025 15:22
@matterai-app

matterai-app Bot commented Jun 20, 2025

Copy link
Copy Markdown
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

@matterai-app

matterai-app Bot commented Jun 25, 2025

Copy link
Copy Markdown
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

@VisargD VisargD merged commit 29c13cc into Portkey-AI:main Jun 25, 2025
2 checks passed
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