@hyperfrontend/immutable-api-utils/built-in-copy/errorerror
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
| Name | Type | Description |
|---|---|---|
§errors | Iterable<unknown> | An iterable of errors to aggregate. |
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
AggregateErrorA 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')
}(Safe copy) Creates a new Error using the captured Error constructor. Use this instead of
new Error().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
ErrorA new Error instance.
Example
Creating Error instances
const error = createError('Operation failed')
// With cause for error chaining
const wrapped = createError('Request failed', { cause: originalError })(Safe copy) Creates a new EvalError using the captured EvalError constructor. Use this instead of
new EvalError().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
EvalErrorA new EvalError instance.
Example
Creating EvalError instance
throw createEvalError('Dynamic code execution is not allowed')(Safe copy) Creates a new RangeError using the captured RangeError constructor. Use this instead of
new RangeError().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
RangeErrorA new RangeError instance.
Example
Creating RangeError instance
if (index < 0 || index >= array.length) {
throw createRangeError(`Index ${index} out of bounds`)
}(Safe copy) Creates a new ReferenceError using the captured ReferenceError constructor. Use this instead of
new ReferenceError().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
ReferenceErrorA new ReferenceError instance.
Example
Creating ReferenceError instance
if (!(key in registry)) {
throw createReferenceError(`Unknown key: ${key}`)
}(Safe copy) Creates a new SyntaxError using the captured SyntaxError constructor. Use this instead of
new SyntaxError().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
SyntaxErrorA new SyntaxError instance.
Example
Creating SyntaxError instance
if (!isValidJson(input)) {
throw createSyntaxError('Invalid JSON format')
}(Safe copy) Creates a new TypeError using the captured TypeError constructor. Use this instead of
new TypeError().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
TypeErrorA new TypeError instance.
Example
Creating TypeError instance
if (typeof value !== 'string') {
throw createTypeError('Expected a string')
}(Safe copy) Creates a new URIError using the captured URIError constructor. Use this instead of
new URIError().Parameters
| Name | Type | Description |
|---|---|---|
§message? | string | Optional error message. |
§options? | ErrorOptions | Optional error options. |
Returns
URIErrorA new URIError instance.
Example
Creating URIError instance
if (!isValidUtf8(encoded)) {
throw createURIError('Malformed URI sequence')
}● Variables
(Safe copy) Namespace object containing Error factory functions. Note: Importing this imports all methods in this namespace (no tree-shaking).