| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Exceptions; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
|
| 9 |
/** |
| 10 |
* Base Yatra Exception |
| 11 |
* |
| 12 |
* Provides structured error handling with error codes and context |
| 13 |
*/ |
| 14 |
class YatraException extends Exception |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var array Additional context data |
| 18 |
*/ |
| 19 |
protected array $context = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string Error code for API responses |
| 23 |
*/ |
| 24 |
protected string $errorCode = 'yatra_error'; |
| 25 |
|
| 26 |
public function __construct(string $message = '', int $code = 0, ?Exception $previous = null, array $context = []) |
| 27 |
{ |
| 28 |
parent::__construct($message, $code, $previous); |
| 29 |
$this->context = $context; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get error context |
| 34 |
*/ |
| 35 |
public function getContext(): array |
| 36 |
{ |
| 37 |
return $this->context; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get API error code |
| 42 |
*/ |
| 43 |
public function getErrorCode(): string |
| 44 |
{ |
| 45 |
return $this->errorCode; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set error context |
| 50 |
*/ |
| 51 |
public function setContext(array $context): self |
| 52 |
{ |
| 53 |
$this->context = $context; |
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add context data |
| 59 |
*/ |
| 60 |
public function addContext(string $key, $value): self |
| 61 |
{ |
| 62 |
$this->context[$key] = $value; |
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Convert to array for API responses |
| 68 |
*/ |
| 69 |
public function toArray(): array |
| 70 |
{ |
| 71 |
return [ |
| 72 |
'error_code' => $this->getErrorCode(), |
| 73 |
'message' => $this->getMessage(), |
| 74 |
'context' => $this->getContext(), |
| 75 |
'file' => $this->getFile(), |
| 76 |
'line' => $this->getLine(), |
| 77 |
]; |
| 78 |
} |
| 79 |
} |
| 80 |
|