-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbortRouteException.php
48 lines (42 loc) · 1.07 KB
/
AbortRouteException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace SnooPHP\Http;
use Throwable;
use Exception;
/**
* An exception thrown when a route is aborted
*
* @author Sneppy
*/
class AbortRouteException extends Exception
{
/**
* @var bool isJson true if message is a json string
*/
protected $isJson = false;
/**
* Builds on top of default constructor
*
* @param object|string|array $message abort message. Objects and arrays are returned as json objects
* @param int $code must be a valid http status code
* @param Exception|null $previous previous exception
*/
public function __construct($message = "server error", $code = 500, Throwable $previous = null)
{
if (is_object($message) || is_array($message))
{
$this->isJson = true;
$message = to_json($message);
}
// Parent constructor
parent::__construct($message, $code, $previous);
}
/**
* Create response from exception
*
* @return Response
*/
public function response()
{
return new Response($this->getMessage(), $this->getCode(), $this->isJson ? ["Content-Type" => "application/json; charset=utf-8"] : []);
}
}