Created
September 5, 2021 20:17
-
-
Save lucasgio/23d0f153d39e958dcee1fceed58e3cd6 to your computer and use it in GitHub Desktop.
Formato personalizado de error para peticion API o WEB. Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cuando hacemos una peticion por un cliente de peticiones http(Postman) si no indicamos en la cabecera el tipo de peticion o sea: | |
- Accept: application/json | |
No debera salir en nuestro cliente un error con formato HTML con error 404.Esto se debe a que nuestra API está esperando una petición de | |
tipo json. | |
Solucion | |
public function register() | |
{ | |
$this->renderable(function (NotFoundHttpException $e,$request) | |
{ | |
if ($request->wantsJson()) | |
{ | |
return response()->json(['message' => 'Object not found'], 404); | |
} else | |
{ | |
return response()->json(['message' => 'Invalid format request'], 401); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment