Skip to content

@technobuddha > library > Object > Conversion

Function: toError()

ts
function toError(entity: unknown): Error;

Defined in: to-error.ts:17

Convert the entity to an Error object.

Parameters

ParameterTypeDescription
entityunknownThe entity to convert, if it is already an error ir will be returned otherwise a new Error object will be created.

Returns

Error

The entity as an Error object.

Example

typescript
toError(new Error('fail')); // returns the same Error object
toError('fail'); // returns Error: fail
toError(404); // returns Error: 404