Skip to main content

getErrorMessage() function

Extracts an error message string from an unknown error value.

Signature​

export declare function getErrorMessage(error: unknown): string;

Parameters​

Parameter

Type

Description

error

unknown

The error value to convert.

Returns​

string

The error message string.

Remarks​

This utility handles error values of any type, extracting the message property from Error instances or converting other types to strings. Useful for consistent error reporting when the error type is unknown.

TypeScript's catch clause types errors as unknown for safety, since JavaScript allows throwing any value (not just Error instances). This function provides a type-safe way to extract a message string:

  • For Error instances: Returns the message property.
  • For other types: Converts to string using String(), which handles primitives, objects with toString(), null, and undefined gracefully.

Common usage pattern:

try {
// code that might throw
} catch (error) {
const message = getErrorMessage(error);
log.error(message);
}

Generated via tsdoc2docusaurus 1.3.2 by API Extractor/Documenter 7.57.2.