Files
kulakpos_web/public/restaurant/Modules/RestaurantDelivery/app/Exceptions/DeliveryException.php

115 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Exceptions;
use Exception;
class DeliveryException extends Exception
{
protected string $errorCode;
public function __construct(
string $message,
string $errorCode = 'DELIVERY_ERROR',
int $code = 400,
?\Throwable $previous = null
) {
$this->errorCode = $errorCode;
parent::__construct($message, $code, $previous);
}
/**
* Get error code.
*/
public function getErrorCode(): string
{
return $this->errorCode;
}
/**
* Render the exception as JSON response.
*/
public function render(): \Illuminate\Http\JsonResponse
{
return response()->json([
'success' => false,
'message' => $this->getMessage(),
'error_code' => $this->errorCode,
], $this->getCode());
}
/**
* Create exception for invalid status transition.
*/
public static function invalidStatusTransition(string $from, string $to): self
{
return new self(
"Cannot transition from '{$from}' to '{$to}'",
'INVALID_STATUS_TRANSITION',
422
);
}
/**
* Create exception for delivery not found.
*/
public static function notFound(string $identifier): self
{
return new self(
"Delivery '{$identifier}' not found",
'DELIVERY_NOT_FOUND',
404
);
}
/**
* Create exception for delivery not trackable.
*/
public static function notTrackable(): self
{
return new self(
'Tracking is not available for this delivery',
'DELIVERY_NOT_TRACKABLE',
400
);
}
/**
* Create exception for delivery already completed.
*/
public static function alreadyCompleted(): self
{
return new self(
'This delivery has already been completed',
'DELIVERY_ALREADY_COMPLETED',
400
);
}
/**
* Create exception for delivery already cancelled.
*/
public static function alreadyCancelled(): self
{
return new self(
'This delivery has already been cancelled',
'DELIVERY_ALREADY_CANCELLED',
400
);
}
/**
* Create exception for no rider assigned.
*/
public static function noRiderAssigned(): self
{
return new self(
'No rider has been assigned to this delivery',
'NO_RIDER_ASSIGNED',
400
);
}
}