Service API: Use { data, error } Format, No try/catch
When calling a service API, use { data, error } response format; no try/catch
Source: .agents/rules/no-try-catch-service-api.mdc
Metadata
- alwaysApply: true
Content
Service API: Use { data, error } Format, No try/catch
- When calling a service API (e.g. from
@/services/...), the service returns{ data, error }. - Do not use try/catch. Await the call, then check
if (error). - If
error: show Error toast (showErrorToast(error)), reset any relevant state, set loading false, andreturn. - Otherwise use
dataand set loading false.
// ❌ Avoid: try/catch around service call
try {
const res = await getPhysicalProductPrices({ ... });
setPrices(res?.data ?? null);
} catch (err) {
showErrorToast(getAxiosErrorMsg(err));
} finally {
setLoading(false);
}
// ✅ Prefer: destructure { data, error }, check error, no try/catch
setLoading(true);
const { data, error } = await getPhysicalProductPrices({
physicalProductId: productId,
selectedItems
});
if (error) {
showErrorToast(error);
setPrices(null);
setLoading(false);
return;
}
setPrices(data ?? null);
setLoading(false);