Skip to content

unacy / ConversionError

Class: ConversionError

Defined in: packages/core/src/errors.ts:151

Error thrown when a conversion cannot be performed.

Remarks

ConversionError is the most common error consumers encounter. It is thrown when no direct edge and no BFS-discoverable path exists between two units, or when allow() is called for a pair with no reachable path.

Inspect e.from and e.to to determine which units are missing a path, then register the required converter with registry.register(from, to, fn).

Example

typescript
try {
  registry.convert(temp, 'Celsius').to('Miles'); // no path
} catch (e) {
  if (e instanceof ConversionError) {
    console.error(`No path from ${String(e.from)} to ${String(e.to)}`);
  }
}

Pitfalls

NEVER call registry.convert(value, fromUnit).to(toUnit) without handling ConversionError — the path may not exist even if both units are registered.

NEVER confuse a missing converter with a wrong-direction converter — if A → B is registered but B → A is not, converting B to A throws ConversionError, not a type error.

Extends

Constructors

Constructor

ts
new ConversionError(
   from, 
   to, 
   reason?): ConversionError;

Defined in: packages/core/src/errors.ts:155

Parameters

ParameterType
fromPropertyKey
toPropertyKey
reason?string

Returns

ConversionError

Overrides

UnacyError.constructor

Properties

PropertyModifierTypeInherited fromDefined in
cause?publicunknownUnacyError.causenode_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es2022.error.d.ts:24
fromreadonlyPropertyKey-packages/core/src/errors.ts:152
messagepublicstringUnacyError.messagenode_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1075
namepublicstringUnacyError.namenode_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1074
stack?publicstringUnacyError.stacknode_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1076
toreadonlyPropertyKey-packages/core/src/errors.ts:153

Released under the MIT License.