Skip to content

Tested extecuteLedgerOrder function#1895

Merged
findolor merged 7 commits intomainfrom
Tested-extecuteLedgerOrder-function
May 28, 2025
Merged

Tested extecuteLedgerOrder function#1895
findolor merged 7 commits intomainfrom
Tested-extecuteLedgerOrder-function

Conversation

@hardingjam
Copy link
Copy Markdown
Contributor

@hardingjam hardingjam commented May 23, 2025

Motivation

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Improved order execution process with enhanced error handling and validation during order placement.
  • Bug Fixes

    • Errors during order execution are now properly reported and surfaced to users with clear messages.
  • Tests

    • Added comprehensive tests to verify order execution behavior and error handling.

@hardingjam hardingjam added this to the Test coverage, tech debt milestone May 23, 2025
@hardingjam hardingjam requested review from findolor and hardyjosh May 23, 2025 10:34
@hardingjam hardingjam self-assigned this May 23, 2025
@hardingjam hardingjam added the test test code label May 23, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 23, 2025

Walkthrough

A new utility function, executeLedgerOrder, was introduced to centralize order execution logic, including error handling and validation. The function is now used in the orders add page, replacing previous inline logic. Unit tests for executeLedgerOrder were also added to verify its behavior in various scenarios.

Changes

File(s) Change Summary
tauri-app/src/lib/services/executeLedgerOrder.ts Added new utility function executeLedgerOrder with validation and error reporting logic.
tauri-app/src/lib/tests/executeLedgerOrder.test.ts Added unit tests for executeLedgerOrder, covering success and error scenarios.
tauri-app/src/routes/orders/add/+page.svelte Refactored to use executeLedgerOrder; renamed handler and updated error handling and modal prop.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SveltePage
    participant executeLedgerOrder
    participant orderAdd
    participant Sentry

    User->>SveltePage: Triggers handleExecuteLedger()
    SveltePage->>executeLedgerOrder: Calls with (dotrainText, deployment, orderAdd, reportErrorToSentry)
    executeLedgerOrder->>executeLedgerOrder: Validates deployment.orderbook
    alt orderbook missing
        executeLedgerOrder-->>SveltePage: Throws error
    else orderbook present
        executeLedgerOrder->>orderAdd: Executes orderAdd(dotrainText, deployment)
        alt orderAdd throws error
            executeLedgerOrder->>Sentry: reportErrorToSentry(error)
            executeLedgerOrder-->>SveltePage: Throws error
        else success
            executeLedgerOrder-->>SveltePage: Returns
        end
    end
Loading

Suggested labels

Tauri

Suggested reviewers

  • hardyjosh
  • findolor

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm warn config production Use --omit=dev instead.
npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-05-28T09_07_28_324Z-debug-0.log


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

*/
export async function executeLedgerOrder(
dotrainText: string,
deployment: DeploymentCfg | undefined,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i think you can remove undefined from the type since you are checking for its existence in the page you are calling

// eslint-disable-next-line @typescript-eslint/no-explicit-any
reportErrorToSentryFn: (error: any, level?: SentrySeverityLevel) => void,
): Promise<void> {
if (!deployment) throw Error('Select a deployment to add order');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

at the same time you can remove this after removing the type

@hardingjam hardingjam linked an issue May 23, 2025 that may be closed by this pull request
9 tasks
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reportErrorToSentryFn: (error: any, level?: SentrySeverityLevel) => void,
): Promise<void> {
if (isEmpty(deployment.order?.orderbook) || isEmpty(deployment.order.orderbook?.address))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

do we also need these checks? i think if we have deployment, we should also have the inner fields. can you confirm?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

only the check for order.orderbook is needed, as that is an optional property of sgOrder

@hardingjam hardingjam enabled auto-merge May 28, 2025 09:06
@hardingjam hardingjam disabled auto-merge May 28, 2025 09:06
Copy link
Copy Markdown
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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 99fc3de and 5140ad8.

📒 Files selected for processing (1)
  • tauri-app/src/routes/orders/add/+page.svelte (3 hunks)
🔇 Additional comments (2)
tauri-app/src/routes/orders/add/+page.svelte (2)

36-36: LGTM: Clean import of the new service function.

The import correctly adds the executeLedgerOrder service function that centralizes order execution logic.


273-273: LGTM: Consistent function reference update.

The modal prop correctly references the renamed handleExecuteLedger function, maintaining consistency with the refactored code.

Comment on lines +147 to 156
async function handleExecuteLedger() {
isSubmitting = true;
try {
if (!deployment) throw Error('Select a deployment to add order');
if (isEmpty(deployment.order?.orderbook) || isEmpty(deployment.order.orderbook?.address))
throw Error('No orderbook associated with scenario');

await orderAdd($globalDotrainFile.text, deployment);
} catch (e) {
reportErrorToSentry(e);
await executeLedgerOrder($globalDotrainFile.text, deployment, orderAdd, reportErrorToSentry);
} catch (e: unknown) {
toasts.error((e as Error).message || 'Ledger execution failed');
}
isSubmitting = false;
}
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.

🛠️ Refactor suggestion

Improve error handling type safety.

The refactoring to use executeLedgerOrder is a good separation of concerns. However, the error handling can be improved for better type safety.

Apply this diff to improve error handling:

-    } catch (e: unknown) {
-      toasts.error((e as Error).message || 'Ledger execution failed');
+    } catch (e: unknown) {
+      const errorMessage = e instanceof Error ? e.message : 'Ledger execution failed';
+      toasts.error(errorMessage);

This ensures safer error message extraction without unsafe type assertions.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async function handleExecuteLedger() {
isSubmitting = true;
try {
if (!deployment) throw Error('Select a deployment to add order');
if (isEmpty(deployment.order?.orderbook) || isEmpty(deployment.order.orderbook?.address))
throw Error('No orderbook associated with scenario');
await orderAdd($globalDotrainFile.text, deployment);
} catch (e) {
reportErrorToSentry(e);
await executeLedgerOrder($globalDotrainFile.text, deployment, orderAdd, reportErrorToSentry);
} catch (e: unknown) {
toasts.error((e as Error).message || 'Ledger execution failed');
}
isSubmitting = false;
}
async function handleExecuteLedger() {
isSubmitting = true;
try {
if (!deployment) throw Error('Select a deployment to add order');
await executeLedgerOrder($globalDotrainFile.text, deployment, orderAdd, reportErrorToSentry);
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : 'Ledger execution failed';
toasts.error(errorMessage);
}
isSubmitting = false;
}
🤖 Prompt for AI Agents
In tauri-app/src/routes/orders/add/+page.svelte around lines 147 to 156, the
catch block uses an unsafe type assertion to extract the error message. To
improve type safety, modify the catch block to safely check if the caught error
is an instance of Error before accessing its message property, and provide a
fallback message if not. This avoids unsafe casting and ensures robust error
handling.

@findolor findolor merged commit 2fc7bd1 into main May 28, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test test code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing tests for tauri app page components

3 participants