<?php

namespace App\Exceptions;

use Dingo\Api\Exception\Handler as DingoHandler;
use Illuminate\Validation\ValidationException;

class ApiHandler extends DingoHandler
{
    public function handle($exception)
    {
        if ($exception instanceof ApiException) {
            $msg = $exception->getMessage();
            $out = json_decode($msg, true);

            return response()->json($out);
        }

        if ($exception instanceof ValidationException) {
            return response()->json(['code' => 1, 'msg' => array_values($exception->errors())[0][0], 'data' => []]);
        }

        return response()->json(['code' => 1, 'msg' => $exception->getMessage(), 'data' => []]);
    }
}
