Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception Handling and Examples for Expected Failures #264

Merged
merged 1 commit into from
Apr 5, 2024
Merged

Exception Handling and Examples for Expected Failures #264

merged 1 commit into from
Apr 5, 2024

Conversation

davidvonthenen
Copy link
Contributor

@davidvonthenen davidvonthenen commented Apr 5, 2024

Addresses issue: #252

This was the stress test to throw as many exceptions and to test edge cases as best as I could. In these scenarios, I found a few issues.

Changes include:

  • In AbstractRestClient just better logging of the exceptions based on log levels
  • In LiveClient, this is where I found a couple of issues went it came to cancelling. These changes include:
    • Connect() and Stop() Each get their own CancelToken to be used for only that operation. That is the token that is passed into the function. That token is optional and a default will be used to timeout those operations in 5 seconds.
    • The internal CancelToken is only used for cancelling out of threads for thread management. I initially try to see if I could compact these two into a single token, but that proves to be "janky" and unpredictable. KISS.
    • Fixes exception logging similar to that of AbstractRestClient.
  • Implemented the ability to disable Logging
  • Cleaned up some examples for Demo purposes.

Summary by CodeRabbit

  • New Features
    • Added new projects for handling different scenarios such as trigger cancellation, bad host detection, and WebSocket management.
    • Introduced a default timeout for connect/disconnect operations to improve reliability.
    • Implemented a new logging level to allow disabling logs, enhancing user control over application verbosity.
  • Improvements
    • Enhanced logging statements across the application for better clarity and debugging.
    • Improved exception handling messages to include more detailed information.
    • Updated WebSocket closure logic for more robust connection management.
  • Documentation
    • Added examples and tests showcasing the usage of the SDK for microphone streaming and handling specific failure scenarios.
  • Bug Fixes
    • Corrected project reference paths to resolve file path issues in test projects.

Copy link
Contributor

coderabbitai bot commented Apr 5, 2024

Walkthrough

The updates across the solution primarily focus on enhancing logging, exception handling, and token management for various components, including REST clients, WebSocket connections, and the microphone streaming example. New projects have been introduced, reflecting a broader test coverage for expected failures and new functionalities. Adjustments in project paths and references aim to streamline the solution structure. The addition of a Disable log level and specific timeout constants further refine the operational parameters and debugging capabilities.

Changes

File(s) Summary
Deepgram.Dev.sln, .../exercise_timeout/ExerciseTimeout.csproj Adjustments in project declarations and paths, including new projects and relocations.
Deepgram/Abstractions/AbstractRestClient.cs, Deepgram/Clients/Live/v1/Client.cs, examples/streaming/microphone/Program.cs Enhanced logging, method signature clarity, and exception handling. Adjustments in token usage and logging levels.
Deepgram/Clients/Live/v1/Constants.cs, Deepgram/Logger/Constants.cs Introduction of DefaultConnectTimeout constant and Disable log level.
Deepgram/Logger/Log.cs Handling for Disable log level to log fatal events to the console.
tests/expected_failures/rest/bad_host/BadHost.csproj, tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj New .NET project files targeting net6.0, including references and features.
tests/expected_failures/rest/bad_host/Program.cs, tests/expected_failures/rest/trigger_cancel/Program.cs, tests/expected_failures/websocket/exercise_timeout/Program.cs Functionality introductions for transcription and logging adjustments, including handling exceptions and cancellation.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 8

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 138a52d and 0e71d44.
Files selected for processing (13)
  • Deepgram.Dev.sln (6 hunks)
  • Deepgram/Abstractions/AbstractRestClient.cs (17 hunks)
  • Deepgram/Clients/Live/v1/Client.cs (12 hunks)
  • Deepgram/Clients/Live/v1/Constants.cs (1 hunks)
  • Deepgram/Logger/Constants.cs (1 hunks)
  • Deepgram/Logger/Log.cs (2 hunks)
  • examples/streaming/microphone/Program.cs (6 hunks)
  • tests/expected_failures/rest/bad_host/BadHost.csproj (1 hunks)
  • tests/expected_failures/rest/bad_host/Program.cs (1 hunks)
  • tests/expected_failures/rest/trigger_cancel/Program.cs (1 hunks)
  • tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj (1 hunks)
  • tests/expected_failures/websocket/exercise_timeout/ExerciseTimeout.csproj (1 hunks)
  • tests/expected_failures/websocket/exercise_timeout/Program.cs (1 hunks)
Additional Context Used
Learnings (1)
Deepgram.Dev.sln (1)
User: dvonthenen"
PR: deepgram/deepgram-dotnet-sdk#253
File: Deepgram.Dev.sln:0-0
Timestamp: 2024-03-28T19:45:21.188Z
Learning: References to "Prerecorded" and other project names in the documentation and build scripts have been updated as part of the PR changes. Remaining mentions of "Prerecorded" in `Deepgram/README.md`, `examples/README.md`, and `README.md` were noted for potential review.
Additional comments not posted (19)
tests/expected_failures/websocket/exercise_timeout/ExerciseTimeout.csproj (1)

11-11: Ensure the updated project reference path "..\..\..\..\Deepgram\Deepgram.csproj" correctly reflects the new directory structure of the project.

tests/expected_failures/rest/bad_host/BadHost.csproj (1)

11-11: Ensure the updated project reference path "..\..\..\..\Deepgram\Deepgram.csproj" correctly reflects the new directory structure of the project.

tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj (1)

11-11: Ensure the updated project reference path "..\..\..\..\Deepgram\Deepgram.csproj" correctly reflects the new directory structure of the project.

Deepgram/Clients/Live/v1/Constants.cs (1)

15-16: The addition of DefaultConnectTimeout with a value of 5000 milliseconds is a sensible enhancement for managing default connect/disconnect timeouts.

Deepgram/Logger/Constants.cs (1)

48-51: The addition of the Disable enum value to LogLevel provides users with the option to explicitly disable logging, enhancing flexibility and control.

tests/expected_failures/rest/bad_host/Program.cs (1)

23-23: Consider adding a comment explaining the use of a non-routable IP address (127.0.0.1) in DeepgramHttpClientOptions to simulate a bad host scenario for demonstration purposes.

tests/expected_failures/rest/trigger_cancel/Program.cs (1)

25-25: Consider adding a comment explaining the use of a short delay (250ms) in CancellationTokenSource to simulate an intentional request cancellation for demonstration purposes.

tests/expected_failures/websocket/exercise_timeout/Program.cs (1)

17-17: Consider adding a comment explaining the choice of LogLevel.Debug for more verbose logging in demonstration scenarios.

examples/streaming/microphone/Program.cs (3)

21-23: Clarify the example for logging initialization to prevent confusion.
Consider adding a comment explaining that the commented line is an alternative way to initialize logging with a different verbosity level.


37-37: The addition of a newline before logging event types enhances the readability of the output.


95-95: Stopping the microphone and cleaning up resources is a good practice for proper resource management.

Deepgram.Dev.sln (1)

128-141: Ensure the newly added projects ("trigger_cancel," "bad_host," "websocket," and "rest") and the reorganized "ExerciseTimeout" project are correctly referenced in the documentation and build scripts, reflecting the changes made in this PR.

Deepgram/Abstractions/AbstractRestClient.cs (6)

48-54: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [51-86]

Consider using structured logging for better log management and analysis.

- Log.Verbose("AbstractRestClient.GetAsync<T>", "ENTER");
- Log.Debug("GetAsync<T>", $"uriSegment: {uriSegment}");
- Log.Debug("GetAsync<T>", $"addons: {addons}");
+ Log.Verbose($"{{Method}} ENTER", "AbstractRestClient.GetAsync<T>");
+ Log.Debug($"{{Method}} uriSegment: {{uriSegment}}, addons: {{addons}}", "GetAsync<T>", uriSegment, addons);

This approach allows for easier parsing and querying of logs, especially when dealing with complex systems or when troubleshooting.


289-295: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [292-330]

In the PostAsync<S, T> method, validate the cancellationToken before proceeding with the operation.

+ if (cancellationToken == null || cancellationToken.Token.CanBeCanceled == false)
+ {
+     throw new ArgumentException("A valid CancellationToken must be provided.");
+ }

This ensures that a valid cancellation token is always passed to the method, enhancing the robustness of cancellation handling.


327-356: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [353-401]

For the PostAsync<R, S, T> method, consider adding error handling for the case where content is expected to be a Stream but is null.

if (typeof(R) == typeof(Stream))
{
    Stream? stream = content as Stream;
+   if (stream == null)
+   {
+       throw new ArgumentNullException(nameof(content), "Content cannot be null when expected to be a Stream.");
+   }
    request.Content = HttpRequestUtil.CreateStreamPayload(stream);
}

This addition prevents potential null reference exceptions and ensures that the method's contract is correctly enforced.


427-433: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [430-475]

In the PatchAsync<S, T> method, consider adding a check to ensure that the cancellationToken is not already canceled before starting the operation.

+ if (cancellationToken != null && cancellationToken.Token.IsCancellationRequested)
+ {
+     Log.Information("PatchAsync<S, T>", "Cancellation was requested before the operation started.");
+     return default(T);
+ }

This preemptive check can save resources by avoiding unnecessary operations when cancellation is already requested.


544-565: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [505-562]

In the PutAsync<S, T> method, ensure sensitive headers are not logged to avoid potential security risks.

if (headers != null)
{
    foreach (var header in headers)
    {
        var tmp = header.Key.ToLower();
-       if ( !(tmp.Contains("password") || tmp.Contains("token") || tmp.Contains("authorization") || tmp.Contains("auth")) )
+       if (tmp.Contains("password") || tmp.Contains("token") || tmp.Contains("authorization") || tmp.Contains("auth"))
+       {
+           Log.Debug("PutAsync<S, T>", $"Skipping sensitive header {header.Key}");
+           continue;
+       }
        request.Headers.Add(header.Key, header.Value);
    }
}

This change ensures that sensitive information is not inadvertently exposed in the logs.


605-626: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [574-623]

For the DeleteAsync<T> method, consider adding a check for the cancellation token's validity to ensure it can be canceled.

+ if (cancellationToken == null || cancellationToken.Token.CanBeCanceled == false)
+ {
+     throw new ArgumentException("A valid CancellationToken must be provided.");
+ }

This ensures that the method behaves predictably in scenarios where cancellation might be necessary.

Deepgram/Clients/Live/v1/Client.cs (1)

72-87: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [62-83]

In the Connect method, consider using a more descriptive variable name for cancelToken to clarify its purpose.

- CancellationTokenSource? cancelToken = null
+ CancellationTokenSource? connectionCancellationToken = null

Renaming the variable enhances code readability by making the cancellation token's role more explicit.

Deepgram/Logger/Log.cs Show resolved Hide resolved
Deepgram/Abstractions/AbstractRestClient.cs Show resolved Hide resolved
Deepgram/Abstractions/AbstractRestClient.cs Show resolved Hide resolved
Deepgram/Clients/Live/v1/Client.cs Show resolved Hide resolved
Deepgram/Clients/Live/v1/Client.cs Show resolved Hide resolved
Deepgram/Clients/Live/v1/Client.cs Outdated Show resolved Hide resolved
Deepgram/Clients/Live/v1/Client.cs Show resolved Hide resolved
Deepgram/Clients/Live/v1/Client.cs Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 138a52d and 8e75582.
Files selected for processing (13)
  • Deepgram.Dev.sln (6 hunks)
  • Deepgram/Abstractions/AbstractRestClient.cs (17 hunks)
  • Deepgram/Clients/Live/v1/Client.cs (12 hunks)
  • Deepgram/Clients/Live/v1/Constants.cs (1 hunks)
  • Deepgram/Logger/Constants.cs (1 hunks)
  • Deepgram/Logger/Log.cs (2 hunks)
  • examples/streaming/microphone/Program.cs (6 hunks)
  • tests/expected_failures/rest/bad_host/BadHost.csproj (1 hunks)
  • tests/expected_failures/rest/bad_host/Program.cs (1 hunks)
  • tests/expected_failures/rest/trigger_cancel/Program.cs (1 hunks)
  • tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj (1 hunks)
  • tests/expected_failures/websocket/exercise_timeout/ExerciseTimeout.csproj (1 hunks)
  • tests/expected_failures/websocket/exercise_timeout/Program.cs (1 hunks)
Files skipped from review as they are similar to previous changes (13)
  • Deepgram.Dev.sln
  • Deepgram/Abstractions/AbstractRestClient.cs
  • Deepgram/Clients/Live/v1/Client.cs
  • Deepgram/Clients/Live/v1/Constants.cs
  • Deepgram/Logger/Constants.cs
  • Deepgram/Logger/Log.cs
  • examples/streaming/microphone/Program.cs
  • tests/expected_failures/rest/bad_host/BadHost.csproj
  • tests/expected_failures/rest/bad_host/Program.cs
  • tests/expected_failures/rest/trigger_cancel/Program.cs
  • tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj
  • tests/expected_failures/websocket/exercise_timeout/ExerciseTimeout.csproj
  • tests/expected_failures/websocket/exercise_timeout/Program.cs

jpvajda
jpvajda previously approved these changes Apr 5, 2024
Copy link
Contributor

@jpvajda jpvajda left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 856fccb and d073e5c.
Files selected for processing (13)
  • Deepgram.Dev.sln (6 hunks)
  • Deepgram/Abstractions/AbstractRestClient.cs (17 hunks)
  • Deepgram/Clients/Live/v1/Client.cs (12 hunks)
  • Deepgram/Clients/Live/v1/Constants.cs (1 hunks)
  • Deepgram/Logger/Constants.cs (1 hunks)
  • Deepgram/Logger/Log.cs (2 hunks)
  • examples/streaming/microphone/Program.cs (6 hunks)
  • tests/expected_failures/rest/bad_host/BadHost.csproj (1 hunks)
  • tests/expected_failures/rest/bad_host/Program.cs (1 hunks)
  • tests/expected_failures/rest/trigger_cancel/Program.cs (1 hunks)
  • tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj (1 hunks)
  • tests/expected_failures/websocket/exercise_timeout/ExerciseTimeout.csproj (1 hunks)
  • tests/expected_failures/websocket/exercise_timeout/Program.cs (1 hunks)
Files skipped from review as they are similar to previous changes (13)
  • Deepgram.Dev.sln
  • Deepgram/Abstractions/AbstractRestClient.cs
  • Deepgram/Clients/Live/v1/Client.cs
  • Deepgram/Clients/Live/v1/Constants.cs
  • Deepgram/Logger/Constants.cs
  • Deepgram/Logger/Log.cs
  • examples/streaming/microphone/Program.cs
  • tests/expected_failures/rest/bad_host/BadHost.csproj
  • tests/expected_failures/rest/bad_host/Program.cs
  • tests/expected_failures/rest/trigger_cancel/Program.cs
  • tests/expected_failures/rest/trigger_cancel/TriggerCancel.csproj
  • tests/expected_failures/websocket/exercise_timeout/ExerciseTimeout.csproj
  • tests/expected_failures/websocket/exercise_timeout/Program.cs

Copy link
Contributor

@jpvajda jpvajda left a comment

Choose a reason for hiding this comment

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

LGTM

@davidvonthenen davidvonthenen merged commit c42fca3 into deepgram:main Apr 5, 2024
4 checks passed
@davidvonthenen davidvonthenen deleted the expected-error-cases branch April 5, 2024 22:27
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