Merge pull request #3193 from Haendlerbund/add-context-to-logging-when-adding-product-to-cart

Add logging to exception thrown in cart
This commit is contained in:
Jitendra Singh 2020-06-16 13:54:52 +05:30 committed by GitHub
commit 3b2df1f99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 46 deletions

View File

@ -2,7 +2,10 @@
namespace Webkul\API\Http\Controllers\Shop;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Webkul\Checkout\Repositories\CartRepository;
use Webkul\Checkout\Repositories\CartItemRepository;
use Webkul\API\Http\Resources\Checkout\Cart as CartResource;
@ -42,9 +45,9 @@ class CartController extends Controller
/**
* Controller instance
*
* @param \Webkul\Checkout\Repositories\CartRepository $cartRepository
* @param \Webkul\Checkout\Repositories\CartItemRepository $cartItemRepository
* @param \Webkul\Checkout\Repositories\WishlistRepository $wishlistRepository
* @param \Webkul\Checkout\Repositories\CartRepository $cartRepository
* @param \Webkul\Checkout\Repositories\CartItemRepository $cartItemRepository
* @param \Webkul\Checkout\Repositories\WishlistRepository $wishlistRepository
*/
public function __construct(
CartRepository $cartRepository,
@ -69,7 +72,7 @@ class CartController extends Controller
/**
* Get customer cart
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function get()
{
@ -86,10 +89,11 @@ class CartController extends Controller
/**
* Store a newly created resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
* @param int $id
*
* @return \Illuminate\Http\JsonResponse
*/
public function store($id)
public function store($id): ?JsonResponse
{
if (request()->get('is_buy_now')) {
Event::dispatch('shop.item.buy-now', $id);
@ -97,36 +101,46 @@ class CartController extends Controller
Event::dispatch('checkout.cart.item.add.before', $id);
$result = Cart::addProduct($id, request()->except('_token'));
try {
$result = Cart::addProduct($id, request()->except('_token'));
if (! $result) {
$message = session()->get('warning') ?? session()->get('error');
if (is_array($result) && isset($result['warning'])) {
return response()->json([
'error' => $result['warning'],
], 400);
}
if ($customer = auth($this->guard)->user()) {
$this->wishlistRepository->deleteWhere(['product_id' => $id, 'customer_id' => $customer->id]);
}
Event::dispatch('checkout.cart.item.add.after', $result);
Cart::collectTotals();
$cart = Cart::getCart();
return response()->json([
'error' => session()->get('warning'),
], 400);
'message' => __('shop::app.checkout.cart.item.success'),
'data' => $cart ? new CartResource($cart) : null,
]);
} catch (Exception $e) {
Log::error('API CartController: ' . $e->getMessage(),
['product_id' => $id, 'cart_id' => cart()->getCart() ?? 0]);
return response()->json([
'error' => [
'message' => $e->getMessage(),
'code' => $e->getCode()
]
]);
}
if ($customer = auth($this->guard)->user()) {
$this->wishlistRepository->deleteWhere(['product_id' => $id, 'customer_id' => $customer->id]);
}
Event::dispatch('checkout.cart.item.add.after', $result);
Cart::collectTotals();
$cart = Cart::getCart();
return response()->json([
'message' => __('shop::app.checkout.cart.item.success'),
'data' => $cart ? new CartResource($cart) : null,
]);
}
/**
* Update the specified resource in storage.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function update()
{
@ -161,7 +175,7 @@ class CartController extends Controller
/**
* Remove the specified resource from storage.
*
* @return \Illuminate\Http\Response
* @return \Illuminate\Http\JsonResponse
*/
public function destroy()
{
@ -182,8 +196,9 @@ class CartController extends Controller
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
* @param int $id
*
* @return \Illuminate\Http\JsonResponse
*/
public function destroyItem($id)
{
@ -206,7 +221,9 @@ class CartController extends Controller
/**
* Function to move a already added product to wishlist will run only on customer authentication.
*
* @param \Webkul\Checkout\Repositories\CartItemRepository $id
* @param \Webkul\Checkout\Repositories\CartItemRepository $id
*
* @return \Illuminate\Http\JsonResponse
*/
public function moveToWishlist($id)
{

View File

@ -2,6 +2,8 @@
namespace Webkul\Checkout;
use Exception;
use Illuminate\Support\Facades\Log;
use Webkul\Checkout\Models\Cart as CartModel;
use Webkul\Checkout\Models\CartAddress;
use Webkul\Checkout\Repositories\CartRepository;
@ -122,9 +124,11 @@ class Cart
/**
* Add Items in a cart with some cart and item details.
*
* @param int $productId
* @param array $data
* @return \Webkul\Checkout\Contracts\Cart|\Exception|array
* @param int $productId
* @param array $data
*
* @return \Webkul\Checkout\Contracts\Cart|string|array
* @throws Exception
*/
public function addProduct($productId, $data)
{
@ -147,7 +151,7 @@ class Cart
session()->forget('cart');
}
throw new \Exception($cartProducts);
throw new Exception($cartProducts);
} else {
$parentCartItem = null;
@ -161,7 +165,7 @@ class Cart
if (! $cartItem) {
$cartItem = $this->cartItemRepository->create(array_merge($cartProduct, ['cart_id' => $cart->id]));
} else {
if (isset($cartProduct['parent_id']) && $cartItem->parent_id != $parentCartItem->id) {
if (isset($cartProduct['parent_id']) && $cartItem->parent_id !== $parentCartItem->id) {
$cartItem = $this->cartItemRepository->create(array_merge($cartProduct, [
'cart_id' => $cart->id
]));
@ -218,7 +222,7 @@ class Cart
$cart = $this->cartRepository->create($cartData);
if (! $cart) {
session()->flash('error', trans('shop::app.checkout.cart.create-error'));
session()->flash('error', __('shop::app.checkout.cart.create-error'));
return;
}
@ -232,7 +236,8 @@ class Cart
* Update cart items information
*
* @param array $data
* @return bool|void|\Exception
*
* @return bool|void|Exception
*/
public function updateItems($data)
{
@ -246,13 +251,13 @@ class Cart
if ($quantity <= 0) {
$this->removeItem($itemId);
throw new \Exception(trans('shop::app.checkout.cart.quantity.illegal'));
throw new Exception(__('shop::app.checkout.cart.quantity.illegal'));
}
$item->quantity = $quantity;
if (! $this->isItemHaveQuantity($item)) {
throw new \Exception(trans('shop::app.checkout.cart.quantity.inventory_warning'));
throw new Exception(__('shop::app.checkout.cart.quantity.inventory_warning'));
}
Event::dispatch('checkout.cart.update.before', $item);
@ -499,7 +504,7 @@ class Cart
$this->saveAddressesWhenRequested($data, $billingAddressData, $shippingAddressData);
$this->linkAddresses($cart, $billingAddressData, $shippingAddressData);
$this->assignCustomerFields($cart);
$cart->save();

View File

@ -2,6 +2,7 @@
namespace Webkul\Shop\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Webkul\Customer\Repositories\WishlistRepository;
use Webkul\Product\Repositories\ProductRepository;
use Webkul\Checkout\Contracts\Cart as CartModel;
@ -75,7 +76,7 @@ class CartController extends Controller
}
if ($result instanceof CartModel) {
session()->flash('success', trans('shop::app.checkout.cart.item.success'));
session()->flash('success', __('shop::app.checkout.cart.item.success'));
if ($customer = auth()->guard('customer')->user()) {
$this->wishlistRepository->deleteWhere(['product_id' => $id, 'customer_id' => $customer->id]);
@ -88,10 +89,13 @@ class CartController extends Controller
}
}
} catch(\Exception $e) {
session()->flash('error', trans($e->getMessage()));
session()->flash('error', __($e->getMessage()));
$product = $this->productRepository->find($id);
Log::error('Shop CartController: ' . $e->getMessage(),
['product_id' => $id, 'cart_id' => cart()->getCart() ?? 0]);
return redirect()->route('shop.productOrCategory.index', $product->url_key);
}

View File

@ -3,6 +3,7 @@
namespace Webkul\Velocity\Http\Controllers\Shop;
use Cart;
use Illuminate\Support\Facades\Log;
use Webkul\Velocity\Helpers\Helper;
use Webkul\Checkout\Contracts\Cart as CartModel;
use Webkul\Product\Repositories\ProductRepository;
@ -93,16 +94,19 @@ class CartController extends Controller
} catch(\Exception $exception) {
$product = $this->productRepository->find($id);
Log::error('Velocity CartController: ' . $exception->getMessage(),
['product_id' => $id, 'cart_id' => cart()->getCart() ?? 0]);
$response = [
'status' => 'danger',
'message' => trans($exception->getMessage()),
'message' => __($exception->getMessage()),
'redirectionRoute' => route('shop.productOrCategory.index', $product->url_key),
];
}
return $response ?? [
'status' => 'danger',
'message' => trans('velocity::app.error.something_went_wrong'),
'message' => __('velocity::app.error.something_went_wrong'),
];
}