# How the Archiver Module Handles Errors During Compression in Node.js

> Discover how the Archiver module handles errors during compression in Node.js. Learn about warning and error events, distinguishing fatal failures from non-fatal issues for robust application handling.

- Repository: [Ahmed Ibrahim/Website-downloader](https://github.com/AhmadIbrahiim/Website-downloader)
- Tags: how-to-guide
- Published: 2026-07-08

---

**The archiver module implements robust error handling by listening to `warning` and `error` events on the archiver instance, distinguishing between non-fatal issues like missing files and fatal compression failures that terminate the process.**

The [`archiver/index.js`](https://github.com/AhmadIbrahiim/Website-downloader/blob/main/archiver/index.js) file in the `AhmadIbrahiim/Website-downloader` repository provides a critical wrapper around the Node.js `archiver` package for creating ZIP archives of downloaded websites. Understanding how this module handles errors during compression is essential for building reliable file archiving pipelines that don't fail silently. The implementation follows official archiver documentation best practices by explicitly capturing stream events to ensure proper error propagation to Socket.io clients or upstream try/catch blocks.

## Event-Based Error Handling Strategy

The module implements a two-tier error handling approach that separates recoverable warnings from fatal errors. This strategy prevents the Node.js process from crashing on minor issues while ensuring critical failures bubble up immediately.

### Handling Non-Fatal Warnings (ENOENT)

The `warning` event listener (lines 29-36) captures non-fatal issues that don't stop the archive from being created, such as missing source files. The code inspects the error code and applies conditional logic:

- **ENOENT errors**: Logged to console but ignored, allowing the archive creation to continue despite missing entries
- **Other warnings**: Re-thrown as exceptions to prevent silent data loss

```javascript
archive.on('warning', (err) => {
  if (err.code === 'ENOENT') {
    console.warn('Archiver warning:', err);
  } else {
    throw err;
  }
});

```

### Handling Fatal Errors

The `error` event listener (lines 38-41) catches fatal problems that prevent archive generation, such as stream errors, permission issues, or disk space exhaustion. Unlike warnings, these errors are immediately re-thrown without inspection, ensuring the calling context receives the exception for proper handling.

```javascript
archive.on('error', (err) => {
  throw err;
});

```

## Implementation Details in archiver/index.js

According to the source code in `AhmadIbrahiim/Website-downloader`, the error handling logic resides in the core wrapper function that configures the archiver instance. The module attaches listeners before piping data to the output stream, ensuring all error conditions are captured.

The warning handler specifically checks for `err.code === 'ENOENT'` to identify missing file entries, which is a common occurrence when downloading websites with broken links. All other warning codes are treated as exceptional conditions. This selective approach balances resilience against missing assets with strictness for unexpected stream behaviors.

In addition to error events, the module attaches listeners to the output stream (`output.on('close')` and `output.on('end')`) to report completion status, though these serve reporting purposes rather than error handling.

## Practical Code Examples

### Example 1: Wrapping Archive Creation in Try/Catch

When calling the archiver wrapper, surround the invocation with try/catch to handle propagated errors:

```javascript
const createArchive = require('./archiver');

try {
  createArchive('example-site', io, { token: 'user-123' });
} catch (err) {
  console.error('Archive creation failed:', err);
  // Notify client via Socket.io
  io.emit('user-123', { 
    progress: 'Failed', 
    error: err.message 
  });
}

```

### Example 2: Extending the Warning Handler

To modify the default behavior and log all warnings without throwing:

```javascript
const archiver = require('archiver');
const archive = archiver('zip');

archive.on('warning', (err) => {
  if (err.code === 'ENOENT') {
    console.warn('Missing file during archiving:', err);
  } else {
    // Log but don't throw for non-ENOENT warnings
    console.warn('Archiver warning:', err);
  }
});

```

### Example 3: Async/Await Integration

For Promise-based flows, wrap the archiver in a Promise that resolves on stream close:

```javascript
const fs = require('fs');
const path = require('path');

async function zipSite(folderName, io, data) {
  try {
    await new Promise((resolve, reject) => {
      const createArchive = require('./archiver');
      
      // Setup output stream manually for Promise control
      const output = fs.createWriteStream(
        path.join('./public/sites', `${folderName}.zip`)
      );
      
      output.on('close', resolve);
      output.on('error', reject);
      
      // Initialize archiver with error handling
      createArchive(folderName, io, data);
    });
    
    console.log('Archive completed successfully');
    io.emit(data.token, { progress: 'Completed' });
    
  } catch (e) {
    console.error('Failed to create archive:', e);
    io.emit(data.token, { 
      progress: 'Failed', 
      error: e.message 
    });
  }
}

```

## Summary

The archiver module in `AhmadIbrahiim/Website-downloader` implements a defensive error handling strategy that:

- **Distinguishes warning severity**: Non-fatal `ENOENT` warnings are logged while other warnings escalate to exceptions
- **Propagates fatal errors immediately**: The `error` event listener re-throws all critical failures to prevent silent corruption
- **Follows official best practices**: Event listeners align with the Node.js Stream API and archiver package recommendations
- **Supports downstream handling**: Thrown errors can be caught by surrounding try/catch blocks or Promise chains for Socket.io notification

## Frequently Asked Questions

### What happens if a file is missing during compression?

The archiver module catches the `warning` event and checks for `ENOENT` (file not found) error codes. If detected, it logs the missing file to the console but continues archiving remaining files. For other warning types, it throws the error immediately to prevent data integrity issues.

### How does the archiver module communicate errors to the client application?

The module propagates errors by throwing them from the event listeners, which can be caught by try/catch blocks or Promise rejections in the calling code. The [`app.js`](https://github.com/AhmadIbrahiim/Website-downloader/blob/main/app.js) entry point or other controllers can then emit error messages via Socket.io using the token provided during the archive creation call.

### Where are completed ZIP files stored in the Website-downloader repository?

Completed archives are streamed to the `./public/sites/` directory, as configured in the output stream setup within [`archiver/index.js`](https://github.com/AhmadIbrahiim/Website-downloader/blob/main/archiver/index.js). The error handling ensures that only valid, complete archives are written, while failed attempts throw errors before corrupt files reach the public directory.

### Can I modify the error handling to ignore all warnings instead of just ENOENT?

Yes, you can modify the `warning` event listener in [`archiver/index.js`](https://github.com/AhmadIbrahiim/Website-downloader/blob/main/archiver/index.js) (lines 29-36) to remove the `throw err` clause for non-ENOENT warnings. However, this is not recommended as it may mask serious issues like permission errors or stream corruption that could result in incomplete ZIP archives.