fast-r2
Version:
A Node.js module to simplify Cloudflare R2 interactions, providing an instance-based API for uploads, deletions, and URL generation.
36 lines (32 loc) • 1.39 kB
JavaScript
// src/utils/errorHandling.js
/**
* Custom error class for Cloudflare R2 operations.
* Extends the native Error object to provide more context.
*/
class R2Error extends Error {
/**
* Creates an instance of R2Error.
* @param {string} message - A descriptive error message.
* @param {Error} [originalError] - The original error that caused this R2Error (e.g., from AWS SDK).
*/
constructor(message, originalError) {
super(message);
this.name = 'R2Error'; // Set the name of the error for easier identification
this.originalError = originalError; // Store the original error for debugging
// Capture the stack trace, excluding the constructor call from the stack
if (Error.captureStackTrace) {
Error.captureStackTrace(this, R2Error);
} else {
// Fallback for environments that don't support captureStackTrace
this.stack = new Error(message).stack;
}
// If an original error is provided, append its message and stack for more detail
if (originalError) {
this.message = `${message} Original error: ${originalError.message}`;
if (originalError.stack) {
this.stack += `\n--- Original Stack ---\n${originalError.stack}`;
}
}
}
}
module.exports = R2Error;