| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData\Exception; |
| 6: | |
| 7: | use LaravelUi5\OData\Http\ODataResponse; |
| 8: | use Illuminate\Contracts\Support\Responsable; |
| 9: | use Illuminate\Support\Facades\App; |
| 10: | use RuntimeException; |
| 11: | use Throwable; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | abstract class ProtocolException extends RuntimeException implements Responsable |
| 17: | { |
| 18: | protected $httpCode = ODataResponse::HTTP_INTERNAL_SERVER_ERROR; |
| 19: | protected $odataCode; |
| 20: | protected $message; |
| 21: | protected $target = null; |
| 22: | protected $details = []; |
| 23: | protected $innerError = []; |
| 24: | protected $headers = []; |
| 25: | protected $suppressContent = false; |
| 26: | protected $originalException = null; |
| 27: | |
| 28: | public function __construct(?string $code = null, ?string $message = null, ?Throwable $originalException = null) |
| 29: | { |
| 30: | if ($code) { |
| 31: | $this->odataCode = $code; |
| 32: | } |
| 33: | |
| 34: | if ($message) { |
| 35: | $this->message = $message; |
| 36: | } |
| 37: | |
| 38: | if ($originalException) { |
| 39: | $this->originalException = $originalException; |
| 40: | } |
| 41: | |
| 42: | parent::__construct($this->message); |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | public function code(string $code): self |
| 51: | { |
| 52: | $this->odataCode = $code; |
| 53: | return $this; |
| 54: | } |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public function message(string $message): self |
| 62: | { |
| 63: | $this->message = $message; |
| 64: | return $this; |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function target(string $target): self |
| 73: | { |
| 74: | $this->target = $target; |
| 75: | return $this; |
| 76: | } |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | public function addDetail(string $code, string $message, ?string $target = null): self |
| 84: | { |
| 85: | $detail = [ |
| 86: | 'code' => $code, |
| 87: | 'message' => $message, |
| 88: | ]; |
| 89: | |
| 90: | if ($target) { |
| 91: | $detail['target'] = $target; |
| 92: | } |
| 93: | |
| 94: | $this->details[] = $detail; |
| 95: | |
| 96: | return $this; |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | public function addInnerError(string $key, string $value): self |
| 106: | { |
| 107: | $this->innerError[$key] = $value; |
| 108: | |
| 109: | return $this; |
| 110: | } |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public function header(string $key, string $value): self |
| 119: | { |
| 120: | $this->headers[$key] = $value; |
| 121: | return $this; |
| 122: | } |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function serialize() |
| 129: | { |
| 130: | return array_filter([ |
| 131: | 'httpCode' => $this->httpCode, |
| 132: | 'odataCode' => $this->odataCode, |
| 133: | 'message' => $this->message, |
| 134: | 'target' => $this->target, |
| 135: | 'details' => $this->details, |
| 136: | 'innererror' => $this->innerError, |
| 137: | 'headers' => $this->headers, |
| 138: | ]); |
| 139: | } |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | public function toError() |
| 146: | { |
| 147: | return [ |
| 148: | 'code' => $this->odataCode, |
| 149: | 'message' => $this->message, |
| 150: | 'target' => $this->target, |
| 151: | 'details' => $this->details, |
| 152: | 'innererror' => $this->innerError ?: (object) [], |
| 153: | ]; |
| 154: | } |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | public function getOriginalException(): ?Throwable |
| 162: | { |
| 163: | return $this->originalException; |
| 164: | } |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | public function toResponse($request = null): ODataResponse |
| 172: | { |
| 173: | $response = App::make(ODataResponse::class); |
| 174: | |
| 175: | $response->setCallback(function () { |
| 176: | if ($this->suppressContent) { |
| 177: | return; |
| 178: | } |
| 179: | |
| 180: | echo json_encode(['error' => $this->toError()], JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); |
| 181: | }); |
| 182: | |
| 183: | $response->setProtocolVersion('1.1'); |
| 184: | $response->setStatusCode($this->httpCode); |
| 185: | $response->headers->replace($this->headers); |
| 186: | $response->headers->set('content-type', 'application/json'); |
| 187: | |
| 188: | return $response; |
| 189: | } |
| 190: | |
| 191: | public function getInnerException(): ?Throwable |
| 192: | { |
| 193: | return $this->originalException; |
| 194: | } |
| 195: | } |
| 196: | |