Updated on 2025-08-15 GMT+08:00

Customizing API Error Codes

Define error codes for custom APIs to help developers quickly locate faults, reduce the debugging and troubleshooting time, and improve user experience. Huawei Cloud Astro Zero supports multi-language internationalization configuration for error code information so that error code information can be displayed in different languages.

In addition to setting error codes in the application development designer, you can also set error codes in the environment configuration. For details, see Managing Error Codes in Applications. The difference between the two is that the error codes configured in the application development designer can be packaged and released together with the error codes. For example, if an application is packaged and released to the running environment, error codes are also packaged to the running environment and do not need to be created again in the running environment. Error codes set in the environment configuration cannot be released with applications.

Customizing Error Codes

  1. Log in to the application designer by referring to Logging In to the Application Designer.
  2. In the navigation pane, choose Logic and choose More > Custom Error Codes.

    Figure 1 Customizing error codes

  3. On the Custom tab page, click Create.

    Alternatively, click Import to import an available error code.

  4. Set error code parameters and click Save.

    Figure 2 Customizing an error code
    Table 1 Parameters for customizing an error code

    Parameter

    Description

    Name

    Name of a customized error code. The naming requirements are as follows:

    • The value cannot exceed 64 characters, including the prefix namespace.

      To prevent duplicate data names among different tenants in Huawei Cloud Astro Zero, each tenant must define a unique namespace when first creating an application. A tenant can create only one namespace. After being created, the namespace cannot be modified.

    • Start with a letter and consist of only letters, digits, underscores (_), hyphens (-), and periods (.).

    Category

    Category to which an error code belongs.

    Value: 1–64 characters.

    Http Code

    Select a code from the drop-down list, for example, 200 or 201.

    Language

    Select a language. Click Add to add more language versions for the error code. If the language to be added does not exist, click next to Language. In the displayed dialog box, click Translation Setting to add a language. For details, see Setting the Supported Languages.

    Format

    Description of an error code. {Number} indicates a variable name. For example, {0} indicates the first output variable, {1} indicates the second output variable, and {N} indicates the output variable N+1.

Using a Custom Error Code

After a custom error code is created, it can be used in a script. Call the error function I18nError('ErrorCodeName','Variable 1','Variable 2') that throws the error in the script. The input parameters of this function are the error code Namespace__testErrorCode and variables ("val1" and "val2") in the error message.

  1. Create a script.

    1. Create an empty script by referring to Creating a Blank Script.
    2. In the script editor, enter the following code:
      // test custom error func , get diff language error by user lang
      import * as error from 'error';
      export class TestDemo {
          @action.method({ label: 'test', description: 'error code', input: 'No input', output: 'No output' })
          public test() {
              // Use the I18nError function to set the error code and variables carried in the error message.
               throw new error.I18nError('Namespace__testErrorCode', ["val1", "val2"]);
          }
      }

      Namespace__testErrorCode indicates the custom error code name. Set it based on the site requirements.

    3. Click , save the script, and click to activate the script.

  2. Bind the developed script according to the operations in Customizing APIs and perform the test.

    Figure 3 Configuring an error message test