# prefer-aggregate-error

Rule `unicorn/prefer-aggregate-error`. Prefer `AggregateError` when throwing collected errors.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #967 by reach, #1222 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-aggregate-error` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-aggregate-error.md) |

## Code examples

### Incorrect: the rule reports this

```ts
const errors: Error[] = [new Error('Email is required.')];

if (errors.length > 0) {
  throw new Error('Validation failed.');
}
```

```ts
const validationErrors: TypeError[] = [new TypeError('Invalid email.')];

if (validationErrors.length !== 0) {
  throw new Error('Validation failed.', {cause});
}
```

### Correct: the rule accepts this

```ts
const errors: Error[] = [new Error('Email is required.')];

if (errors.length > 0) {
  throw new AggregateError(errors, 'Validation failed.');
}
```

```ts
const validationErrors: TypeError[] = [new TypeError('Invalid email.')];

if (validationErrors.length !== 0) {
  throw new AggregateError(validationErrors, 'Validation failed.', {cause});
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-aggregate-error": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-add-event-listener-options](https://lintradar.dev/rules/unicorn/prefer-add-event-listener-options.md): Prefer an options object over a boolean in `.addEventListener()`.
- [unicorn/prefer-array-from-map](https://lintradar.dev/rules/unicorn/prefer-array-from-map.md): Prefer using the `Array.from()` mapping function argument.
- [unicorn/no-using-resource-escape](https://lintradar.dev/rules/unicorn/no-using-resource-escape.md): Disallow returning or exporting resources declared with `using`, including through capturing functions.
- [unicorn/prefer-array-from-range](https://lintradar.dev/rules/unicorn/prefer-array-from-range.md): Prefer `Array.from({length}, …)` when creating range arrays.
- [unicorn/no-useless-re-export](https://lintradar.dev/rules/unicorn/no-useless-re-export.md): Disallow redundant re-exports.
- [unicorn/prefer-array-last-methods](https://lintradar.dev/rules/unicorn/prefer-array-last-methods.md): Prefer last-oriented array methods over `Array#reverse()` or `Array#toReversed()` followed by a method.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-aggregate-error. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
