Skip to content

fix: improve error handling by replacing silent failures in djed sdk#99

Open
P-ragati wants to merge 2 commits into
DjedAlliance:mainfrom
P-ragati:fix-djed-error-handling
Open

fix: improve error handling by replacing silent failures in djed sdk#99
P-ragati wants to merge 2 commits into
DjedAlliance:mainfrom
P-ragati:fix-djed-error-handling

Conversation

@P-ragati
Copy link
Copy Markdown

@P-ragati P-ragati commented Mar 18, 2026

Summary

This PR improves error handling across the djed SDK by replacing silent failures (console.log) with explicit error propagation.

Changes

  • Replaced console.log with console.error in catch blocks
  • Added meaningful error messages using throw new Error(...)
  • Prevented silent failures that could return undefined values

Why this change?

Previously, errors were only logged, which could make debugging difficult and lead to unexpected behavior. This update ensures that errors are properly surfaced to the caller.

Impact

  • No change in core logic
  • Improved reliability and debuggability of SDK functions

Summary by CodeRabbit

Bug Fixes

  • Enhanced error reporting and propagation across SDK operations:
    • Reserve coin price calculations now provide explicit failure messages
    • Stable coin price calculations include clear error reporting
    • Fee fetching operations properly propagate errors to callers
    • Improved error messaging strengthens API reliability and debugging

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 18, 2026

Warning

Rate limit exceeded

@P-ragati has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 40 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1fbd7919-99e1-4ddc-811c-ddc7e28d90a0

📥 Commits

Reviewing files that changed from the base of the PR and between d90ab7e and 5274570.

📒 Files selected for processing (3)
  • djed-sdk/src/djed/reserveCoin.js
  • djed-sdk/src/djed/stableCoin.js
  • djed-sdk/src/djed/tradeUtils.js
📝 Walkthrough

Walkthrough

The PR standardizes error handling across three SDK utility files by replacing console.log with console.error in catch blocks and adding explicit error throws to propagate failures. This affects 6 catch blocks across price calculation and fee retrieval functions.

Changes

Cohort / File(s) Summary
Error Handling Standardization
djed-sdk/src/djed/reserveCoin.js, djed-sdk/src/djed/stableCoin.js, djed-sdk/src/djed/tradeUtils.js
Updated catch blocks to use console.error instead of console.log and added explicit throw new Error() statements with descriptive messages for price calculation and fee retrieval functions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 With whiskers twitched in careful review,
I've standardized the errors true,
console.error now rings out clear,
Throwing messages without fear!
Six catches fixed with patterns neat,
Error handling now complete! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing silent failures with explicit error handling throughout the djed SDK, which is the core objective of all three file modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

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

🧹 Nitpick comments (3)
djed-sdk/src/djed/reserveCoin.js (1)

41-42: Keep original exception cause in both reserve-coin price paths.

Lines 42 and 71 currently replace underlying failures with generic errors. Attach the original cause so upstream handlers can inspect the real failure.

Proposed patch (apply pattern to both catch blocks)
   } catch (error) {
     console.error("tradeDataPriceBuyRc error:", error);
-    throw new Error("Failed to calculate reserve coin buy price");
+    throw new Error("Failed to calculate reserve coin buy price", { cause: error });
   }
   } catch (error) {
     console.error("tradeDataPriceSellRc error:", error);
-    throw new Error("Failed to calculate reserve coin sell price");
+    throw new Error("Failed to calculate reserve coin sell price", { cause: error });
   }

Also applies to: 70-71

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@djed-sdk/src/djed/reserveCoin.js` around lines 41 - 42, The catch blocks in
reserveCoin.js currently log the error and throw a new generic Error (messages
like "Failed to calculate reserve coin buy price") which loses the original
exception; update both catch blocks (the one logging "tradeDataPriceBuyRc
error:" and the later one around lines 70-71) to rethrow a new Error that
preserves the original error as the cause (e.g., new Error("Failed to calculate
reserve coin buy price", { cause: error })) so upstream handlers can inspect the
underlying failure, keeping the original console.error logging intact.
djed-sdk/src/djed/stableCoin.js (1)

41-42: Retain root-cause data in all stable-coin error rethrows.

At Lines 42, 76, and 135, the thrown generic errors hide actionable upstream context. Preserve cause consistently across these three catch blocks.

Proposed patch pattern
-throw new Error("Failed to calculate stable coin buy price");
+throw new Error("Failed to calculate stable coin buy price", { cause: error });

-throw new Error("Failed to calculate stable coin sell price");
+throw new Error("Failed to calculate stable coin sell price", { cause: error });

-throw new Error("Failed to calculate future stable coin price");
+throw new Error("Failed to calculate future stable coin price", { cause: error });

Also applies to: 75-76, 134-135

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@djed-sdk/src/djed/stableCoin.js` around lines 41 - 42, In each catch block
that currently logs the error and then throws a new generic Error (e.g., the
block that logs "tradeDataPriceBuySc error:" and the two other stable-coin catch
blocks), preserve the original error by rethrowing with it as the cause—replace
throws like throw new Error("Failed to calculate stable coin buy price") with
throw new Error("Failed to calculate stable coin buy price", { cause: error })
(and do the analogous change for the other two messages) so upstream code
retains the root-cause error; ensure you reference the same human-readable
message used in the console.error for each catch block.
djed-sdk/src/djed/tradeUtils.js (1)

202-203: Preserve upstream error context when rethrowing.

At Line 203, rethrowing a brand-new generic error loses root-cause details for callers. Use new Error("Failed to fetch fees from contract", { cause: error }) to keep the original error attached, maintaining debuggability across the call stack.

This syntax is supported in Node.js 16.9.0+. Since all actively supported Node.js versions (20 LTS, 22, 24) include this feature natively, the suggested change is compatible with current runtimes. If your project needs to support Node.js versions older than 16.9.0, use the fallback: const err = new Error("Failed to fetch fees from contract"); err.cause = error; throw err;

Proposed patch
   } catch (error) {
     console.error("getFees error:", error);
-    throw new Error("Failed to fetch fees from contract");
+    throw new Error("Failed to fetch fees from contract", { cause: error });
   }
 };
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@djed-sdk/src/djed/tradeUtils.js` around lines 202 - 203, The current catch in
getFees logs the error then throws a new generic Error, losing the original
cause; modify the rethrow in the getFees function to preserve the upstream error
by creating the Error with a cause (e.g., new Error("Failed to fetch fees from
contract", { cause: error })) and if you must support older Node versions set
err.cause = error before throwing; keep the existing processLogger/console.error
line but replace the plain throw with the cause-preserving pattern so callers
can inspect error.cause.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@djed-sdk/src/djed/reserveCoin.js`:
- Around line 41-42: The catch blocks in reserveCoin.js currently log the error
and throw a new generic Error (messages like "Failed to calculate reserve coin
buy price") which loses the original exception; update both catch blocks (the
one logging "tradeDataPriceBuyRc error:" and the later one around lines 70-71)
to rethrow a new Error that preserves the original error as the cause (e.g., new
Error("Failed to calculate reserve coin buy price", { cause: error })) so
upstream handlers can inspect the underlying failure, keeping the original
console.error logging intact.

In `@djed-sdk/src/djed/stableCoin.js`:
- Around line 41-42: In each catch block that currently logs the error and then
throws a new generic Error (e.g., the block that logs "tradeDataPriceBuySc
error:" and the two other stable-coin catch blocks), preserve the original error
by rethrowing with it as the cause—replace throws like throw new Error("Failed
to calculate stable coin buy price") with throw new Error("Failed to calculate
stable coin buy price", { cause: error }) (and do the analogous change for the
other two messages) so upstream code retains the root-cause error; ensure you
reference the same human-readable message used in the console.error for each
catch block.

In `@djed-sdk/src/djed/tradeUtils.js`:
- Around line 202-203: The current catch in getFees logs the error then throws a
new generic Error, losing the original cause; modify the rethrow in the getFees
function to preserve the upstream error by creating the Error with a cause
(e.g., new Error("Failed to fetch fees from contract", { cause: error })) and if
you must support older Node versions set err.cause = error before throwing; keep
the existing processLogger/console.error line but replace the plain throw with
the cause-preserving pattern so callers can inspect error.cause.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9a4c913a-b29e-431a-8f62-72eaba629c22

📥 Commits

Reviewing files that changed from the base of the PR and between 509e7d3 and d90ab7e.

📒 Files selected for processing (3)
  • djed-sdk/src/djed/reserveCoin.js
  • djed-sdk/src/djed/stableCoin.js
  • djed-sdk/src/djed/tradeUtils.js

- Attach cause to rethrown errors in reserveCoin, stableCoin, and tradeUtils
- Improves debuggability and error traceability
Copilot AI review requested due to automatic review settings March 22, 2026 16:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to improve the djed SDK’s reliability by replacing silent error logging in key pricing/fee helpers with explicit error propagation so callers can handle failures.

Changes:

  • Updated getFees to log via console.error and throw a descriptive error instead of silently logging.
  • Updated stablecoin pricing helpers to throw descriptive errors on failure (buy/sell + future price calculation).
  • Updated reservecoin pricing helpers to throw descriptive errors on failure (buy/sell).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
djed-sdk/src/djed/tradeUtils.js Changes fee-fetching failure handling to throw instead of silently logging.
djed-sdk/src/djed/stableCoin.js Changes stablecoin pricing helpers to throw with clearer error messages.
djed-sdk/src/djed/reserveCoin.js Changes reservecoin pricing helpers to throw with clearer error messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 201 to 204
} catch (error) {
console.log("error", error);
console.error("getFees error:", error);
throw new Error("Failed to fetch fees from contract", { cause: error });
}
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

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

These changes are only in src/, but djed-sdk/package.json exports dist/umd/index.js + dist/esm/index.js and .npmignore excludes src/. As-is, publishing/consuming the package will not include this improved error handling. Please rebuild and commit the dist/ outputs (or add a build/prepublish step and/or point main/module at the built output generated during publish).

Copilot uses AI. Check for mistakes.
Comment on lines 133 to 136
} catch (error) {
console.log("calculateFutureScPrice error ", error);
console.error("calculateFutureScPrice error:", error);
throw new Error("Failed to calculate future stable coin price", { cause: error });
}
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

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

calculateFutureScPrice calls web3Promise(...), but web3Promise is not imported in this module (only decimalScaling/buildTx are imported from ../helpers). That will cause a ReferenceError at runtime when this function is called (and now gets wrapped by the new throw). Import web3Promise from ../helpers (or reference the correct helper) so the function can actually execute contract calls.

Copilot uses AI. Check for mistakes.
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