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:
@@ -77,7 +77,9 @@ const perform = async (z: ZObject, bundle: Bundle): Promise<AddResponse | EventR
|
|||||||
body,
|
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.
|
// Add returns {event_id, status:PENDING|RUNNING}; poll only when opted in.
|
||||||
if (wait && data.event_id && data.status !== 'SUCCEEDED' && data.status !== 'FAILED') {
|
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.',
|
'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' },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -35,11 +35,11 @@ export const handleBadResponses = (
|
|||||||
}
|
}
|
||||||
if (response.status >= 400) {
|
if (response.status >= 400) {
|
||||||
const data = (response.data as Record<string, unknown>) || {};
|
const data = (response.data as Record<string, unknown>) || {};
|
||||||
const detail = (data.detail ||
|
const raw =
|
||||||
data.error ||
|
data.detail || data.error || data.message || response.content || 'unknown error';
|
||||||
data.message ||
|
// DRF sometimes returns detail as an object/array; stringify so the
|
||||||
response.content ||
|
// thrown message never renders as "[object Object]".
|
||||||
'unknown error') as string;
|
const detail = typeof raw === 'string' ? raw : JSON.stringify(raw);
|
||||||
throw new z.errors.Error(
|
throw new z.errors.Error(
|
||||||
`Mem0 API request failed (HTTP ${response.status}): ${detail}`,
|
`Mem0 API request failed (HTTP ${response.status}): ${detail}`,
|
||||||
'Mem0ApiError',
|
'Mem0ApiError',
|
||||||
|
|||||||
@@ -16,8 +16,9 @@ const perform = async (z: ZObject, bundle: Bundle): Promise<Memory[]> => {
|
|||||||
},
|
},
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
const data = response.data as Memory[] | { results?: Memory[] };
|
// Guard against a null/empty body; always return an array.
|
||||||
return Array.isArray(data) ? data : data.results || [];
|
const data = response.data as Memory[] | { results?: Memory[] } | null;
|
||||||
|
return Array.isArray(data) ? data : data?.results ?? [];
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ const perform = async (z: ZObject, bundle: Bundle): Promise<Memory[]> => {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body,
|
body,
|
||||||
});
|
});
|
||||||
// Searches must return an array.
|
// Searches must return an array; guard against a null/empty body.
|
||||||
const data = response.data as Memory[] | { results?: Memory[] };
|
const data = response.data as Memory[] | { results?: Memory[] } | null;
|
||||||
return Array.isArray(data) ? data : data.results || [];
|
return Array.isArray(data) ? data : data?.results ?? [];
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
@@ -124,6 +124,18 @@ describe('search / get array-shape enforcement (offline)', () => {
|
|||||||
} as any);
|
} as any);
|
||||||
expect(z.requests[0].params).toEqual({ page: 2, page_size: 10 });
|
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)', () => {
|
describe('delete_memory (offline)', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user