Lo que intento hacer es que cuando se envie una solicitud mal, por ejemplo la busqueda de un registro inexistente, me devuelva el mensaje de error en json, como puedo resolver este problema?
Este es el error completo que me aparece en el laravel.log:
Declaration of App\Exceptions\Handler::convertValidationExceptionToResponse(App\Exceptions\ValidationException $e, $request) should be compatible with Illuminate\Foundation\Exceptions\Handler::convertValidationExceptionToResponse(Illuminate\Validation\ValidationException $e, $request) {"exception":"[object] (ErrorException(code: 0): Declaration of App\Exceptions\Handler::convertValidationExceptionToResponse(App\Exceptions\ValidationException $e, $request) should be compatible with Illuminate\Foundation\Exceptions\Handler::convertValidationExceptionToResponse(Illuminate\Validation\ValidationException $e, $request) at E:\Proyectos\Laravel\apirestful\app\Exceptions\Handler.php:9)
Este es el codigo que tengo en mi App\Exception\Handler.php:
<?php
namespace App\Exceptions;
use Exception;
use App\Traits\ApiResponser;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
use ApiResponser;
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Exception $exception)
{
if ($exception instanceof ValidationException)
{
return $this->convertValidationExceptionToResponse($exception, $request);
}
if ($exception instanceof ModelNotFoundException)
{
$modelo = strtolower(class_basename($exception->getModel())) ;
return $this->errorResponse('No existe ninguna instancia de modelo {$modelo} con el id especificado', 404);
}
return parent::render($request, $exception);
}
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
{
$errors = $e->validator->errors()->getMessages();
return $this->errorResponse($errors, 422);
}
}
Este es codigo de mi App\Traits\ApiResponser:
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
trait ApiResponser
{
private function successResponse($data, $code)
{
return response()->json($data, $code);
}
protected function errorResponse($message, $code)
{
return response()->json(['error' => $message, 'code' => $code], $code);
}
protected function showAll(Collection $collection, $code = 200)
{
return $this->successResponse(['data' => $collection], $code);
}
protected function showOne(Model $instance, $code = 200)
{
return $this->successResponse(['data' => $instance], $code);
}
}