Skip to content

Inconsistent Error Handling in Encryption/Decryption Functions #123

@pranavkonde

Description

@pranavkonde

Inconsistent Error Handling in Encryption/Decryption Functions

Problem

The decryptFile function returns undefined on error instead of throwing an exception, causing silent failures.

Current Code

// ❌ BAD: Returns undefined on error
const decryptFile = async (cipher: any, password: any) => {
  try {
    // ... decryption logic
    return decryptedContent
  } catch (error) {
    console.error('Error decrypting file')
    console.error(error)
    return  // Returns undefined!
  }
}

Impact

  • Silent failures in production
  • Difficult debugging
  • Runtime errors when calling code expects data but gets undefined
  • No distinction between wrong password vs corrupted data

Solution

1. Create Error Classes

// src/errors/EncryptionErrors.ts
export class DecryptionError extends Error { }
export class InvalidPasswordError extends DecryptionError { }
export class CorruptedDataError extends DecryptionError { }

2. Fix decryptFile Function

const decryptFile = async (cipher: any, password: any) => {
  try {
    // ... decryption logic
    return decryptedContent
  } catch (error) {
    if (error.name === 'OperationError') {
      throw new InvalidPasswordError('Invalid password');
    }
    throw new DecryptionError('Decryption failed', error);
  }
}

3. Update Calling Code

// Before
const data = await decryptFile(cipher, password);
if (!data) { /* handle error */ }

// After
try {
  const data = await decryptFile(cipher, password);
} catch (error) {
  if (error instanceof InvalidPasswordError) {
    // Handle wrong password
  }
  // Handle other errors
}

Affected Files

  • src/Lighthouse/uploadEncrypted/encryptionNode.ts
  • src/Lighthouse/uploadEncrypted/encryptionBrowser.ts
  • All files calling decryptFile

Breaking Change

This changes decryptFile from returning undefined to throwing errors. All calling code must be updated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions