How to test if render() an exception into an HTTP response working correctly. I want to test without calling $this->get()
This is a method in Laravel:
public function render($request, Exception $e)
{
$response['exception'] = get_class($e);
$response['message'] = $e->getMessage();
if ($e instanceof LockException) {
return $this->errorResponse('lock', $response, 'Lock error has occurred', $e->getCode());
}
return parent::render($request, $e);
}
I need to test if LockException has turn into an HTTP response.
Something like this. In your test, instantiate the controller into a variable, create a blank request and pass in your LockException:
$response = $controller->render($request, $exception);
$this->assertEquals('string of HTML you expect', $response);
Related
I'm trying to use Laravel API Resource and handle the error message by sending a specific HTTP code
Here is my code :
public function show($id)
{
try {
return FruitResource::make(Fruit::find($id));
}
catch(Exception $e)
{
throw new HttpException(500, 'My custom error message');
}
}
My try/catch is systematically ignored when I try to access the route.
I am voluntarily accessing an object that is not in the database. I have ErrorException with message Trying to get property 'id' of non-object.
I would like to be able to send my own Exception here, in case the user tries to access data that doesn't exist. And return a json error.
Try this (notice the \ before Exception):
public function show($id)
{
try {
return FruitResource::make(Fruit::find($id));
}
catch(\Exception $e)
{
throw new HttpException(500, 'My custom error message');
}
}
I am trying to return a custom exception message using Laravel's ValidationException class. I have it successfully working in the following example:
public function store(Request $request)
{
$this->validate($request, CurrencyValidatorArrays::$store);
try {
$this->currenciesInstance->createOrUpdateCurrency($request->all());
return redirect()->route('currencies.index')
->with('success', 'Successfully created currency');
} catch (Exception $e) {
return redirect()->route('currencies.create')
->with('error', $e->getMessage());
}
}
where the exception is thrown from within createOrUpdateCurrency()
if(Currency::where('position', $data['position'])->where('id', '!=', $id)->exists()) {
throw ValidationException::withMessages([
'error' => ['Position value is already taken']
]);
}
This then passes the exception message back to my view for display.
However, I am trying to implement this somewhere else when trying to delete a company I am checking that no users exist first:
public function destroy($id)
{
try {
$this->companiesInstance->deleteCompany($id);
return redirect()->route('companies.index')
->with('success', 'Successfully deleted company');
} catch (Exception $e) {
return redirect()->route('companies.index')
->with('error', $e->getMessage());
}
}
Inside of deleteCompany():
$company = Company::find($id);
if($company->users()->exists()){
throw ValidationException::withMessages([
'error' => ['Position value is already taken']
]);
}
For some reason this doesn't hit my catch block from the main destroy method,if I change the exception type to ValidationException from Exception I can access and see the exception object but not in the way I can in the Store() methods version. Any ideas what is going on here?
UPDATE:
So I have a some what of a workaround:
To get by the exception type issue I used:
catch (ValidationException | Exception $e) { ...
But that still does not help when accessing the exception messages as when it's of type ValidationException the default constructor validation message is returned from getMessage() and not my specified one.
PHP 7 handles exceptions a bit differently. You can read it here.
Coming back to your code, you have to try and catch object of Throwable instead of Exception.
try
{
// Code that may throw an Exception or Error.
}
catch (Throwable $t)
{
// Code that handles the error
}
Hope this helps.
I would like to catch somehow the laravel error, warning message. I don't want to disable them from the config/app.php file. I am using monolog to log some information.
This is my piece of code:
public function view($id){
try {
$tag = Tags::find(12313); // tags is a model
}catch(Exception $error){
echo 'error'; exit();
$this->log->logMessage(Logger::ERROR, $error->getMessage());
return redirect()->route('admin.tags')->with(['msg' => 'Smth went wrong']);
}
}
$this->log is a class where I am using the monolog class to log information.
The fact is that right now , it doesn't go to the catch part . I don't get the error message. I'm getting this message from laravel:
Trying to get property of non-object (View: ......
I intentionally put the number 12313 there to see if it is working or not. And for some reason is not working and I am not redirected . The idea, if something happened I want to redirect the user to a specific page with a general error message. How can I achieve that ?
You can do it in laravel .You can handle erors in App\Exceptions\Handler class
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException)
{
return response()->view('errors.404', [], 404);
}
if ($exception instanceof MethodNotAllowedHttpException)
{
return response()->view('errors.405', [], 405);
}
if($exception instanceof MethodNotAllowedHttpException)
{
return response()->view('errors.404', [], 405);
}
return parent::render($request, $exception);
}
find() method doesn't throw an exception if the record is not found. So do this instead:
public function view($id)
{
$tag = Tags::find(12313); // tags is a model
if (is_null($tag)) {
$this->log->logMessage(Logger::ERROR, $error->getMessage());
return redirect()->route('admin.tags')->with(['msg' => 'Smth went wrong']);
}
}
Or use findOrFail() which will throw an exception if specified record is not found.
Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The findOrFail and firstOrFail methods will retrieve the first result of the query; however, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown
To reproduce the error, simply upload a file(s) to any POST routes in Laravel that exceeds the post_max_size in your php.ini configuration.
My goal is to simply catch the error so I can inform the user that the file(s) he uploaded is too large. Say:
public function postUploadAvatar(Request $request)
try {
// Do something with $request->get('avatar')
// Maybe validate file, store, whatever.
} catch (PostTooLargeException $e) {
return 'File too large!';
}
}
The above code is in standard Laravel 5 (PSR-7). The problem with it is that the function can't execute once an error occurs on the injected request. Thereby can't catch it inside the function. So how to catch it then?
Laravel uses its ValidatePostSize middleware to check the post_max_size of the request and then throws the PostTooLargeException if the CONTENT_LENGTH of the request is too big. This means that the exception is thrown way before it even gets to your controller.
What you can do is use the render() method in your App\Exceptions\Handler e.g.
public function render($request, Exception $exception)
{
if ($exception instanceof PostTooLargeException) {
return response('File too large!', 422);
}
return parent::render($request, $exception);
}
Please note that you have to return a response from this method, you can't just return a string like you can from a controller method.
The above response is to replicate the return 'File too large!'; you have in the example in your question, you can obviously change this to be something else.
Hope this helps!
You can also redirect to a Laravel view of your choice if you wish to add a more content richer response to the user.
public function render($request, Exception $exception)
{
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException)
return response()->view('errors.post-too-large');
return parent::render($request, $exception);
}
ALSO NOTE:
For the exception to be caught you should make sure you provide the proper path to the PostTooLargeException by either importing the class using the use Illuminate\Http\Exceptions\PostTooLargeException; import statement or just writing the full path like in my example.
The exception is rendered before the Session starts so you cannot redirect back with an error message.
You can create a new blade file and redirect there like this:
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException) {
return $this->showCustomErrorPage();
}
return parent::render($request, $exception);
}
protected function showCustomErrorPage()
{
return view('errors.errors_page');
//you can also return a response like: "return response('File too large!', 422);"
}
To show a static message using sessions:
1.You have to move the ValidatePostSize class from the middleware array into the middlewareGroups array, directly after the StartSession.
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
After that in Handler.php you can do:
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException) {
return $this->showCustomErrorPage();
}
return parent::render($request, $exception);
}
protected function showCustomErrorPage()
{
return \Illuminate\Support\Facades\Redirect::back()->withErrors(['max_upload' => 'The Message']);
}
Show the message on your controller like this:
#error('max_upload')
<div class="alert" id="create-news-alert-image">{{ $message }}</div>
#enderror
I want to show the page 500 internal server error Page. when user had syntax error mistake in project can anyone help me? if i do some mistake in syntax i want to show that particular blade.
You need to create handler to catching FatalErrorExceptions in your handler like below code:
Handler
In app/Exceptions/Handler.php
public function render($request, Exception $e)
{
// 404 page when a model is not found
if ($e instanceof ModelNotFoundException) {
return response()->view('errors.404', [], 404);
}
// custom error message
if ($e instanceof \ErrorException) {
return response()->view('errors.500', [], 500);
} else {
return parent::render($request, $e);
}
return parent::render($request, $e);
}
View
See resources/views/errors/500.blade.php. If not exist then create it.
You can get more detailed OR other ways from Laravel 5 custom error view for 500
In your resources/views/errors folder create a file named 500.blade.php.
Laravel makes it easy to display custom error pages for various HTTP
status codes. For example, if you wish to customize the error page for
500 HTTP status codes, create a resources/views/errors/500.blade.php. This file will be served on all 500 errors generated by your application.
The problem is that Laravel will only do this automatic rendering of error pages for exceptions that are instances of HttpException. Unfortunately when your server throws an error (method does not exist, variable undefined, etc) it actually throws a FatalErrorException. As such, it is uncaught, and trickles down to the SymfonyDisplayer() which either gives you the trace (debug true) or ugly one-liner 'Whoops, looks like something went wrong' (debug false).
To solve this you have add this to your render method to app/Exceptions/Handler
# /app/Exceptions/Handler.php
# use Symfony\Component\Debug\Exception\FlattenException;
# public function render($request, Exception $e)
$exception = FlattenException::create($e);
$statusCode = $exception->getStatusCode($exception);
if ($statusCode === 404 or $statusCode === 500) {
return response()->view('errors.' . $statusCode, [], $statusCode);
}
Docs
My solution is simple, just replace your render() method in Exceptions\Handler.php file with:
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Exception $exception
*
* #return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if ($request->expectsJson()) {
return $this->renderJson($request, $exception);
}
if ($this->shouldReport($exception) && app()->environment('production')) {
$exception = new HttpException(500, $exception->getMessage(), $exception);
}
return parent::render($request, $exception);
}
It will show 500 page if app in production environment. You will need to have 500.blade.php view in your resources/views/errors folder.
// app/Exceptions/Handler.php
protected function prepareResponse($request, Exception $e)
{
if($this->isHttpException($e) === false && config('app.debug') === false) {
$e = new HttpException(500);
}
return parent::prepareResponse($request, $e);
}
Like #Amit said
The problem is that Laravel will only do this automatic rendering of
error pages for exceptions that are instances of HttpException.
So my solution is to replace whatever exception that is not HttpException by a HttpException.
in app\Exceptions\Handler create the following method:
protected function convertExceptionToResponse(Exception $e)
{
$e = FlattenException::create($e);
return response()->view('errors.500', ['exception' => $e], $e->getStatusCode(), $e->getHeaders());
}
it will override the one in the parent class (Illuminate\Foundation\Exceptions\Handler) that displays the whoops page.
In Laravel 5.4, you could override prepareException function in your app\Exception\Handler.php:
/**
* #inheridoc
*/
protected function prepareException(Exception $e)
{
$exception = parent::prepareException($e);
if(!config('app.debug')) {
if(!$exception instanceof HttpException && $this->shouldReport($exception)) {
$exception = new HttpException(500);
}
}
return $exception;
}