#9067·pouchdb

Fix error handling in pouchdb-errors to prevent crashes when Error.stack is inaccessible

Author: nicotroiaCreated Feb 17, 2025Updated Mar 5, 2026

Fix error handling in pouchdb-errors to prevent crashes when Error.stack is inaccessible

Here is the diff that solved my problem:

diff
diff --git a/node_modules/pouchdb-errors/lib/index.js b/node_modules/pouchdb-errors/lib/index.js
index f271755..ccc388a 100644
--- a/node_modules/pouchdb-errors/lib/index.js
+++ b/node_modules/pouchdb-errors/lib/index.js
@@ -57,15 +57,25 @@ function createError(error, reason) {
       }
     }
 
-    if (this.stack === undefined) {
-      this.stack = (new Error()).stack;
+    // Safely handle the stack property
+    try {
+      if (this.stack === undefined) {
+        this.stack = (new Error()).stack;
+      }
+    } catch (e) {
+      // Fallback for environments where stack assignment fails
+      this.stack = 'No stack trace available';
     }
 
     if (reason !== undefined) {
       this.reason = reason;
     }
   }
-  CustomPouchError.prototype = PouchError.prototype;
+
+  // Ensure proper inheritance from PouchError
+  CustomPouchError.prototype = Object.create(PouchError.prototype);
+  CustomPouchError.prototype.constructor = CustomPouchError;
+
   return new CustomPouchError(reason);
 }
 

This issue body was partially generated by patch-package.