Skip to content

Commit

Permalink
return validation exception (422) instead of lunar exception (500)
Browse files Browse the repository at this point in the history
  • Loading branch information
repl6669 committed Feb 4, 2025
1 parent 3efbf7d commit c4b14ca
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use Dystore\Api\Domain\CartLines\JsonApi\V1\CartLineRequest;
use Dystore\Api\Domain\Carts\Actions\AddToCart;
use Illuminate\Support\Facades\App;
use Illuminate\Validation\ValidationException;
use LaravelJsonApi\Core\Responses\DataResponse;
use LaravelJsonApi\Laravel\Http\Controllers\Actions\Destroy;
use LaravelJsonApi\Laravel\Http\Controllers\Actions\Store;
use LaravelJsonApi\Laravel\Http\Controllers\Actions\Update;
use Lunar\Exceptions\Carts\CartException;
use Lunar\Models\Contracts\CartLine as CartLineContract;

class CartLinesController extends Controller implements CartLinesControllerContract
Expand All @@ -29,7 +31,13 @@ public function creating(CartLineRequest $request, CartLineQuery $query): DataRe
{
$data = CartLineData::fromRequest($request);

[, $cartLine] = App::make(AddToCart::class)($data);
try {
[, $cartLine] = App::make(AddToCart::class)($data);
} catch (CartException $e) {
$messages = $e->errors();

throw ValidationException::withMessages($e->errors()->getMessages());
}

return DataResponse::make($cartLine)
->withQueryParameters($query)
Expand All @@ -43,7 +51,13 @@ public function updating(CartLineContract $cartLine, CartLineRequest $request, C
{
$data = CartLineData::fromRequest($request);

[, $cartLine] = App::make(UpdateCartLine::class)($data, $cartLine);
try {
[, $cartLine] = App::make(UpdateCartLine::class)($data, $cartLine);
} catch (CartException $e) {
$messages = $e->errors();

throw ValidationException::withMessages($e->errors()->getMessages());
}

return DataResponse::make($cartLine)
->withQueryParameters($query)
Expand Down

0 comments on commit c4b14ca

Please sign in to comment.