Skip to content

Conversation

@omkarK06
Copy link
Contributor

@omkarK06 omkarK06 commented Oct 19, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for data fetching and processing, providing clearer feedback to users.
    • Improved dynamic rendering of error messages and search results in the user interface.
  • Bug Fixes

    • Resolved issues with undefined properties, ensuring robust error message display and a smoother user experience.
  • Refactor

    • Streamlined logic for managing field visibility and refined data processing methods for better performance and clarity.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 19, 2024

Walkthrough

The pull request introduces enhancements to the useLogs composable in web/src/composables/useLogs.ts and the Index.vue component in web/src/plugins/logs. Key modifications include improved error handling in functions such as getQueryPartitions, getPaginatedData, and loadStreamFields. Additionally, the rendering logic for error messages and search results in Index.vue has been refined, utilizing optional chaining for safer property access. Overall, these changes enhance the robustness and clarity of error handling and data processing.

Changes

File Change Summary
web/src/composables/useLogs.ts Enhanced error handling in getQueryPartitions, getPaginatedData, loadStreamFields, updateUrlQueryParams, and generateHistogramData. Improved fallback values in error handling.
web/src/plugins/logs/Index.vue Updated rendering logic for error messages and search results, including optional chaining for safer property access. Simplified conditional rendering and updated collapseFieldList method.

Possibly related PRs

Suggested reviewers

  • nikhilsaikethe

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@omkarK06 omkarK06 force-pushed the fix/cancel_query_undefined_error branch from 0887123 to 35aff79 Compare October 19, 2024 05:28
@github-actions github-actions bot added the ☢️ Bug Something isn't working label Oct 19, 2024
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: 2

🧹 Outside diff range and nitpick comments (3)
web/src/composables/useLogs.ts (3)

Line range hint 1267-1275: Refactor Error Handling to Reduce Redundancy

The error handling code between lines 1267-1275 is similar to other parts of the codebase. Consider creating a utility function to handle errors uniformly, reducing code duplication and improving maintainability.

You could create a function like handleError:

function handleError(err) {
  let errorMsg = '';
  let errorDetail = '';
  let trace_id = '';

  if (err.response != undefined) {
    errorMsg = err.response?.data?.error || err.response?.data?.message || '';
    errorDetail = err.response?.data?.error_detail || '';
    trace_id = err.response?.data?.trace_id || '';
  } else {
    errorMsg = err?.message || '';
    trace_id = err?.trace_id || '';
  }

  return { errorMsg, errorDetail, trace_id };
}

Then replace the error handling code:

- if (err.response != undefined) {
-   searchObj.data.errorMsg = err.response?.data?.error || err.response?.data?.message || "";
-   if (err.response.data.hasOwnProperty("error_detail")) {
-     searchObj.data.errorDetail = err.response.data.error_detail;
-   }
-   if (err.response.data.hasOwnProperty("trace_id")) {
-     trace_id = err.response.data?.trace_id;
-   }
- } else {
-   searchObj.data.errorMsg = err.message;
-   if (err.hasOwnProperty("trace_id")) {
-     trace_id = err?.trace_id;
-   }
- }
+ const { errorMsg, errorDetail, trace_id } = handleError(err);
+ searchObj.data.errorMsg = errorMsg;
+ searchObj.data.errorDetail = errorDetail;
🧰 Tools
🪛 Biome

[error] 1268-1268: Do not access Object.prototype method 'hasOwnProperty' from target object.

It's recommended using Object.hasOwn() instead of using Object.hasOwnProperty().
See MDN web docs for more details.

(lint/suspicious/noPrototypeBuiltins)


Line range hint 1285-1290: Avoid Overwriting Error Messages

At line 1285, searchObj.data.errorMsg is reassigned inside the if (err?.request?.status >= 429) block, potentially overwriting the error message set earlier. Ensure this reassignment is intentional or consider consolidating the error message assignments to prevent loss of information.

Consider adjusting the code:

if (err?.request?.status >= 429) {
  notificationMsg.value = err?.response?.data?.message;
- searchObj.data.errorMsg = err?.response?.data?.message || "";
  searchObj.data.errorDetail = err?.response?.data?.error_detail;
}

Or, if you need to update searchObj.data.errorMsg, ensure it doesn't unintentionally overwrite important messages.


2341-2344: Simplify Optional Chaining Usage

In lines 2341-2344, you can simplify the assignments using optional chaining to make the code cleaner.

Refactored code:

if (err?.request?.status >= 429) {
  notificationMsg.value = err?.response?.data?.message || "";
  searchObj.data.errorMsg = err?.response?.data?.message || "";
  searchObj.data.errorDetail = err?.response?.data?.error_detail || "";
}

This removes unnecessary lines and uses the || operator to provide default values.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6714d26 and ff5ecb0.

📒 Files selected for processing (2)
  • web/src/composables/useLogs.ts (4 hunks)
  • web/src/plugins/logs/Index.vue (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
web/src/plugins/logs/Index.vue (1)

248-250: Improved error handling and security

Great improvements in this section:

  1. The use of optional chaining (?.) enhances code robustness by safely accessing potentially undefined properties.
  2. Utilizing SanitizedHtmlRenderer for displaying HTML content is a excellent security practice, helping to prevent XSS attacks.
  3. The error message display logic has been refined and simplified, improving readability and maintainability.

These changes contribute to a more stable and secure application.

@omkarK06 omkarK06 force-pushed the fix/cancel_query_undefined_error branch from d80f2c1 to 2c678cd Compare October 19, 2024 08:49
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

🧹 Outside diff range and nitpick comments (2)
web/src/composables/useLogs.ts (2)

2310-2311: Consider implementing a centralized error handling function

The TODO comment suggests creating a handleError function to consolidate error handling logic. This is a good idea that could improve code maintainability and consistency across the application.

Would you like assistance in designing and implementing the handleError function? This could involve:

  1. Identifying common error handling patterns in the current code.
  2. Designing a flexible function signature that can handle various error scenarios.
  3. Implementing the function with proper TypeScript typing.
  4. Updating existing error handling code to use the new function.

2319-2323: Comprehensive error handling improvements

These changes enhance the error handling in the getPaginatedData function by:

  1. Using optional chaining (?.) to safely access nested properties.
  2. Providing fallback values with the || operator.
  3. Setting detailed error information on the searchObj.data object.

These improvements make the error handling more robust and consistent with the changes seen earlier in the file.

Once the handleError function mentioned in the TODO comment is implemented, consider refactoring this error handling code to use that centralized function. This would further improve consistency and maintainability across the application.

Example refactor (pseudo-code):

const { errorMsg, errorDetail, trace_id } = handleError(err);
searchObj.data.errorMsg = errorMsg;
searchObj.data.errorDetail = errorDetail;
searchObj.data.errorCode = err?.response?.data?.code || "";

Also applies to: 2329-2330, 2335-2338, 2347-2350

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ff5ecb0 and 2c678cd.

📒 Files selected for processing (2)
  • web/src/composables/useLogs.ts (4 hunks)
  • web/src/plugins/logs/Index.vue (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • web/src/plugins/logs/Index.vue
🧰 Additional context used
📓 Learnings (1)
web/src/composables/useLogs.ts (1)
Learnt from: omkarK06
PR: openobserve/openobserve#4842
File: web/src/composables/useLogs.ts:2332-2332
Timestamp: 2024-10-19T08:47:15.694Z
Learning: Using `||` to provide default values when handling potentially undefined values, such as `err?.response?.data?.code || ""`, is acceptable and works correctly in `web/src/composables/useLogs.ts`.
🪛 ast-grep
web/src/composables/useLogs.ts

[warning] 2316-2316: The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
Context: "Error while processing histogram request."
Note: [CWE-287] Improper Authentication. [REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

🔇 Additional comments (2)
web/src/composables/useLogs.ts (2)

1267-1270: Improved error handling with optional chaining and fallbacks

The changes in this segment enhance the error handling by using optional chaining (?.) and providing fallback values with the || operator. This approach makes the code more robust by gracefully handling potential undefined values in the error response structure.


1288-1288: Consistent error handling for rate-limiting scenarios

This change applies the same improved error handling pattern (optional chaining and fallback value) to the specific case of HTTP 429 errors. It maintains consistency with the earlier improvements and ensures a proper error message is set even if the response structure is unexpected.

@bjp232004 bjp232004 force-pushed the fix/cancel_query_undefined_error branch from 2c678cd to 685a19c Compare October 19, 2024 11:53
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

🧹 Outside diff range and nitpick comments (3)
web/src/composables/useLogs.ts (3)

1288-1288: Consider consistent error message handling

While the use of optional chaining is good, consider aligning this line with the previous error handling pattern for consistency:

- searchObj.data.errorMsg = err?.response?.data?.message || "";
+ searchObj.data.errorMsg = err?.response?.data?.error || err?.response?.data?.message || "";

This change would make the error handling more consistent throughout the function.


2310-2311: Implement centralized error handling

The TODO comment suggests creating a handleError function to centralize error handling logic. This is a good practice that can improve code maintainability and reduce duplication.

Would you like assistance in implementing the handleError function? I can provide a basic implementation that aligns with the current error handling patterns in the code.


Line range hint 2319-2350: Improved error handling with optional chaining

The changes in this segment significantly improve the error handling by using optional chaining and providing fallback values consistently. This makes the code more robust against undefined property access errors.

To further improve the code, consider reducing duplication by extracting the common error handling logic into a separate function. For example:

function extractErrorInfo(err: any) {
  const errorMsg = err?.response?.data?.error || err?.response?.data?.message || err?.message || "Unknown error";
  const errorDetail = err?.response?.data?.error_detail || "";
  const traceId = err?.response?.data?.trace_id || err?.trace_id || "";
  return { errorMsg, errorDetail, traceId };
}

// Usage
const { errorMsg, errorDetail, traceId } = extractErrorInfo(err);
searchObj.data.errorMsg = errorMsg;
searchObj.data.errorDetail = errorDetail;
// ... use traceId as needed

This refactoring would make the error handling more consistent and easier to maintain across different parts of the code.

🧰 Tools
🪛 ast-grep

[warning] 2316-2316: The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
Context: "Error while processing histogram request."
Note: [CWE-287] Improper Authentication. [REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2c678cd and 685a19c.

📒 Files selected for processing (2)
  • web/src/composables/useLogs.ts (4 hunks)
  • web/src/plugins/logs/Index.vue (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • web/src/plugins/logs/Index.vue
🧰 Additional context used
📓 Learnings (1)
web/src/composables/useLogs.ts (1)
Learnt from: omkarK06
PR: openobserve/openobserve#4842
File: web/src/composables/useLogs.ts:2332-2332
Timestamp: 2024-10-19T08:47:15.694Z
Learning: Using `||` to provide default values when handling potentially undefined values, such as `err?.response?.data?.code || ""`, is acceptable and works correctly in `web/src/composables/useLogs.ts`.
🪛 ast-grep
web/src/composables/useLogs.ts

[warning] 2316-2316: The application creates a database connection with an empty password. This can lead to unauthorized access by either an internal or external malicious actor. To prevent this vulnerability, enforce authentication when connecting to a database by using environment variables to securely provide credentials or retrieving them from a secure vault or HSM (Hardware Security Module).
Context: "Error while processing histogram request."
Note: [CWE-287] Improper Authentication. [REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

🔇 Additional comments (1)
web/src/composables/useLogs.ts (1)

1267-1270: Improved error handling with optional chaining

The changes in this segment enhance the error message construction by using optional chaining and providing multiple fallback options. This approach is more robust and prevents potential undefined property access errors.

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

Labels

☢️ Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants