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

Divide custom exception into specific ones #261

Merged
merged 13 commits into from
Aug 16, 2024

Conversation

rob93c
Copy link
Member

@rob93c rob93c commented Aug 4, 2024

Along with a major exception overhaul, this pull request also removes the use of the recently deprecated method disableWebPagePreview.

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for Telegram API interactions and file operations.
    • Introduced new exception classes for better specificity in error reporting.
  • Bug Fixes

    • Improved error management across multiple components, ensuring more descriptive error messages.
  • Refactor

    • Updated methods to throw more specific exceptions related to media processing and command execution, enhancing clarity and maintainability.
  • Style

    • Adjusted import statements and class structures for better organisation within the codebase.

@rob93c rob93c added the enhancement New feature or request label Aug 4, 2024
@rob93c rob93c self-assigned this Aug 4, 2024
Copy link

coderabbitai bot commented Aug 4, 2024

Walkthrough

Walkthrough

The recent changes enhance error handling and code coverage reporting in the Stickerifier application. New exception classes, including BaseException, FileOperationException, and MediaException, have been introduced for precise error management. Additionally, the build.gradle file now excludes specific classes from the JaCoCo coverage report, streamlining the analysis. Overall, these updates improve the robustness, maintainability, and functionality of the codebase.

Changes

Files Change Summary
build.gradle Added exclusion pattern for JaCoCo coverage report to omit files in the stickerify/exception directory.
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java Enhanced error handling with new exception types and refined method signatures for clarity.
src/main/java/com/github/stickerifier/stickerify/exception/*.java Introduced new exception classes (BaseException, FileOperationException, MediaException, etc.) to improve error management.
src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java Updated methods to throw more specific exceptions, clarifying error reporting.
src/main/java/com/github/stickerifier/stickerify/process/*.java Changed exception handling to utilise ProcessException, refocusing error management strategies.
src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java Updated test cases to assert MediaException instead of TelegramApiException, reflecting new behaviour.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Bot
    participant MediaHelper
    participant ExceptionHandler
    
    User->>Bot: Send media file
    Bot->>MediaHelper: Process media
    MediaHelper->>MediaHelper: Validate media
    MediaHelper->>ExceptionHandler: Throw MediaException if invalid
    ExceptionHandler-->>Bot: Handle MediaException
    Bot-->>User: Notify failure
Loading

Poem

In the garden of code, a rabbit hops high,
With exceptions anew, watch the errors fly by!
JaCoCo now skips what it doesn’t need,
As we refine our paths, sowing seeds of good deed.
Hooray for the changes, let’s dance and rejoice,
In a world of clean code, we hear the right voice! 🐇✨


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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

CodeRabbit Configuration 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/schema.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

@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

Outside diff range, codebase verification and nitpick comments (3)
src/main/java/com/github/stickerifier/stickerify/exception/MediaOptimizationException.java (1)

1-7: LGTM!

The implementation of the MediaOptimizationException class is correct and follows best practices for custom exceptions.

Suggestion: Add JavaDoc comments.

Adding JavaDoc comments to the class and its constructor can improve code readability and maintainability.

+/**
+ * Exception thrown when media optimization fails.
+ */
public class MediaOptimizationException extends MediaException {
+  /**
+   * Constructs a new MediaOptimizationException with the specified detail message.
+   *
+   * @param message the detail message
+   */
  public MediaOptimizationException(String message) {
    super(message);
  }
}
src/main/java/com/github/stickerifier/stickerify/exception/TelegramApiException.java (1)

1-10: LGTM!

The implementation of the TelegramApiException class is correct and follows best practices for custom exceptions.

Suggestion: Add JavaDoc comments.

Adding JavaDoc comments to the class and its constructor can improve code readability and maintainability.

+/**
+ * Exception thrown when there is an error with the Telegram API.
+ */
public class TelegramApiException extends BaseException {
  /**
   * @see BaseException#BaseException(String, Object...)
   */
+  /**
+   * Constructs a new TelegramApiException with the specified detail message and parameters.
+   *
+   * @param message the detail message
+   * @param parameters the parameters for the message
+   */
  public TelegramApiException(String message, Object... parameters) {
    super(message, parameters);
  }
}
src/main/java/com/github/stickerifier/stickerify/exception/FileOperationException.java (1)

1-11: LGTM!

The implementation of the FileOperationException class is correct and follows best practices for custom exceptions.

Suggestion: Add JavaDoc comments.

Adding JavaDoc comments to the class and its constructors can improve code readability and maintainability.

+/**
+ * Exception thrown when a file operation fails.
+ */
public class FileOperationException extends MediaException {
+  /**
+   * Constructs a new FileOperationException with the specified cause.
+   *
+   * @param cause the cause of the exception
+   */
  public FileOperationException(Throwable cause) {
    super(cause);
  }

+  /**
+   * Constructs a new FileOperationException with the specified detail message and cause.
+   *
+   * @param message the detail message
+   * @param cause the cause of the exception
+   */
  public FileOperationException(String message, Throwable cause) {
    super(message, cause);
  }
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 175ccd5 and 18aad35.

Files selected for processing (14)
  • build.gradle (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (5 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/BaseException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/FileOperationException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/MediaException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/MediaOptimizationException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/ProcessException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/TelegramApiException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (16 hunks)
  • src/main/java/com/github/stickerifier/stickerify/process/PathLocator.java (2 hunks)
  • src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java (3 hunks)
  • src/main/java/com/github/stickerifier/stickerify/telegram/Answer.java (1 hunks)
  • src/test/java/com/github/stickerifier/stickerify/logger/LoggingEvent.java (1 hunks)
  • src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java (2 hunks)
Files skipped from review due to trivial changes (2)
  • src/main/java/com/github/stickerifier/stickerify/telegram/Answer.java
  • src/test/java/com/github/stickerifier/stickerify/logger/LoggingEvent.java
Additional context used
GitHub Check: QDJVM
src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

[warning] 172-172: Check Kotlin and Java source code coverage
Method 'processTelegramFailure' coverage is below the threshold 50%

src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

[warning] 175-175: Check Kotlin and Java source code coverage
Method 'isFileSizeLowerThan' coverage is below the threshold 50%


[warning] 326-326: Check Kotlin and Java source code coverage
Method 'createTempFile' coverage is below the threshold 50%

Additional comments not posted (34)
src/main/java/com/github/stickerifier/stickerify/exception/ProcessException.java (3)

1-2: LGTM!

The package declaration is correct.


3-6: LGTM!

The class ProcessException correctly extends BaseException and the constructor with Throwable cause is implemented correctly.


8-13: LGTM!

The constructor with a parameterized message and JavaDoc comment is implemented correctly.

src/main/java/com/github/stickerifier/stickerify/exception/MediaException.java (5)

1-2: LGTM!

The package declaration is correct.


3-6: LGTM!

The class MediaException correctly extends BaseException and the constructor with a message is implemented correctly.


8-10: LGTM!

The constructor with Throwable cause is implemented correctly.


12-14: LGTM!

The constructor with a message and Throwable cause is implemented correctly.


16-21: LGTM!

The constructor with a parameterized message and JavaDoc comment is implemented correctly.

src/main/java/com/github/stickerifier/stickerify/exception/BaseException.java (4)

1-2: LGTM!

The package declaration is correct.


3-4: LGTM!

The import statement for MessageFormatter is correct.


5-8: LGTM!

The class BaseException correctly extends Exception and the constructor with Throwable cause is implemented correctly.


10-20: LGTM!

The constructor with a parameterized message and JavaDoc comment is implemented correctly. The usage of MessageFormatter.basicArrayFormat is appropriate.

src/main/java/com/github/stickerifier/stickerify/process/PathLocator.java (1)

6-6: LGTM! Verify the usage of ProcessException.

The import statement change aligns with the PR objectives. Ensure that ProcessException is correctly used throughout the codebase.

Verification successful

The usage of ProcessException is correctly implemented throughout the codebase.

The import statement change aligns with the PR objectives. The ProcessException is used consistently and appropriately in the following files:

  • ProcessHelper.java
  • PathLocator.java
  • MediaHelper.java
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `ProcessException` in the codebase.

# Test: Search for the usage of `ProcessException`. Expect: Correct usage in all instances.
rg --type java -A 5 $'ProcessException'

Length of output: 6856

build.gradle (1)

51-51: LGTM! Verify the exclusion pattern.

The exclusion pattern addition aligns with the PR objectives. Ensure that the exclusion pattern is correctly applied in the coverage report.

Verification successful

Verified: The exclusion pattern has been correctly applied.

The exclusion pattern **/stickerify/exception/** is correctly included in the build.gradle file as part of the JaCoCo configuration.

  • build.gradle:51: **/stickerify/exception/**
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the exclusion pattern in the JaCoCo configuration.

# Test: Search for the exclusion pattern in the `build.gradle` file. Expect: Correct exclusion pattern.
rg --type gradle -A 5 $'**/stickerify/exception/**'

Length of output: 154


Script:

#!/bin/bash
# Description: Verify the exclusion pattern in the JaCoCo configuration.

# Test: Search for the exclusion pattern in the `build.gradle` file. Expect: Correct exclusion pattern.
rg --type gradle -F '**/stickerify/exception/**' -A 5

Length of output: 424

src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java (2)

6-6: LGTM! Verify the usage of ProcessException.

The import statement change aligns with the PR objectives. Ensure that ProcessException is correctly used throughout the codebase.

Verification successful

The usage of ProcessException is correctly implemented throughout the codebase.

The import and usage of ProcessException in ProcessHelper.java, PathLocator.java, and MediaHelper.java are consistent with the intended exception handling improvements.

  • ProcessHelper.java: ProcessException is thrown in the executeCommand method.
  • PathLocator.java: ProcessException is caught and logged.
  • MediaHelper.java: ProcessException is caught and rethrown as MediaException.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `ProcessException` in the codebase.

# Test: Search for the usage of `ProcessException`. Expect: Correct usage in all instances.
rg --type java -A 5 $'ProcessException'

Length of output: 6856


25-25: LGTM! Verify the usage of ProcessException.

The method signature change aligns with the PR objectives. Ensure that ProcessException is correctly used throughout the codebase.

Verification successful

Verified the usage of ProcessException.

The ProcessException is used correctly throughout the codebase in the following files:

  • src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java
  • src/main/java/com/github/stickerifier/stickerify/process/PathLocator.java

The exception handling aligns with the intended purpose of enhancing exception handling in the project.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `ProcessException` in the codebase.

# Test: Search for the usage of `ProcessException`. Expect: Correct usage in all instances.
rg --type java -A 5 $'ProcessException'

Length of output: 6856

src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (5)

137-137: LGTM! The changes broaden the scope of error management.

The catch block now handles multiple exception types (TelegramApiException and MediaException), enhancing error management during file answering operations.


Line range hint 144-154: LGTM! The changes improve error handling.

The method signature now includes FileOperationException in its throws clause, reflecting an improved approach to error handling when retrieving files from the Telegram API.


158-163: LGTM! The changes enhance the robustness of error processing.

The method now accepts a more general BaseException instead of just TelegramApiException, allowing for unified handling of different exception types.


172-182: LGTM! The new method improves code clarity and maintainability.

The newly introduced method processTelegramFailure encapsulates the logic for processing Telegram-related failures, enhancing code clarity and maintainability.

Tools
GitHub Check: QDJVM

[warning] 172-172: Check Kotlin and Java source code coverage
Method 'processTelegramFailure' coverage is below the threshold 50%


194-204: LGTM! The changes enhance message formatting flexibility.

The method now includes LinkPreviewOptions, allowing for more nuanced control over link previews in messages sent via the Telegram bot.

src/test/java/com/github/stickerifier/stickerify/media/MediaHelperTest.java (2)

239-239: LGTM! The changes reflect the updated exception handling.

The test case now asserts MediaException instead of TelegramApiException, reflecting a shift in the expected exception type.


246-246: LGTM! The changes reflect the updated exception handling.

The test case now asserts MediaException instead of TelegramApiException, reflecting a shift in the expected exception type.

src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (11)

Line range hint 68-93: LGTM! The changes improve error handling.

The method now throws MediaException instead of TelegramApiException, indicating a more focused error handling approach for media-related issues.


Line range hint 125-154: LGTM! The changes improve error handling.

The method now throws FileOperationException instead of TelegramApiException, reflecting a shift towards more granular exception types that are relevant to file operations.


175-179: LGTM! The changes improve error handling.

The method now throws FileOperationException instead of TelegramApiException, reflecting a shift towards more granular exception types that are relevant to file operations.

Tools
GitHub Check: QDJVM

[warning] 175-175: Check Kotlin and Java source code coverage
Method 'isFileSizeLowerThan' coverage is below the threshold 50%


191-197: LGTM! The changes improve error handling.

The method now throws FileOperationException instead of TelegramApiException, reflecting a shift towards more granular exception types that are relevant to file operations.


Line range hint 220-291: LGTM! The changes improve error handling.

The method now throws MediaException instead of TelegramApiException, aligning with the new error handling strategy.


308-308: LGTM! The changes improve error handling.

The method now throws MediaException instead of TelegramApiException, aligning with the new error handling strategy.


326-330: LGTM! The changes improve error handling.

The method now throws FileOperationException instead of TelegramApiException, reflecting a shift towards more granular exception types that are relevant to file operations.

Tools
GitHub Check: QDJVM

[warning] 326-326: Check Kotlin and Java source code coverage
Method 'createTempFile' coverage is below the threshold 50%


342-342: LGTM! The changes improve error handling.

The method now throws MediaException instead of TelegramApiException, aligning with the new error handling strategy.


361-365: LGTM! The changes improve error handling.

The method now throws MediaException instead of TelegramApiException, aligning with the new error handling strategy.


378-378: LGTM! The changes improve error handling.

The method now throws FileOperationException instead of TelegramApiException, reflecting a shift towards more granular exception types that are relevant to file operations.


Line range hint 400-422: LGTM! The changes improve error handling.

The method now throws MediaException instead of TelegramApiException, aligning with the new error handling strategy.

Copy link

@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 details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 18aad35 and 51d5522.

Files selected for processing (2)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (16 hunks)
  • src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java
  • src/main/java/com/github/stickerifier/stickerify/process/ProcessHelper.java

Copy link

@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 details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 51d5522 and 40a92dc.

Files selected for processing (2)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (6 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (16 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

Copy link

@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 details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 40a92dc and 9fe845f.

Files selected for processing (3)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java (6 hunks)
  • src/main/java/com/github/stickerifier/stickerify/exception/CorruptedVideoException.java (1 hunks)
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java (16 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java
  • src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java
Additional comments not posted (1)
src/main/java/com/github/stickerifier/stickerify/exception/CorruptedVideoException.java (1)

1-7: Well-structured custom exception class.

The CorruptedVideoException class is well-defined and extends MediaException, providing a constructor for detailed error messaging. This aligns with the PR's objective of enhancing exception handling by introducing specific exception types.

Copy link
Contributor

@MartelliEnrico MartelliEnrico left a comment

Choose a reason for hiding this comment

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

LGTM

@rob93c rob93c merged commit 4777750 into main Aug 16, 2024
6 checks passed
@rob93c rob93c deleted the suppress-predictable-telegram-issues branch August 16, 2024 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants