Skip to content

Error Exception

Modules can individually provide their own Error exceptions

Define Error

It takes two steps to define Error. Taking the module a-demo as an example:

1. Define Error enum

src/suite/a-demo/modules/a-demo/src/config/errors.ts

typescript
export enum Errors {
  'ErrorTest' = 1001,
}
  • Convention: Error Code > 1000

2. Define Error language resources

English: src/suite/a-demo/modules/a-demo/src/config/locale/en-us.ts

typescript
export default {
  ErrorTest: 'This is a error test',
};

Chinese: src/suite/a-demo/modules/a-demo/src/config/locale/zh-cn.ts

typescript
export default {
  ErrorTest: '这是一个错误测试',
};

Use Error

You can directly throw the module's Error exception through the Scope instance

typescript
export class TestA {
  protected async __init__() {
    this.scope.error.ErrorTest.throw();
  }
}
  • Gif Demonstration scope-error

Use Error cross-module

typescript
import type { ScopeModuleADemo } from 'zova-module-a-demo';

export class TestA {
  @UseScope('a-demo')
  scopeModuleADemo: ScopeModuleADemo;

  protected async __init__() {
    this.scopeModuleADemo.error.ErrorTest.throw();
  }
}

Released under the MIT License.