Handler.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace plugin\admin\app\exception;
  15. use Throwable;
  16. use Webman\Http\Request;
  17. use Webman\Http\Response;
  18. /**
  19. * Class Handler
  20. * @package support\exception
  21. */
  22. class Handler extends \support\exception\Handler
  23. {
  24. public function render(Request $request, Throwable $exception): Response
  25. {
  26. $code = $exception->getCode();
  27. $debug = $this->_debug ?? $this->debug;
  28. if ($request->expectsJson()) {
  29. $json = ['code' => $code ?: 500, 'msg' => $debug ? $exception->getMessage() : 'Server internal error', 'type' => 'failed'];
  30. $debug && $json['traces'] = (string)$exception;
  31. return new Response(200, ['Content-Type' => 'application/json'],
  32. \json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
  33. }
  34. $error = $debug ? \nl2br((string)$exception) : 'Server internal error';
  35. return new Response(500, [], $error);
  36. }
  37. }