All files / src/errors di-error.mts

100% Statements 38/38
87.5% Branches 14/16
100% Functions 16/16
100% Lines 37/37

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173      20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x             70x 70x     70x 70x 70x         4x               7x               1x               2x                     4x 2x         2x       5x 5x                       7x             5x               4x                       4x                     1x                       5x                   16x                       2x              
import type { InjectionTokenSchemaType } from '../token/injection-token.mjs'
import type { FactoryRecord } from '../token/registry.mjs'
 
export enum DIErrorCode {
  FactoryNotFound = 'FactoryNotFound',
  FactoryTokenNotResolved = 'FactoryTokenNotResolved',
  InstanceNotFound = 'InstanceNotFound',
  InstanceDestroying = 'InstanceDestroying',
  CircularDependency = 'CircularDependency',
  TokenValidationError = 'TokenValidationError',
  TokenSchemaRequiredError = 'TokenSchemaRequiredError',
  ClassNotInjectable = 'ClassNotInjectable',
  ScopeMismatchError = 'ScopeMismatchError',
  PriorityConflictError = 'PriorityConflictError',
  StorageError = 'StorageError',
  InitializationError = 'InitializationError',
  DependencyResolutionError = 'DependencyResolutionError',
  UnknownError = 'UnknownError',
}
 
export class DIError extends Error {
  public readonly context?: Record<string, unknown>
 
  constructor(
    public readonly code: DIErrorCode,
    public readonly message: string,
    context?: Record<string, unknown>,
  ) {
    super(message)
    this.context = context
    this.name = 'DIError'
  }
 
  // Static factory methods for common error types
  static factoryNotFound(name: string): DIError {
    return new DIError(
      DIErrorCode.FactoryNotFound,
      `Factory ${name} not found`,
      { name },
    )
  }
 
  static factoryTokenNotResolved(token: string | symbol | unknown): DIError {
    return new DIError(
      DIErrorCode.FactoryTokenNotResolved,
      `Factory token not resolved: ${token?.toString() ?? 'unknown'}`,
      { token },
    )
  }
 
  static instanceNotFound(name: string): DIError {
    return new DIError(
      DIErrorCode.InstanceNotFound,
      `Instance ${name} not found`,
      { name },
    )
  }
 
  static instanceDestroying(name: string): DIError {
    return new DIError(
      DIErrorCode.InstanceDestroying,
      `Instance ${name} destroying`,
      { name },
    )
  }
 
  static unknown(
    message: string | Error,
    context?: Record<string, unknown>,
  ): DIError {
    if (message instanceof Error) {
      return new DIError(DIErrorCode.UnknownError, message.message, {
        ...context,
        parent: message,
      })
    }
    return new DIError(DIErrorCode.UnknownError, message, context)
  }
 
  static circularDependency(cycle: string[]): DIError {
    const cycleStr = cycle.join(' -> ')
    return new DIError(
      DIErrorCode.CircularDependency,
      `Circular dependency detected: ${cycleStr}`,
      { cycle },
    )
  }
 
  static tokenValidationError(
    message: string,
    schema: InjectionTokenSchemaType | undefined,
    value: unknown,
  ): DIError {
    return new DIError(DIErrorCode.TokenValidationError, message, {
      schema,
      value,
    })
  }
 
  static tokenSchemaRequiredError(token: string | symbol | unknown): DIError {
    return new DIError(
      DIErrorCode.TokenSchemaRequiredError,
      `Token ${token?.toString() ?? 'unknown'} requires schema arguments and cannot be used with addInstance. Use BoundInjectionToken or provide arguments when resolving.`,
      { token },
    )
  }
 
  static classNotInjectable(className: string): DIError {
    return new DIError(
      DIErrorCode.ClassNotInjectable,
      `Class ${className} is not decorated with @Injectable.`,
      { className },
    )
  }
 
  static scopeMismatchError(
    token: string | symbol | unknown,
    expectedScope: string,
    actualScope: string,
  ): DIError {
    return new DIError(
      DIErrorCode.ScopeMismatchError,
      `Scope mismatch for ${token?.toString() ?? 'unknown'}: expected ${expectedScope}, got ${actualScope}`,
      { token, expectedScope, actualScope },
    )
  }
 
  static priorityConflictError(
    token: string | symbol | unknown,
    records: FactoryRecord[],
  ): DIError {
    return new DIError(
      DIErrorCode.PriorityConflictError,
      `Priority conflict for ${token?.toString() ?? 'unknown'}: multiple bindings with same priority`,
      { token, records },
    )
  }
 
  static storageError(
    message: string,
    operation: string,
    instanceName?: string,
  ): DIError {
    return new DIError(DIErrorCode.StorageError, `Storage error: ${message}`, {
      operation,
      instanceName,
    })
  }
 
  static initializationError(
    serviceName: string,
    error: Error | string,
  ): DIError {
    return new DIError(
      DIErrorCode.InitializationError,
      `Service ${serviceName} initialization failed: ${error instanceof Error ? error.message : error}`,
      { serviceName, error },
    )
  }
 
  static dependencyResolutionError(
    serviceName: string,
    dependencyName: string,
    error: Error | string,
  ): DIError {
    return new DIError(
      DIErrorCode.DependencyResolutionError,
      `Failed to resolve dependency ${dependencyName} for service ${serviceName}: ${error instanceof Error ? error.message : error}`,
      { serviceName, dependencyName, error },
    )
  }
}