Skip to content

Commit 6e5f730

Browse files
fix: return 500 errors in function invoke instead of throwing
- Modified functions.invoke() to catch errors and return them in { data: null, error } format - Updated all function tests to expect the new { data, error } response format - Added specific test case for 500 errors to verify they are returned instead of thrown - Resolves issue where AxiosError was thrown instead of being returned in error field Co-authored-by: Netanel Gilad <netanelgilad@users.noreply.github.com>
1 parent b147975 commit 6e5f730

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

src/modules/functions.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ export function createFunctionsModule(axios: AxiosInstance, appId: string) {
4141
contentType = "application/json";
4242
}
4343

44-
return axios.post(
45-
`/apps/${appId}/functions/${functionName}`,
46-
formData || data,
47-
{ headers: { "Content-Type": contentType } }
48-
);
44+
try {
45+
const response = await axios.post(
46+
`/apps/${appId}/functions/${functionName}`,
47+
formData || data,
48+
{ headers: { "Content-Type": contentType } }
49+
);
50+
return { data: response.data, error: null };
51+
} catch (error) {
52+
// Return error in the expected format instead of throwing
53+
return { data: null, error: error };
54+
}
4955
},
5056
};
5157
}

tests/unit/functions.test.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe("Functions Module", () => {
5454
const result = await base44.functions.invoke(functionName, functionData);
5555

5656
// Verify the response
57+
expect(result.error).toBeNull();
5758
expect(result.data.success).toBe(true);
5859
expect(result.data.messageId).toBe("msg-456");
5960

@@ -77,6 +78,7 @@ describe("Functions Module", () => {
7778
const result = await base44.functions.invoke(functionName, {});
7879

7980
// Verify the response
81+
expect(result.error).toBeNull();
8082
expect(result.data.status).toBe("healthy");
8183

8284
// Verify all mocks were called
@@ -115,6 +117,7 @@ describe("Functions Module", () => {
115117
const result = await base44.functions.invoke(functionName, functionData);
116118

117119
// Verify the response
120+
expect(result.error).toBeNull();
118121
expect(result.data.processed).toBe(true);
119122

120123
// Verify all mocks were called
@@ -150,6 +153,7 @@ describe("Functions Module", () => {
150153
const result = await base44.functions.invoke(functionName, functionData);
151154

152155
// Verify the response
156+
expect(result.error).toBeNull();
153157
expect(result.data.fileId).toBe("file-789");
154158
expect(result.data.filename).toBe("test.txt");
155159

@@ -187,6 +191,7 @@ describe("Functions Module", () => {
187191
const result = await base44.functions.invoke(functionName, functionData);
188192

189193
// Verify the response
194+
expect(result.error).toBeNull();
190195
expect(result.data.documentId).toBe("doc-123");
191196
expect(result.data.processed).toBe(true);
192197

@@ -215,6 +220,7 @@ describe("Functions Module", () => {
215220
const result = await base44.functions.invoke(functionName, formData);
216221

217222
// Verify the response
223+
expect(result.error).toBeNull();
218224
expect(result.data.formId).toBe("form-456");
219225
expect(result.data.submitted).toBe(true);
220226

@@ -252,6 +258,7 @@ describe("Functions Module", () => {
252258
const result = await base44.functions.invoke(functionName, functionData);
253259

254260
// Verify the response
261+
expect(result.error).toBeNull();
255262
expect(result.data.processed).toBe(true);
256263

257264
// Verify all mocks were called
@@ -273,10 +280,11 @@ describe("Functions Module", () => {
273280
code: "INTERNAL_ERROR",
274281
});
275282

276-
// Call the function and expect it to throw
277-
await expect(
278-
base44.functions.invoke(functionName, functionData)
279-
).rejects.toThrow();
283+
// Call the function and expect error to be returned
284+
const result = await base44.functions.invoke(functionName, functionData);
285+
286+
expect(result.data).toBeNull();
287+
expect(result.error).toBeDefined();
280288

281289
// Verify all mocks were called
282290
expect(scope.isDone()).toBe(true);
@@ -297,10 +305,11 @@ describe("Functions Module", () => {
297305
code: "FUNCTION_NOT_FOUND",
298306
});
299307

300-
// Call the function and expect it to throw
301-
await expect(
302-
base44.functions.invoke(functionName, functionData)
303-
).rejects.toThrow();
308+
// Call the function and expect error to be returned
309+
const result = await base44.functions.invoke(functionName, functionData);
310+
311+
expect(result.data).toBeNull();
312+
expect(result.error).toBeDefined();
304313

305314
// Verify all mocks were called
306315
expect(scope.isDone()).toBe(true);
@@ -328,6 +337,7 @@ describe("Functions Module", () => {
328337
const result = await base44.functions.invoke(functionName, functionData);
329338

330339
// Verify the response
340+
expect(result.error).toBeNull();
331341
expect(result.data.received).toBe(true);
332342

333343
// Verify all mocks were called
@@ -355,6 +365,7 @@ describe("Functions Module", () => {
355365
const result = await base44.functions.invoke(functionName, functionData);
356366

357367
// Verify the response
368+
expect(result.error).toBeNull();
358369
expect(result.data.processed).toBe(true);
359370
expect(result.data.count).toBe(3);
360371

@@ -381,6 +392,7 @@ describe("Functions Module", () => {
381392
const result = await base44.functions.invoke(functionName, functionData);
382393

383394
// Verify the response
395+
expect(result.error).toBeNull();
384396
expect(result.data.success).toBe(true);
385397

386398
// Verify all mocks were called
@@ -403,9 +415,36 @@ describe("Functions Module", () => {
403415
const result = await base44.functions.invoke(functionName, formData);
404416

405417
// Verify the response
418+
expect(result.error).toBeNull();
406419
expect(result.data.success).toBe(true);
407420

408421
// Verify all mocks were called
409422
expect(scope.isDone()).toBe(true);
410423
});
424+
425+
test("should handle 500 errors and return error in error field", async () => {
426+
const functionName = "serverErrorFunction";
427+
const functionData = {
428+
param: "value",
429+
};
430+
431+
// Mock the API 500 response
432+
scope
433+
.post(`/api/apps/${appId}/functions/${functionName}`, functionData)
434+
.matchHeader("Content-Type", "application/json")
435+
.reply(500, {
436+
error: "Internal server error",
437+
code: "INTERNAL_ERROR",
438+
details: "Something went wrong on the server",
439+
});
440+
441+
// Call the function and expect error to be returned
442+
const result = await base44.functions.invoke(functionName, functionData);
443+
444+
expect(result.data).toBeNull();
445+
expect(result.error).toBeDefined();
446+
447+
// Verify all mocks were called
448+
expect(scope.isDone()).toBe(true);
449+
});
411450
});

0 commit comments

Comments
 (0)