Error handling and execution reporting utilities.
Provides utilities for error handling, message formatting, and execution timing to support error reporting and performance analysis.
Type Guard Assertion
Validate values against type guards with automatic error messages:
import { assert } from '@metreeca/core/error';import { isString, isNumber } from '@metreeca/core';const name = assert(input, isString); // throws TypeError if not a stringconst count = assert(data, isNumber, "count must be numeric"); Copy
import { assert } from '@metreeca/core/error';import { isString, isNumber } from '@metreeca/core';const name = assert(input, isString); // throws TypeError if not a stringconst count = assert(data, isNumber, "count must be numeric");
Error Throwing in Expressions
Throw errors in expression contexts where statements aren't allowed:
import { error } from '@metreeca/core/error';const value = map.get(key) ?? error("Missing required key");const result = isValid(input) ? processInput(input) : error(new ValidationError("Invalid input")); Copy
import { error } from '@metreeca/core/error';const value = map.get(key) ?? error("Missing required key");const result = isValid(input) ? processInput(input) : error(new ValidationError("Invalid input"));
Message Formatting
Extract readable messages from various value types:
import { message } from '@metreeca/core/error';console.error(`Failed with: ${message(errorValue)}`);// Error objects -> message property// Numbers -> locale-formatted strings// Other values -> string representation Copy
import { message } from '@metreeca/core/error';console.error(`Failed with: ${message(errorValue)}`);// Error objects -> message property// Numbers -> locale-formatted strings// Other values -> string representation
Execution Timing
Monitor timing for synchronous and asynchronous operations:
import { time } from '@metreeca/core/error';const result = await time( async () => fetchData(url), (data, elapsed) => console.log(`Fetched in ${elapsed}ms`));const computed = time( () => expensiveCalculation(), (result, elapsed) => logPerformance('calculation', elapsed)); Copy
import { time } from '@metreeca/core/error';const result = await time( async () => fetchData(url), (data, elapsed) => console.log(`Fetched in ${elapsed}ms`));const computed = time( () => expensiveCalculation(), (result, elapsed) => logPerformance('calculation', elapsed));
Validates a value against a type guard and returns it.
Throws an error in expression contexts.
Extracts a readable message string from an unknown value.
Executes a task (sync or async) and monitors its execution time.
Error handling and execution reporting utilities.
Provides utilities for error handling, message formatting, and execution timing to support error reporting and performance analysis.
Type Guard Assertion
Validate values against type guards with automatic error messages:
Error Throwing in Expressions
Throw errors in expression contexts where statements aren't allowed:
Message Formatting
Extract readable messages from various value types:
Execution Timing
Monitor timing for synchronous and asynchronous operations: