You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

export const UNITS_ERRORS = {
UNKNOWN_UNIT: (unit: string): string => `[formats.units] Unknown unit: "${unit}".`,
INCOMPATIBLE_UNITS: (from: string, fromBase: string, to: string, toBase: string): string =>
`[formats.units] Incompatible units: "${from}" (${fromBase}) and "${to}" (${toBase}).`,
DEFAULT_UNIT_NOT_FOUND: (system: string, kind: string): string =>
`[formats.units] Default unit not found for "${system}:${kind}".`
} as const;
export const UNITS_ERROR_NAMES = {
UNKNOWN_UNIT: 'UnitsUnknownUnitError',
INCOMPATIBLE_UNITS: 'UnitsIncompatibleUnitsError',
DEFAULT_UNIT_NOT_FOUND: 'UnitsDefaultUnitNotFoundError'
} as const;
export class UnitsUnknownUnitError extends RangeError {
constructor(readonly unit: string) {
super(UNITS_ERRORS.UNKNOWN_UNIT(unit));
this.name = UNITS_ERROR_NAMES.UNKNOWN_UNIT;
}
}
export class UnitsIncompatibleUnitsError extends RangeError {
constructor(
readonly from: string,
readonly fromBase: string,
readonly to: string,
readonly toBase: string
) {
super(UNITS_ERRORS.INCOMPATIBLE_UNITS(from, fromBase, to, toBase));
this.name = UNITS_ERROR_NAMES.INCOMPATIBLE_UNITS;
}
}
export class UnitsDefaultUnitNotFoundError extends RangeError {
constructor(
readonly system: string,
readonly kind: string
) {
super(UNITS_ERRORS.DEFAULT_UNIT_NOT_FOUND(system, kind));
this.name = UNITS_ERROR_NAMES.DEFAULT_UNIT_NOT_FOUND;
}
}
export function isUnitsUnknownUnitError(error: unknown): error is UnitsUnknownUnitError {
return error instanceof UnitsUnknownUnitError;
}
export function isUnitsIncompatibleUnitsError(
error: unknown
): error is UnitsIncompatibleUnitsError {
return error instanceof UnitsIncompatibleUnitsError;
}
export function isUnitsDefaultUnitNotFoundError(
error: unknown
): error is UnitsDefaultUnitNotFoundError {
return error instanceof UnitsDefaultUnitNotFoundError;
}

Powered by TurnKey Linux.