12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Exceptions;
- use Exception;
- use Illuminate\Http\Response;
- /**
- * 用作逻辑错误返回.
- * Class ResponseException
- * @package App\Exceptions
- * Auth Zhong
- * Date 2018/10/31
- */
- class ResponseException extends Exception
- {
- /**
- * @var Response
- */
- protected $response;
- public function __construct($response, $data = [], $code = 400)
- {
- if ($response instanceof Response) {
- $this->response=$response;
- } else {
- $responseData['message']=$response;
- $responseData['errors']=$data;
- if (request()->isXmlHttpRequest()) {
- $this->response=response()->json($responseData)->setStatusCode($code);
- } else {
- $this->response=response()->view('errors.'.$code, $responseData, $code);
- }
- }
- }
- /**
- * Render the exception into an HTTP response.
- *
- * @return \Illuminate\Http\Response
- */
- public function render()
- {
- return $this->response;
- }
- /**
- * @return Response
- */
- public function getResponse(): Response
- {
- return $this->response;
- }
- }
|