php优雅处理业务异常输出数据

2022年7月26日14:50:20

通常我们在mvc框架处理的时候,在控制器里面基本都这么写

class TestController extends BaseController
{
    use ResponseTrait;

    public function index(Request $request)
    {
//        $iterator = new ArrayIterator(array('recipe' => 'pancakes', 'egg', 'milk', 'flour'));
//        p(iterator_count($iterator));

        try {

            throw new Exception('111111111');


        } catch (Throwable $e) {
            return $this->fails($e);
        }

//        return view('welcome');
    }

trait代码
trait ResponseTrait
{
    public function success(mixed $data = '', string $msg = '操作成功')
    {
        return response()->json(['code' => GlobalCode::SUCCESS, 'msg' => $msg, 'data' => $data]);
    }

    public function fail(Exception $e, $status = 200, array $headers = [])
    {
        if (request()->debug == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
            return response()->json(['code' => GlobalCode::FAIL, 'msg' => $e->getMessage(), 'data' => $e->getTraceAsString()], $status, $headers);
        } else {
            return response()->json(['code' => GlobalCode::FAIL, 'msg' => $e->getMessage(), 'data' => $e->getMessage()], $status, $headers);
        }
    }

    public function grant(Exception $e)
    {
        if (request()->debug == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
            return response()->json(['code' => GlobalCode::GRANT, 'msg' => $e->getMessage(), 'data' => $e->getTraceAsString()]);
        } else {
            return response()->json(['code' => GlobalCode::GRANT, 'msg' => $e->getMessage(), 'data' => $e->getMessage()]);
        }
    }

    public function successMsg(string $msg = 'success')
    {
        return response()->json(['code' => GlobalCode::SUCCESS, 'msg' => $msg, 'data' => $msg]);
    }

    public function failMsg(string|array $msg = 'success')
    {
        return response()->json(['code' => GlobalCode::FAIL, 'msg' => $msg, 'data' => $msg]);
    }
}

如果业务逻辑异常比较多比如 
use RuntimeException; //系统异常
use LogicException;   //业务逻辑异常
use App\Exceptions\AuthorizeException; //授权异常

那么需要处理就需要写很多个返回方法
优化版本
 public function fails(Throwable $e, $status = 200, array $headers = [])
    {
        try {
//            p($e::class);
            throw $e;
        } catch (RuntimeException $runtimeException) {
            return  $this->ret(GlobalCode::FAIL, $runtimeException->getMessage(), $runtimeException->getTraceAsString());
        } catch (LogicException $logicException) {

        } catch (AuthorizeException $authorizeException) {

        } catch (Exception $exception) {

        } catch (Error $error) {

        } finally {

        }
    }

    public function ret(string $code, string $msg, string $trace)
    {
        if (request()->debug == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
            return response()->json(['code' => $code, 'msg' => $msg, 'data' => $trace]);
        } else {
            return response()->json(['code' => $code, 'msg' => $msg, 'data' => $msg]);
        }
    }

这样各个异常都可以在一个方法里面返回,只需要在业务里面抛出对应的异常就可以

posted on 2022-07-26 15:06  zh7314  阅读(87)  评论(0编辑  收藏  举报