All files / src/errors EngineError.ts

100% Statements 17/17
83.33% Branches 5/6
100% Functions 2/2
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                            50x 1x 1x   49x 49x 49x 49x 49x 49x 49x     50x   50x 50x             2x 1x   1x 2x                
import { EngineErrorCode, IEngineError, I18nKey } from "../types.js";
 
/**
 * V8 エンジンの Error コンストラクタ定義。
 */
interface V8ErrorConstructor {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  captureStackTrace?: (target: object, ctor?: any) => void;
}
 
/**
 * 2026 Zenith Tier: プロジェクト全体の統一例外クラス。
 */
export class EngineError extends Error implements IEngineError {
  public readonly code: EngineErrorCode;
  public readonly engineId?: string | undefined;
  public readonly originalError?: unknown | undefined;
  public readonly remediation?: string | undefined;
  public readonly i18nKey?: I18nKey | undefined;
  public readonly i18nParams?: Record<string, string | number> | undefined;
 
  constructor(params: IEngineError | string) {
    if (typeof params === "string") {
      super(params);
      this.code = EngineErrorCode.UNKNOWN_ERROR;
    } else {
      super(params.message);
      this.code = params.code;
      this.engineId = params.engineId;
      this.originalError = params.originalError;
      this.remediation = params.remediation;
      this.i18nKey = params.i18nKey;
      this.i18nParams = params.i18nParams;
    }
 
    this.name = "EngineError";
 
    const v8Error = Error as unknown as V8ErrorConstructor;
    v8Error.captureStackTrace?.(this, EngineError);
  }
 
  /**
   * 任意の例外を EngineError に変換します。
   */
  static from(err: unknown, engineId?: string): EngineError {
    if (err instanceof EngineError) {
      return err;
    }
    const message = err instanceof Error ? err.message : String(err);
    return new EngineError({
      code: EngineErrorCode.UNKNOWN_ERROR,
      message,
      engineId,
      originalError: err,
    });
  }
}