Skip to content

Commit b3b3472

Browse files
Merge pull request #143 from opencomponents/fix-handling-errors
Fix handling errors
2 parents 418d280 + c861a7b commit b3b3472

File tree

3 files changed

+73
-6
lines changed

3 files changed

+73
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oc-client-browser",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "OC browser client",
55
"main": "index.js",
66
"types": "index.d.ts",

src/oc-client.js

Lines changed: 13 additions & 3 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) {
@@ -35,7 +41,6 @@ export function createOc(oc) {
3541
let logError = (msg) => console.log(msg);
3642
let logInfo = (msg) => ocConf.debug && console.log(msg);
3743
let handleFetchResponse = (response) => {
38-
if (!response.ok) throw response;
3944
if (response.headers.get("Content-Type") !== "x-text/stream")
4045
return response.json();
4146

@@ -276,10 +281,15 @@ export function createOc(oc) {
276281
.then((apiResponse) => {
277282
if (!options.action) {
278283
let response = apiResponse[0].response;
279-
let err = response.error ? response.details || response.error : null;
284+
let err = response.error
285+
? createErrorFromObject(response.details || response.error)
286+
: null;
280287
cb(err, response.data, apiResponse[0]);
281288
} else {
282-
cb(null, apiResponse.data);
289+
let err = apiResponse.error
290+
? createErrorFromObject(apiResponse.details || apiResponse.error)
291+
: null;
292+
cb(err, apiResponse.data);
283293
}
284294
})
285295
.catch(cb);

tests/getData.spec.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ test.describe("oc-client : getData", () => {
200200
});
201201

202202
// Verify error was passed to callback
203-
expect(errorResult.callbackError).toEqual("details about oups");
203+
// If callbackError is an Error object, compare its message
204+
const errorMsg =
205+
errorResult.callbackError instanceof Error
206+
? errorResult.callbackError.message
207+
: errorResult.callbackError;
208+
expect(errorMsg).toEqual("details about oups");
204209
});
205210

206211
test("should call the callback with server.js error if no details are available", async ({
@@ -250,7 +255,11 @@ test.describe("oc-client : getData", () => {
250255
});
251256

252257
// Verify error was passed to callback
253-
expect(errorResult.callbackError).toEqual("oups");
258+
const errorMsg =
259+
errorResult.callbackError instanceof Error
260+
? errorResult.callbackError.message
261+
: errorResult.callbackError;
262+
expect(errorMsg).toEqual("oups");
254263
});
255264

256265
test("should handle JSON requests correctly", async ({ page }) => {
@@ -555,4 +564,52 @@ test.describe("oc-client : getData", () => {
555564
// Verify error was passed to callback
556565
expect(errorResult.callbackError).toEqual("Network error");
557566
});
567+
568+
test("should reject with an Error object containing API error data when network returns 500", async ({
569+
page,
570+
}) => {
571+
const result = await page.evaluate(async () => {
572+
// Save original fetch
573+
const originalFetch = window.fetch;
574+
// Mock fetch to simulate a 500 error with error object in response
575+
window.fetch = () => {
576+
return Promise.resolve({
577+
headers: { get: () => null },
578+
json: () =>
579+
Promise.resolve({
580+
error: "API error",
581+
details: {
582+
foo: "bar",
583+
message: "Something went wrong",
584+
stack: "stacktrace",
585+
},
586+
}),
587+
});
588+
};
589+
try {
590+
await window.oc.getAction({
591+
component: "test-component",
592+
baseUrl: "http://api",
593+
version: "1.0.0",
594+
action: "do",
595+
});
596+
return { success: true };
597+
} catch (err) {
598+
// Restore original fetch
599+
window.fetch = originalFetch;
600+
return {
601+
success: false,
602+
isError: err instanceof Error,
603+
message: err.message,
604+
stack: err.stack,
605+
foo: err.foo,
606+
};
607+
}
608+
});
609+
expect(result.success).toBe(false);
610+
expect(result.isError).toBe(true);
611+
expect(result.message).toBe("Something went wrong");
612+
expect(result.stack).toBe("stacktrace");
613+
expect(result.foo).toBe("bar");
614+
});
558615
});

0 commit comments

Comments
 (0)