apache / apache/pouchdb

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

Open
#9,067 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
17.6k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

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](https://github.com/ds300/patch-package/issues/296).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.