ApiException.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php namespace App\Exceptions\Api;
  2. use Exception;
  3. use Throwable;
  4. /**
  5. * 接口异常
  6. */
  7. class ApiException extends Exception
  8. {
  9. // 错误返回码
  10. protected $errCode = 'error';
  11. // 错误返回数据
  12. protected $errData = '';
  13. // 本地化前缀
  14. protected $langkey = 'message';
  15. /**
  16. * 抛出错误信息
  17. *
  18. * @param String $message — [optional] The Exception message to throw.
  19. * @param Mixed $errData — 错误返回数据
  20. * @param Int $code — [optional] The Exception code.
  21. * @param Throwable $previous — [optional] The previous throwable used for the exception chaining.
  22. * @return Mixed
  23. *
  24. */
  25. public function __construct( $message="",$errData='',$code=0,?Throwable $previous=null )
  26. {
  27. // 如果本地化前缀存在则本地化处理
  28. if( $this->langkey ) {
  29. // 消息key
  30. $msgKey = $this->langkey.'.'.$message;
  31. // 提示信息本地化
  32. $trans = trans($msgKey);
  33. // 如果没有找到对应的提示,使用原提示信息
  34. $message = ($trans == $msgKey) ? $message : $trans;
  35. }
  36. // 传递数据到父级
  37. parent::__construct($message, $code, $previous);
  38. // 错误信息说
  39. if( $errData ) $this->errData = $errData;
  40. }
  41. /**
  42. * 获取错误返回码
  43. *
  44. */
  45. public function getErrCode()
  46. {
  47. return $this->errCode;
  48. }
  49. /**
  50. * 获取返回数据
  51. *
  52. */
  53. public function getErrData()
  54. {
  55. return $this->errData;
  56. }
  57. }