Skip to content

Commit 5891213

Browse files
serialize errors
1 parent 320f1ed commit 5891213

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/oc-client.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/* globals __CLIENT_VERSION__, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __DISABLE_LEGACY_TEMPLATES__, __EXTERNALS__, __IMPORTS__ */
22
import { decode } from "@rdevis/turbo-stream";
33

4+
function createErrorFromObject(o) {
5+
const e = new Error(o.message || o);
6+
if (o.stack) e.stack = o.stack;
7+
return Object.assign(e, o.originalError, o);
8+
}
9+
410
export function createOc(oc) {
511
// If oc client is already inside the page, we do nothing.
612
if (oc.status) {
@@ -275,10 +281,15 @@ export function createOc(oc) {
275281
.then((apiResponse) => {
276282
if (!options.action) {
277283
let response = apiResponse[0].response;
278-
let err = response.error ? response.details || response.error : null;
284+
let err = response.error
285+
? createErrorFromObject(response.details || response.error)
286+
: null;
279287
cb(err, response.data, apiResponse[0]);
280288
} else {
281-
cb(null, apiResponse.data);
289+
let err = apiResponse.error
290+
? createErrorFromObject(apiResponse.details || apiResponse.error)
291+
: null;
292+
cb(err, apiResponse.data);
282293
}
283294
})
284295
.catch(cb);

0 commit comments

Comments
 (0)