Skip to content

Commit 23336a8

Browse files
committed
Implement toJSON for ArangoError/HttpError
Fixes #632.
1 parent f4bed07 commit 23336a8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ This driver uses semantic versioning:
1616

1717
## [Unreleased]
1818

19+
### Added
20+
21+
- Implemented `toJSON` methods for `ArangoError` and `HttpError`
22+
23+
This prevents an error where `JSON.stringify` would reliably throw if passed
24+
an instance of either of these error types generated by arangojs. Note that
25+
you may still want to implement your own JSON representation logic as system
26+
errors (e.g. `ECONNREFUSED`) are not wrapped by arangojs and thrown as-is.
27+
1928
### Fixed
2029

2130
- Stack traces are now improved for most errors when using `precaptureStackTraces`

src/error.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ export class ArangoError extends ExtendableError {
155155
get isArangoError(): true {
156156
return true;
157157
}
158+
159+
toJSON() {
160+
return {
161+
error: true,
162+
errorMessage: this.message,
163+
errorNum: this.errorNum,
164+
code: this.code,
165+
};
166+
}
158167
}
159168

160169
/**
@@ -186,4 +195,11 @@ export class HttpError extends ExtendableError {
186195
if (err[key]) this[key] = err[key]!;
187196
}
188197
}
198+
199+
toJSON() {
200+
return {
201+
error: true,
202+
code: this.code,
203+
};
204+
}
189205
}

0 commit comments

Comments
 (0)