Skip to content

fix: alerts list --limit/--offset reject invalid input instead of silently misbehaving (#578) - #589

Open
ygd58 wants to merge 1 commit into
nansen-ai:mainfrom
ygd58:fix/alerts-list-limit-offset
Open

fix: alerts list --limit/--offset reject invalid input instead of silently misbehaving (#578)#589
ygd58 wants to merge 1 commit into
nansen-ai:mainfrom
ygd58:fix/alerts-list-limit-offset

Conversation

@ygd58

@ygd58 ygd58 commented Sep 5, 2026

Copy link
Copy Markdown

Fixes #578.

Problem

nansen alerts list --limit/--offset silently misbehaved instead of erroring on bad input, which is dangerous for a command whose whole audience is scripts and agents:

  • if (options.offset) / if (options.limit) treat 0 as falsy, so --limit 0 and --offset 0 were silently ignored (treated as "not set") instead of honored.
  • Number("abc") is NaN, and Array.prototype.slice coerces a NaN argument to 0 — so --offset abc silently became a no-op and --limit abc silently returned zero results, with no error telling the caller their input wasn't a number.
  • A negative --offset (e.g. -1) was accepted and fed straight into slice(), which treats negative indices as "from the end" — silently returning the wrong records instead of rejecting the input.

Fix

--limit and --offset are now validated as non-negative integers (checked with Number.isInteger, not just presence), matching the strict client-side validation convention already used elsewhere in the CLI (e.g. parseSlippageBps in bridge.js, buildPagination in query-options.js). Invalid input now throws a NansenError with ErrorCode.INVALID_PARAMS and a message naming the bad value, consistent with how this same handler already rejects --enabled --disabled together.

0 is now correctly distinguished from "unset" via !== undefined checks instead of truthiness.

Testing

npm test
Test Files  1 failed | 65 passed (66)
     Tests  5 failed | 2682 passed | 16 skipped (2703)

The 5 failures are pre-existing, in src/__tests__/doctor.test.js, and reproduce identically on main without this change — they rely on chmod-based unreadable-file simulation, which doesn't apply when tests run as root. Unrelated to this fix. 7 new tests added for this fix, all passing.

npm run lint

Clean, no output.

Manual verification:

--limit 0   -> [] (previously returned everything)
--offset 0  -> full list (previously already worked, now explicit)
--limit abc -> throws "--limit must be a non-negative integer, got \"abc\"" (previously silently returned [])
--offset -1 -> throws "--offset must be a non-negative integer, got \"-1\"" (previously silently returned the last record)

Checklist

  • npm test passes (output above; unrelated pre-existing failures noted)
  • npm run lint passes
  • New code paths have tests (7 added, covering 0, non-numeric, negative, and non-integer values for both flags)
  • No console.log in core
  • Error messages are actionable (name the flag and the bad value)
  • Changeset added (patch — bug fix, corrects previously-silent wrong behavior)
  • src/schema.json — no changes needed (no new commands/options, just stricter validation of existing ones)

@nansen-pr-reviewer

Copy link
Copy Markdown

pr-reviewer Summary for #0cc4c18

No issues found

The code review completed successfully with no findings.

Review effort: 1/5 (Trivial)

Summary

This is a clean, focused bug fix that correctly addresses all three silent-failure modes described in the PR. The implementation is correct and consistent with the project's validation conventions.

The fix is sound:

  • !== undefined correctly distinguishes 0 from "unset" — the core falsy-check bug is resolved.
  • Number.isInteger(Number(options.offset)) catches NaN (non-numeric strings), negative values, and floats in a single guard, exactly matching patterns like buildPagination elsewhere in the codebase.
  • NansenError with ErrorCode.INVALID_PARAMS and an actionable message naming the bad value is the correct error type for this handler.

Tests are thorough: 7 new test cases cover 0, non-numeric strings, negative values, and floats for both flags. The one combination not explicitly tested (offset: '2.5') is exercised by the same code path as offset: 'abc' and limit: '2.5', so coverage is sufficient.

Changeset: correctly categorized as patch, targets the right package (nansen-cli), and the description is accurate.

No issues found. Ready to merge.


Token usage: 3,418 input, 3,118 output, 299,469 cache read, 29,694 cache write | Usage Guide

New pushes are reviewed automatically with a 10-minute cooldown between reviews. To request a review at any time, comment @nansen-pr-reviewer re-review.

@nansen-pr-reviewer nansen-pr-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Auto-approved

This PR was automatically approved because:

  • Claude recommends approval
  • Claude assessed this as a minimal effort change
  • The effort level is within the auto-approval threshold of 2
  • No high or critical issues were detected

If you have any concerns, please request a manual review.

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.

alerts list --limit/--offset and numeric range flags silently produce wrong results on invalid input

1 participant