Skip to content

Commit

Permalink
fix: fix error handling in shopping-cart
Browse files Browse the repository at this point in the history
Добавил проверки на наличие ожидаемой структуры ответа сервера для кейсов ошибок. В каких-то редких
случаях приложение не загружалось при 500-ой ошибке api коризны пользователя.

closes #232
  • Loading branch information
kavabunga committed Jan 11, 2024
1 parent a198d6c commit a4ae0c8
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/contexts/cart-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export const CartProvider: React.FC<CartProviderProps> = ({ children }) => {
})
.catch((error) => {
setError((prev) => {
return { ...prev, loadCartData: error.errors[0].detail };
return {
...prev,
loadCartData: error.errors?.[0]?.detail || 'Ошибка загрузки корзины покупок',
};
});
})
.finally(() => {
Expand All @@ -140,7 +143,10 @@ export const CartProvider: React.FC<CartProviderProps> = ({ children }) => {
})
.catch((error) => {
setError((prev) => {
return { ...prev, updateCart: error.errors[0].detail };
return {
...prev,
updateCart: error.errors?.[0]?.detail || 'Ошибка обновления корзины покупок',
};
});
})
.finally(() => {
Expand All @@ -154,7 +160,11 @@ export const CartProvider: React.FC<CartProviderProps> = ({ children }) => {
.catch((error) => {
if (error?.errors) {
setError((prev) => {
return { ...prev, deleteCart: error.errors[0]?.detail };
return {
...prev,
deleteCart:
error.errors[0]?.detail || 'Ошибка удаления товара из корзины покупок',
};
});
}
})
Expand All @@ -172,9 +182,12 @@ export const CartProvider: React.FC<CartProviderProps> = ({ children }) => {
return { ...prev, clearCart: message };
});
})
.catch(({ errors }) => {
.catch((error) => {
setError((prev) => {
return { ...prev, clearCart: errors };
return {
...prev,
clearCart: error.errors || 'Ошибка очистки корзины покупок',
};
});
});
};
Expand Down

0 comments on commit a4ae0c8

Please sign in to comment.