error-handler.ts 1.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import { HttpError, RuntimeError } from '@ibiz-template/core';
import { DefectModelError, UnsupportedModelError } from '@ibiz-template/model';
import { UnauthorizedHandler } from '@/guard';

/**
 * 事件处理工具
 *
 * @author lxm
 * @date 2022-09-21 18:09:31
 * @export
 * @class ErrorHandler
 */
export class ErrorHandler {
  /**
   * 处理异常
   *
   * @author lxm
   * @date 2022-09-21 18:09:00
   * @static
   * @param {Error} err
   */
  static handlerError(err: Error) {
    if (
      err instanceof DefectModelError ||
      err instanceof UnsupportedModelError
    ) {
27 28 29 30 31
      if (ibiz.env.dev) {
        ibiz.message.error(err.message, 10, true);
      } else {
        ibiz.log.error(err.message);
      }
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    } else if (err instanceof HttpError) {
      if (err.status === 401) {
        UnauthorizedHandler.handle();
      } else {
        ibiz.notification.error({
          title: err.status.toString(),
          desc: err.message,
          duration: 10,
        });
      }
    } else if (err instanceof RuntimeError) {
      ibiz.message.error(err.message, 10);
    }
    ibiz.log.error(err);
  }
}