fix(zapier): harden null-body handling and error messages

From an independent review pass:
- Guard search / get / add against a null or empty 2xx body (previously an
  uncaught TypeError on .results / .status); return [] or an empty object.
- Stringify non-string API error detail so a thrown message never renders as
  "[object Object]".
- Correct the Add sample to the default no-wait shape {event_id, status}.
- Add a regression unit test for the null-body path.

Verified: build + zapier validate + 16 tests (13 unit, 3 live E2E), plus a
live check of the v3-add to /v1/event poll cross-version path.
This commit is contained in:
Himanshu-Sangshetti
2026-07-23 11:49:25 +05:30
parent 9b1e2ef604
commit 1bd9910382
5 changed files with 28 additions and 12 deletions
@@ -77,7 +77,9 @@ const perform = async (z: ZObject, bundle: Bundle): Promise<AddResponse | EventR
body,
});
const data = response.data as AddResponse;
// Default to an empty object so an empty/no-content 2xx body can't crash the
// `.status` / `.event_id` reads below.
const data = (response.data ?? {}) as AddResponse;
// Add returns {event_id, status:PENDING|RUNNING}; poll only when opted in.
if (wait && data.event_id && data.status !== 'SUCCEEDED' && data.status !== 'FAILED') {
@@ -160,6 +162,7 @@ export default {
'longer than Zapier allows this step to run, and a timeout does not mean the add failed.',
},
],
sample: { status: 'SUCCEEDED', event_id: '00000000-0000-0000-0000-000000000000', results: [] },
// Default (no-wait) returns the accepted event; the wait path returns the resolved event.
sample: { event_id: '00000000-0000-0000-0000-000000000000', status: 'PENDING' },
},
};
+5 -5
View File
@@ -35,11 +35,11 @@ export const handleBadResponses = (
}
if (response.status >= 400) {
const data = (response.data as Record<string, unknown>) || {};
const detail = (data.detail ||
data.error ||
data.message ||
response.content ||
'unknown error') as string;
const raw =
data.detail || data.error || data.message || response.content || 'unknown error';
// DRF sometimes returns detail as an object/array; stringify so the
// thrown message never renders as "[object Object]".
const detail = typeof raw === 'string' ? raw : JSON.stringify(raw);
throw new z.errors.Error(
`Mem0 API request failed (HTTP ${response.status}): ${detail}`,
'Mem0ApiError',
@@ -16,8 +16,9 @@ const perform = async (z: ZObject, bundle: Bundle): Promise<Memory[]> => {
},
body,
});
const data = response.data as Memory[] | { results?: Memory[] };
return Array.isArray(data) ? data : data.results || [];
// Guard against a null/empty body; always return an array.
const data = response.data as Memory[] | { results?: Memory[] } | null;
return Array.isArray(data) ? data : data?.results ?? [];
};
export default {
@@ -16,9 +16,9 @@ const perform = async (z: ZObject, bundle: Bundle): Promise<Memory[]> => {
method: 'POST',
body,
});
// Searches must return an array.
const data = response.data as Memory[] | { results?: Memory[] };
return Array.isArray(data) ? data : data.results || [];
// Searches must return an array; guard against a null/empty body.
const data = response.data as Memory[] | { results?: Memory[] } | null;
return Array.isArray(data) ? data : data?.results ?? [];
};
export default {
@@ -124,6 +124,18 @@ describe('search / get array-shape enforcement (offline)', () => {
} as any);
expect(z.requests[0].params).toEqual({ page: 2, page_size: 10 });
});
it('search and get_memories return [] on a null/empty body (no crash)', async () => {
const zSearch = makeZ([{ data: null }]);
const found = await searchMemories.operation.perform(zSearch, {
inputData: { query: 'x', user_id: 'u1' },
} as any);
expect(found).toEqual([]);
const zGet = makeZ([{ data: null }]);
const all = await getMemories.operation.perform(zGet, { inputData: { user_id: 'u1' } } as any);
expect(all).toEqual([]);
});
});
describe('delete_memory (offline)', () => {