Skip to content

[TT-12193] Fix poor error handling in webhook event templates#6303

Merged
titpetric merged 1 commit into
masterfrom
fix/event-handler-webhooks-error-handling
May 23, 2024
Merged

[TT-12193] Fix poor error handling in webhook event templates#6303
titpetric merged 1 commit into
masterfrom
fix/event-handler-webhooks-error-handling

Conversation

@titpetric

@titpetric titpetric commented May 23, 2024

Copy link
Copy Markdown
Contributor

User description

Fixes error handling in webhook handler templates

https://tyktech.atlassian.net/browse/TT-12193


PR Type

Bug fix, Tests


Description

  • Added error handling for template execution in the CreateBody method of WebHookHandler.
  • Refactored test helper function to include error handling.
  • Replaced panic and error checks with assert.NoError in tests.
  • Improved error messages in tests for better clarity.

Changes walkthrough 📝

Relevant files
Bug fix
event_handler_webhooks.go
Add error handling in `CreateBody` method of WebHookHandler

gateway/event_handler_webhooks.go

  • Added error handling for template execution in CreateBody method.
  • +4/-1     
    Tests
    event_handler_webhooks_test.go
    Improve error handling and assertions in webhook handler tests

    gateway/event_handler_webhooks_test.go

  • Refactored test helper function to include error handling.
  • Replaced panic and error checks with assert.NoError.
  • Improved error messages in tests.
  • +22/-24 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    PR Description updated to latest commit (fe24bf0)

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    API Changes

    no api changes detected

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are straightforward and localized to specific methods, involving basic error handling and test refactoring.

    🧪 Relevant tests

    Yes

    ⚡ Possible issues

    Possible Bug: The error handling in CreateBody might not log the error, which could make debugging more difficult if the template execution fails.

    🔒 Security concerns

    No

    Code feedback:
    relevant filegateway/event_handler_webhooks.go
    suggestion      

    Consider logging the error before returning it in the CreateBody method. This can help in debugging issues related to template execution failures. [important]

    relevant linereturn "", err

    relevant filegateway/event_handler_webhooks_test.go
    suggestion      

    Use more descriptive error messages in your tests to help identify issues quickly during failures. For example, in TestCreateBody, specify what was expected and what was actually received. [medium]

    relevant linet.Errorf("Body incorrect, is: %q", body)

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add a detailed error message for template execution failure

    Consider adding a detailed error message when the template execution fails. This can help
    with debugging and understanding the context of the error.

    gateway/event_handler_webhooks.go [204-205]

     if err := w.template.Execute(&reqBody, em); err != nil {
    -    return "", err
    +    return "", fmt.Errorf("template execution failed: %w", err)
     }
     
    Suggestion importance[1-10]: 9

    Why: Adding a detailed error message when the template execution fails can significantly aid in debugging and understanding the context of the error. This is a valuable enhancement for error handling.

    9
    Use descriptive error messages in test assertions

    Use a more descriptive error message in the test assertion to provide clarity on what the
    expected outcome should be.

    gateway/event_handler_webhooks_test.go [178]

    -t.Errorf("Body incorrect, is: %q", body)
    +t.Errorf("Body incorrect, expected to contain %q, got: %q", expectedBody, body)
     
    Suggestion importance[1-10]: 8

    Why: Using a more descriptive error message in the test assertion provides clarity on what the expected outcome should be, which improves the readability and maintainability of the test code.

    8
    Best practice
    Improve error handling in tests for better error reporting

    Replace the direct panic on error with a more controlled error handling approach in tests
    to avoid abrupt test exits and allow for better error reporting.

    gateway/event_handler_webhooks_test.go [25]

    -assert.NoError(t, err)
    +if err != nil {
    +    t.Errorf("Unexpected error: %v", err)
    +}
     
    Suggestion importance[1-10]: 7

    Why: Replacing the direct panic on error with a more controlled error handling approach in tests can avoid abrupt test exits and provide better error reporting. However, the existing use of assert.NoError is already a common practice in Go tests.

    7
    Explicitly check for errors in tests for detailed error handling

    Consider checking for errors explicitly in tests instead of using assert.NoError to ensure
    that all errors are properly handled and logged.

    gateway/event_handler_webhooks_test.go [199]

    -assert.NoError(t, err)
    +if err != nil {
    +    t.Errorf("Error while creating body: %v", err)
    +}
     
    Suggestion importance[1-10]: 7

    Why: Explicitly checking for errors instead of using assert.NoError ensures that all errors are properly handled and logged. This can provide more detailed error information, although assert.NoError is already a standard practice in Go tests.

    7

    @titpetric titpetric changed the title Fix poor error handling in webhook event templates [TT-12193] Fix poor error handling in webhook event templates May 23, 2024
    @sonarqubecloud

    Copy link
    Copy Markdown

    Quality Gate Failed Quality Gate failed

    Failed conditions
    50.0% Coverage on New Code (required ≥ 80%)

    See analysis details on SonarCloud

    @titpetric
    titpetric enabled auto-merge (squash) May 23, 2024 08:10
    @titpetric
    titpetric merged commit 11d5798 into master May 23, 2024
    @titpetric
    titpetric deleted the fix/event-handler-webhooks-error-handling branch May 23, 2024 08:24
    @titpetric

    Copy link
    Copy Markdown
    Contributor Author

    /release to release-5.3

    @tykbot

    tykbot Bot commented May 23, 2024

    Copy link
    Copy Markdown

    Working on it! Note that it can take a few minutes.

    tykbot Bot pushed a commit that referenced this pull request May 23, 2024
    ### **User description**
    Fixes error handling in webhook handler templates
    
    https://tyktech.atlassian.net/browse/TT-12193
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added error handling for template execution in the `CreateBody` method
    of `WebHookHandler`.
    - Refactored test helper function to include error handling.
    - Replaced panic and error checks with `assert.NoError` in tests.
    - Improved error messages in tests for better clarity.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug fix
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_handler_webhooks.go</strong><dd><code>Add error
    handling in `CreateBody` method of WebHookHandler</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks.go
    <li>Added error handling for template execution in
    <code>CreateBody</code> method.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-6587ad3f2629cfa6c84a71144127acd6cc7824e5141f0b4961848945a87e0198">+4/-1</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    
    <summary><strong>event_handler_webhooks_test.go</strong><dd><code>Improve
    error handling and assertions in webhook handler
    tests</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks_test.go
    <li>Refactored test helper function to include error handling.<br> <li>
    Replaced panic and error checks with <code>assert.NoError</code>.<br>
    <li> Improved error messages in tests.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-1a2f8d6ab4443031b0f8486809453f6389291a6f625745fe8c65ac5cdb0e9621">+22/-24</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    Co-authored-by: Tit Petric <[email protected]>
    (cherry picked from commit 11d5798)
    @tykbot

    tykbot Bot commented May 23, 2024

    Copy link
    Copy Markdown

    @titpetric Succesfully merged PR

    buger added a commit that referenced this pull request May 23, 2024
    … event templates (#6303)
    
    [TT-12193] Fix poor error handling in webhook event templates (#6303)
    
    ### **User description**
    Fixes error handling in webhook handler templates
    
    https://tyktech.atlassian.net/browse/TT-12193
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added error handling for template execution in the `CreateBody` method
    of `WebHookHandler`.
    - Refactored test helper function to include error handling.
    - Replaced panic and error checks with `assert.NoError` in tests.
    - Improved error messages in tests for better clarity.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug fix
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_handler_webhooks.go</strong><dd><code>Add error
    handling in `CreateBody` method of WebHookHandler</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks.go
    <li>Added error handling for template execution in
    <code>CreateBody</code> method.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-6587ad3f2629cfa6c84a71144127acd6cc7824e5141f0b4961848945a87e0198">+4/-1</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    
    <summary><strong>event_handler_webhooks_test.go</strong><dd><code>Improve
    error handling and assertions in webhook handler
    tests</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks_test.go
    <li>Refactored test helper function to include error handling.<br> <li>
    Replaced panic and error checks with <code>assert.NoError</code>.<br>
    <li> Improved error messages in tests.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-1a2f8d6ab4443031b0f8486809453f6389291a6f625745fe8c65ac5cdb0e9621">+22/-24</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    Co-authored-by: Tit Petric <[email protected]>
    nerdydread pushed a commit that referenced this pull request Sep 6, 2024
    ### **User description**
    Fixes error handling in webhook handler templates
    
    https://tyktech.atlassian.net/browse/TT-12193
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added error handling for template execution in the `CreateBody` method
    of `WebHookHandler`.
    - Refactored test helper function to include error handling.
    - Replaced panic and error checks with `assert.NoError` in tests.
    - Improved error messages in tests for better clarity.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug fix
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_handler_webhooks.go</strong><dd><code>Add error
    handling in `CreateBody` method of WebHookHandler</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks.go
    <li>Added error handling for template execution in
    <code>CreateBody</code> method.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-6587ad3f2629cfa6c84a71144127acd6cc7824e5141f0b4961848945a87e0198">+4/-1</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    
    <summary><strong>event_handler_webhooks_test.go</strong><dd><code>Improve
    error handling and assertions in webhook handler
    tests</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks_test.go
    <li>Refactored test helper function to include error handling.<br> <li>
    Replaced panic and error checks with <code>assert.NoError</code>.<br>
    <li> Improved error messages in tests.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-1a2f8d6ab4443031b0f8486809453f6389291a6f625745fe8c65ac5cdb0e9621">+22/-24</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    Co-authored-by: Tit Petric <[email protected]>
    edsonmichaque pushed a commit that referenced this pull request Feb 18, 2025
    … event templates (#6303)
    
    [TT-12193] Fix poor error handling in webhook event templates (#6303)
    
    ### **User description**
    Fixes error handling in webhook handler templates
    
    https://tyktech.atlassian.net/browse/TT-12193
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added error handling for template execution in the `CreateBody` method
    of `WebHookHandler`.
    - Refactored test helper function to include error handling.
    - Replaced panic and error checks with `assert.NoError` in tests.
    - Improved error messages in tests for better clarity.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug fix
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_handler_webhooks.go</strong><dd><code>Add error
    handling in `CreateBody` method of WebHookHandler</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks.go
    <li>Added error handling for template execution in
    <code>CreateBody</code> method.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-6587ad3f2629cfa6c84a71144127acd6cc7824e5141f0b4961848945a87e0198">+4/-1</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    
    <summary><strong>event_handler_webhooks_test.go</strong><dd><code>Improve
    error handling and assertions in webhook handler
    tests</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks_test.go
    <li>Refactored test helper function to include error handling.<br> <li>
    Replaced panic and error checks with <code>assert.NoError</code>.<br>
    <li> Improved error messages in tests.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-1a2f8d6ab4443031b0f8486809453f6389291a6f625745fe8c65ac5cdb0e9621">+22/-24</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    Co-authored-by: Tit Petric <[email protected]>
    edsonmichaque pushed a commit that referenced this pull request Feb 18, 2025
    … event templates (#6303)
    
    [TT-12193] Fix poor error handling in webhook event templates (#6303)
    
    ### **User description**
    Fixes error handling in webhook handler templates
    
    https://tyktech.atlassian.net/browse/TT-12193
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added error handling for template execution in the `CreateBody` method
    of `WebHookHandler`.
    - Refactored test helper function to include error handling.
    - Replaced panic and error checks with `assert.NoError` in tests.
    - Improved error messages in tests for better clarity.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug fix
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_handler_webhooks.go</strong><dd><code>Add error
    handling in `CreateBody` method of WebHookHandler</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks.go
    <li>Added error handling for template execution in
    <code>CreateBody</code> method.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-6587ad3f2629cfa6c84a71144127acd6cc7824e5141f0b4961848945a87e0198">+4/-1</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    
    <summary><strong>event_handler_webhooks_test.go</strong><dd><code>Improve
    error handling and assertions in webhook handler
    tests</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks_test.go
    <li>Refactored test helper function to include error handling.<br> <li>
    Replaced panic and error checks with <code>assert.NoError</code>.<br>
    <li> Improved error messages in tests.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-1a2f8d6ab4443031b0f8486809453f6389291a6f625745fe8c65ac5cdb0e9621">+22/-24</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    Co-authored-by: Tit Petric <[email protected]>
    edsonmichaque pushed a commit to edsonmichaque/tyk-1 that referenced this pull request Feb 18, 2025
    … event templates (TykTechnologies#6303)
    
    [TT-12193] Fix poor error handling in webhook event templates (TykTechnologies#6303)
    
    ### **User description**
    Fixes error handling in webhook handler templates
    
    https://tyktech.atlassian.net/browse/TT-12193
    
    ___
    
    ### **PR Type**
    Bug fix, Tests
    
    
    ___
    
    ### **Description**
    - Added error handling for template execution in the `CreateBody` method
    of `WebHookHandler`.
    - Refactored test helper function to include error handling.
    - Replaced panic and error checks with `assert.NoError` in tests.
    - Improved error messages in tests for better clarity.
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant
    files</th></tr></thead><tbody><tr><td><strong>Bug fix
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    <summary><strong>event_handler_webhooks.go</strong><dd><code>Add error
    handling in `CreateBody` method of WebHookHandler</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks.go
    <li>Added error handling for template execution in
    <code>CreateBody</code> method.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-6587ad3f2629cfa6c84a71144127acd6cc7824e5141f0b4961848945a87e0198">+4/-1</a>&nbsp;
    &nbsp; &nbsp; </td>
    </tr>                    
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
      <td>
        <details>
    
    <summary><strong>event_handler_webhooks_test.go</strong><dd><code>Improve
    error handling and assertions in webhook handler
    tests</code></dd></summary>
    <hr>
    
    gateway/event_handler_webhooks_test.go
    <li>Refactored test helper function to include error handling.<br> <li>
    Replaced panic and error checks with <code>assert.NoError</code>.<br>
    <li> Improved error messages in tests.<br>
    
    
    </details>
        
    
      </td>
    <td><a
    href="https://github.com/TykTechnologies/tyk/pull/6303/files#diff-1a2f8d6ab4443031b0f8486809453f6389291a6f625745fe8c65ac5cdb0e9621">+22/-24</a>&nbsp;
    </td>
    </tr>                    
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools
    and their descriptions
    
    Co-authored-by: Tit Petric <[email protected]>
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants