Skip to content

Fix to catch referenced errors in IFERROR/IFNA#275

Merged
ggyuchive merged 1 commit into
wafflebase:mainfrom
resolvicomai:fix/iferror-referenced-errors
May 21, 2026
Merged

Fix to catch referenced errors in IFERROR/IFNA#275
ggyuchive merged 1 commit into
wafflebase:mainfrom
resolvicomai:fix/iferror-referenced-errors

Conversation

@resolvicomai

@resolvicomai resolvicomai commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Reuse the referenced-cell error detection already used by ERROR.TYPE.
  • Make IFERROR(A1, fallback) catch stored error sentinels from referenced cells.
  • Make IFNA(A1, fallback) catch referenced #N/A while preserving other errors.

Why

IFERROR and IFNA evaluate a cell reference to a ref node before the final result is rendered. That meant direct errors were caught, but a referenced cell whose stored value was an error sentinel leaked through. This keeps the behavior consistent with ERROR.TYPE, which already treats stored formula error values as errors.

Linked Issues

Fixes #273

Author checklist

  • I searched existing issues and PRs and confirmed this is not a duplicate.
  • I read every line of this diff and can explain why each change is necessary.
  • Changes follow the conventions in the touched packages; any deviations are explained in Why above.
  • If AI tools assisted with this PR, I noted where in Notes for Reviewers below.

Verification

CI automatically posts a verification summary comment on this PR with
per-lane results for both verify:self and verify:integration.

  • verify:self — CI comment shows ✅
  • verify:integration — CI comment shows ✅

Skip reason (if applicable):

Local verification:

  • pnpm --filter @wafflebase/sheets exec vitest run test/formula/formula.test.ts -t "IFERROR|IFNA|ERROR.TYPE"
  • pnpm --filter @wafflebase/sheets typecheck
  • TZ=UTC pnpm --filter @wafflebase/sheets test
  • npm audit signatures
  • git diff --check

Risk Assessment

  • User-facing risk: Low; scoped to formula error handling for referenced cells.
  • Data/security risk: Low; no persistence, network, or schema changes.
  • Rollback plan: Revert this commit to restore previous formula behavior.

Notes for Reviewers

  • UI changes (screenshots/gifs if applicable): None.
  • Follow-up work (if any): None known.
  • AI assistance: Codex assisted with implementation; I reviewed the diff and verification results before submitting.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • IFERROR function now properly detects errors from cell references and returns the fallback value when errors are found.
    • IFNA function improved to distinguish between #N/A errors (replaced with fallback) and other errors (passed through) when using cell references.
  • Tests

    • Enhanced test coverage for IFERROR and IFNA with referenced error cells.

Review Change Stack

Formula references evaluate to ref nodes first, so IFERROR and IFNA were missing error sentinels already stored in referenced cells. Reuse the ERROR.TYPE error-value helper path so referenced formula errors are detected consistently.
@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR fixes error detection in IFERROR and IFNA functions by introducing a shared errorValueFromNode helper that detects both direct error nodes and errors stored in referenced grid cells. The helper is used by three formula functions, with ERROR.TYPE refactored for consistency, and tests verify the corrected behavior with cell references.

Changes

Error Detection Refactoring

Layer / File(s) Summary
Error extraction helper
packages/sheets/src/formula/functions-helpers.ts
errorValueFromNode helper extracts ErrValue from an EvalNode by checking direct error nodes or resolving references via optional grid lookup, returning undefined if no error is found.
IFERROR and IFNA error detection
packages/sheets/src/formula/functions-logical.ts
iferrorFunc and ifnaFunc now use errorValueFromNode(value, grid) to detect errors. IFERROR returns fallback for any error; IFNA returns fallback only for ErrValue.NA. Both now accept named grid parameter instead of underscore-prefixed variant.
ERROR.TYPE consolidation
packages/sheets/src/formula/functions-info.ts
errortypeFunc is refactored to use the shared errorValueFromNode helper, removing the local resolveErrValue function and consolidating error detection logic.
Grid-based error detection tests
packages/sheets/test/formula/formula.test.ts
Test coverage for IFERROR and IFNA is expanded with grid-based assertions verifying that IFERROR catches errors from referenced cells (both #DIV/0! and #N/A), while IFNA distinguishes #DIV/0! (pass-through) from #N/A (replaced by fallback).

Sequence Diagram

sequenceDiagram
  participant Formula as Formula Evaluation
  participant Helper as errorValueFromNode
  participant Grid as Grid
  Formula->>Helper: errorValueFromNode(node, grid)
  alt node is direct error
    Helper-->>Formula: ErrValue
  else node is reference
    Helper->>Grid: lookup referenced cell
    Grid-->>Helper: cell value
    alt cell contains error
      Helper-->>Formula: ErrValue
    else cell is normal value
      Helper-->>Formula: undefined
    end
  else no error
    Helper-->>Formula: undefined
  end
  Formula->>Formula: decide fallback return
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • wafflebase/wafflebase#104: Refactors typed ErrValue error-code resolution in functions-info.ts and error comparisons, directly connected to this PR's errorValueFromNode helper consolidation.
  • wafflebase/wafflebase#101: Modifies functions-logical.ts error handling for IFNA/IFERROR by changing error detection from literal #N/A! checks to centralized errorValueFromNode-based ErrValue.NA checks.

Poem

A helper arrives with error detection keen,
Sniffing out errors both direct and seen through screens,
IFERROR and IFNA now catch referenced mistakes,
No more silent failures—the grid transparently breaks! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The code changes directly address all objectives from issue #273: IFERROR now catches referenced errors via errorValueFromNode helper, IFNA distinguishes between #DIV/0! and #N/A, and test coverage validates both direct and referenced error handling.
Out of Scope Changes check ✅ Passed All changes are in scope: new errorValueFromNode helper enables shared error detection, functions-info uses it for ERROR.TYPE, functions-logical updates IFERROR/IFNA, and tests validate the fixes. No unrelated modifications detected.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main change: fixing IFERROR/IFNA to catch referenced errors in cells, which is the core objective addressed across multiple file modifications.

✏️ 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.

@ggyuchive
ggyuchive self-requested a review May 21, 2026 14:29
@codecov

codecov Bot commented May 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/sheets/src/formula/functions-helpers.ts 93.75% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@ggyuchive ggyuchive changed the title Catch referenced errors in IFERROR Fix to catch referenced errors in IFERROR/IFNA May 21, 2026

@ggyuchive ggyuchive left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks for your contribution.

@ggyuchive
ggyuchive merged commit ba1296a into wafflebase:main May 21, 2026
4 checks passed
@hackerwins hackerwins mentioned this pull request May 24, 2026
2 tasks
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.

IFERROR() does not catch errors from cell references

2 participants