Files

104 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Exceptions;
use Exception;
class AssignmentException extends Exception
{
protected string $errorCode;
public function __construct(
string $message,
string $errorCode = 'ASSIGNMENT_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 no riders available.
*/
public static function noRidersAvailable(): self
{
return new self(
'No riders are available in the area',
'NO_RIDERS_AVAILABLE',
400
);
}
/**
* Create exception for assignment timeout.
*/
public static function timeout(): self
{
return new self(
'Assignment request timed out',
'ASSIGNMENT_TIMEOUT',
408
);
}
/**
* Create exception for max reassignments reached.
*/
public static function maxReassignmentsReached(): self
{
return new self(
'Maximum reassignment attempts reached',
'MAX_REASSIGNMENTS_REACHED',
400
);
}
/**
* Create exception for rider already assigned.
*/
public static function alreadyAssigned(): self
{
return new self(
'A rider has already been assigned to this delivery',
'RIDER_ALREADY_ASSIGNED',
400
);
}
/**
* Create exception for assignment rejected.
*/
public static function rejected(string $reason = ''): self
{
$message = 'Assignment was rejected';
if ($reason) {
$message .= ": {$reason}";
}
return new self($message, 'ASSIGNMENT_REJECTED', 400);
}
}