R002: 错误码断言必须是number类型

Detect string numeric literals in error.code assertions within OpenHarmony test code.

31|6|Updated Jan 29, 2026
One-click install
npx skills add https://github.com/openharmonyinsight/openharmony-skills --skill r002-number
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: R002: 错误码断言必须是number类型
Source: https://github.com/openharmonyinsight/openharmony-skills/tree/main/skills/check-test-code-quality/rules/R002
Command: npx skills add https://github.com/openharmonyinsight/openharmony-skills --skill r002-number

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It prevents test failures and fragile logic caused by asserting OpenHarmony error.code with string literals instead of numbers.

Core Features & Use Cases

  • Detect common assertion patterns: flags string-literal usage in expect/assertEqual/assertTrue and if-condition comparisons involving xxx.code.
  • Validate numeric-only error codes: only reports cases where the asserted string is a pure numeric literal (e.g., "401", "14000011").
  • Supports basic variable tracking: identifies string-typed variables/constants/parameters that are later used to compare against error.code, including catch-block alias cases (e.g., err as error alias).

Quick Start

Use this rule to scan your OpenHarmony test files and automatically list every place where error.code is compared or asserted using numeric strings, then convert those assertions to number literals.

Frequently Asked Questions about R002: 错误码断言必须是number类型

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why do my OpenHarmony unit tests fail when asserting error.code with string literals?▼

You should assert error.code using number literals instead of string numeric literals to prevent type-mismatch test failures. Replacing string assertions like "401" with numeric values ensures correct type checking and accurate test validation in OpenHarmony test code.

How do I detect string literal assertions for error codes in ets or ts test files?▼

Scan OpenHarmony ets/ts/js test files to detect type-mismatch assertions where error.code is compared using string numeric literals. The detection matches assertEqual, assertTrue, and if-condition patterns while filtering out comments and non-assert usages, listing flagged numeric strings.

How does error code type checking handle variables assigned in catch blocks?▼

Yes, error code type checking supports basic variable tracking by identifying string-typed variables, constants, and parameters later compared against error.code. It handles catch-block alias cases, ensuring string variables used in assertions are traced and flagged for type mismatches.

Does the error code assertion check flag non-numeric string values or comments?▼

No, the error code assertion check filters out comments, console logging, and non-assert usages. It only flags cases where the asserted string is a pure numeric literal like "401" or "14000011", ensuring non-numeric string values are not reported as type mismatches.

What is the best way to fix type-mismatch assertions for OpenHarmony error codes?▼

The best way to fix type-mismatch assertions for OpenHarmony error codes is to scan test files and convert numeric string literals to number literals. Automatically list every place where error.code is compared using strings, then replace them with numeric values for proper type checking.