@hyperfrontend/immutable-api-utils/built-in-copy/error

error

Locked, prototype-pollution-resistant copies of the global Error constructor and its subclasses.

Error, TypeError, RangeError, ReferenceError, SyntaxError, URIError, EvalError, and AggregateError constructors are wrapped in create*Error factories at module-load time and frozen into a tamper-proof namespace, so error construction keeps producing genuine instances even if the global error constructors are later patched. Effective only when imported before any untrusted code has had a chance to mutate the prototype chain.

API Reference§

ƒ Functions

§function

createAggregateError(errors: Iterable<unknown>, message?: string, options?: ErrorOptions): AggregateError

(Safe copy) Creates a new AggregateError using the captured AggregateError constructor. Use this instead of new AggregateError().
Parameters
NameTypeDescription
§errors
Iterable<unknown>An iterable of errors to aggregate.
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
AggregateError
A new AggregateError instance.
Example

Creating AggregateError instance

const results = await Promise.allSettled(promises)
const failures = results.filter(r => r.status === 'rejected').map(r => r.reason)
if (failures.length > 0) {
  throw createAggregateError(failures, 'Multiple operations failed')
}
§function

createError(message?: string, options?: ErrorOptions): Error

(Safe copy) Creates a new Error using the captured Error constructor. Use this instead of new Error().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
Error
A new Error instance.
Example

Creating Error instances

const error = createError('Operation failed')
// With cause for error chaining
const wrapped = createError('Request failed', { cause: originalError })
§function

createEvalError(message?: string, options?: ErrorOptions): EvalError

(Safe copy) Creates a new EvalError using the captured EvalError constructor. Use this instead of new EvalError().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
EvalError
A new EvalError instance.
Example

Creating EvalError instance

throw createEvalError('Dynamic code execution is not allowed')
§function

createRangeError(message?: string, options?: ErrorOptions): RangeError

(Safe copy) Creates a new RangeError using the captured RangeError constructor. Use this instead of new RangeError().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
RangeError
A new RangeError instance.
Example

Creating RangeError instance

if (index < 0 || index >= array.length) {
  throw createRangeError(`Index ${index} out of bounds`)
}
§function

createReferenceError(message?: string, options?: ErrorOptions): ReferenceError

(Safe copy) Creates a new ReferenceError using the captured ReferenceError constructor. Use this instead of new ReferenceError().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
ReferenceError
A new ReferenceError instance.
Example

Creating ReferenceError instance

if (!(key in registry)) {
  throw createReferenceError(`Unknown key: ${key}`)
}
§function

createSyntaxError(message?: string, options?: ErrorOptions): SyntaxError

(Safe copy) Creates a new SyntaxError using the captured SyntaxError constructor. Use this instead of new SyntaxError().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
SyntaxError
A new SyntaxError instance.
Example

Creating SyntaxError instance

if (!isValidJson(input)) {
  throw createSyntaxError('Invalid JSON format')
}
§function

createTypeError(message?: string, options?: ErrorOptions): TypeError

(Safe copy) Creates a new TypeError using the captured TypeError constructor. Use this instead of new TypeError().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
TypeError
A new TypeError instance.
Example

Creating TypeError instance

if (typeof value !== 'string') {
  throw createTypeError('Expected a string')
}
§function

createURIError(message?: string, options?: ErrorOptions): URIError

(Safe copy) Creates a new URIError using the captured URIError constructor. Use this instead of new URIError().
Parameters
NameTypeDescription
§message?
stringOptional error message.
§options?
ErrorOptionsOptional error options.
Returns
URIError
A new URIError instance.
Example

Creating URIError instance

if (!isValidUtf8(encoded)) {
  throw createURIError('Malformed URI sequence')
}

● Variables

§type

Error

(Safe copy) Namespace object containing Error factory functions. Note: Importing this imports all methods in this namespace (no tree-shaking).