Skip to content

fix(scripts): require explicit separator in extractErrorCode pattern (Fixes #860) - #888

Closed
kleitonbernardino-lab wants to merge 1 commit into
MergeFi:mainfrom
kleitonbernardino-lab:fix/error-code-regex-860
Closed

kleitonbernardino-lab wants to merge 1 commit into
MergeFi:mainfrom
kleitonbernardino-lab:fix/error-code-regex-860

Conversation

@kleitonbernardino-lab

Copy link
Copy Markdown

Summary of Changes

This PR tightens the error code extraction regex in scripts/error-codes.mjs to avoid false-positive error code detection on arbitrary error strings, fixing #860:

  1. Mandatory Separator on code Matcher:
    • Replaced loose regexes /�code\s*[:=]?\s*(\d+)/i, /�error\s*[:=]?\s*(\d+)/i, and /�value\s*[:=]?\s*(\d+)/i with structured patterns requiring explicit delimiters:
      const match = error.match(/error\s*code\s*[:=]\s*(\d+)/i) ||
                    error.match(/�code\s*[:=]\s*(\d+)/i);
  2. Prevent False Positives on Free Text & HTTP Errors:
    • Free text phrases like HTTP error 500: Bad Gateway or unexpected value 42 will no longer falsely match as contract error codes 500 or 42.
    • The function returns null when no structured error code is present, allowing formatError to accurately display "No error code detected" rather than generating a false contract error diagnosis.
  3. Unit Tests (scripts/error-codes.test.mjs):
    • Added unit tests verifying explicit error codes (code: 12, error code = 7) are properly extracted.
    • Added unit tests asserting that HTTP errors (e.g. HTTP error 500) and arbitrary free text (Unexpected value 42 in stream) return null.

Verification

Tests pass cleanly:

$ node --test scripts/error-codes.test.mjs
▶ extractErrorCode pattern tightening (#860)
  ✔ extracts error code when explicit separator is present
  ✔ rejects free-text error strings like 'error 500' without explicit separator
  ✔ rejects arbitrary free-text strings matching 'value N' or 'value: N'
  ✔ extracts error code from Send failed / Tx failed string with embedded JSON
  ✔ extracts error code from structured error objects
  ✔ returns null for empty or non-error values
✔ extractErrorCode pattern tightening (#860)
▶ decodeErrorCode and formatError integration tests
  ✔ decodes known error codes properly
  ✔ formatError handles unstructured HTTP errors without false diagnosis (#860)
  ✔ formatError formats valid error codes with detected meanings
✔ decodeErrorCode and formatError integration tests
ℹ tests 9
ℹ suites 2
ℹ pass 9
ℹ fail 0

Fixes #860.

…ixes MergeFi#860)

Signed-off-by: Kleiton Bernardino <kleiton.bernardino@gmail.com>
@vercel

vercel Bot commented Nov 11, 2026

Copy link
Copy Markdown

@kleitonbernardino-lab is attempting to deploy a commit to the chonilius' projects Team on Vercel.

A member of the Team first needs to authorize it.

@chonilius chonilius closed this Nov 12, 2026
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.

scripts/error-codes.mjs's extractErrorCode matches /\bvalue\s*[:=]?\s*(\d+)/i on free text, so unrelated numbers become error codes

2 participants