Not working Custom HTTP Error Pages in laravel 5.2 - laravel

I use Laravel 5.2
I wanted to change this error :
Whoops, looks like something went wrong.
first please see : Laravel
I created a new file resources/views/errors/404.blade.php but my app error didn't change !
it change just when not found url at route but when insert url injection in to $_GET it show "whoops .." yet
for example work for this link : http://domain.com/dgdgergehrhddg54d6g8
but not working for this injection : http://domain.com/listmanage=8 insert 9 instens of 8
error message when debug is true :
ErrorException: file.php line 215
Trying to get property of non-object

You are viewing that error page because of the environment you are into.
As default, for local environments, the "Whoops" format is shown.
For Production environments, the error/x.blade.php files are used.
To customize this you simply go to: ./app/Exceptions/Handler.php
And modify the render function. You can do something like this:
public function render($request, Exception $e)
{
// If an ErrorException is received and this enviroment is local
if ($e instanceof \ErrorException && app()->environment() == 'local') {
// Show customized page
return response()->view('errors.404', [], $e->getCode());
}
return parent::render($request, $e);
}
Cheers :)

on Larvel 5.2 on your app/exceptions/handler.php just extend this method renderHttpException ie add this method to handler.php
/**
* Render the given HttpException.
*
* #param \Symfony\Component\HttpKernel\Exception\HttpException $e
* #return \Symfony\Component\HttpFoundation\Response
*/
protected function renderHttpException(HttpException $e)
{
// to get status code ie 404, 503, 500
$status = $e->getStatusCode();
if (view()->exists("errors.{$status}")) {
return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
} else {
return $this->convertExceptionToResponse($e);
}
}
Hope that helps.

Related

Codeigniter Fatal error: Call to a member function getMessage() on a non-object

I am integrating PayPal Library with Codeigniter 3. When I first tried it worked well and I used test payment and gave me success but now, I am trying pay now button gives me error like this:
Fatal error: Call to a member function getMessage() on a non-object in
C:\OSPanel\domains\localhost\1\public_html\application\libraries\PayPal-PHP-
SDK\paypal\rest-api-sdk-php\sample\common.php on line 119
This is the Code of getMessage():
public static function printError($title, $objectName, $objectId = null,
$request = null, $exception = null)
{
$data = null;
if ($exception instanceof \PayPal\Exception\PayPalConnectionException) {
$data = $exception->getData();
}
self::printOutput($title, $objectName, $objectId, $request, $data,
$exception->getMessage());
}
And here when I call ResultError in Contorller:
try {
$payment->create($this->_api_context);
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $ex);
exit(1);
}
I searched a lot and found that they are saying there is something with null values and I changed them still no result. Please help me to solve this.

How to prevent an error on undefined routes in Laravel 5.5

I developed an API with Laravel 5.5. All is working fine.
But imagine that a user enter an "api" url directly in the browser (for example: api/books), then they will receive an error:
InvalidArgumentException
Route [login] not defined.
How to prevent this? I tried to add some routes in the routes/web.php file, but without success.
Or perhaps I should do nothing (there will be very few users who will do that)?
I found the answer here:
Laravel 5.5 change unauthenticated login redirect url
I only do that in the "app/Exceptions/Handler.php" file, I modified the function "render" like that :
public function render($request, Exception $exception)
{
// return parent::render($request, $exception);
// add dom
return redirect('/');
// or redirection with a json
/*
return response()->json(
[
'errors' => [
'status' => 401,
'message' => 'Unauthenticated',
]
], 401
);
*/
}
And it works fine. As the "Laravel" part will be used only as back end for APIs, it will be enough.

Does formatErrors not working anymore in Laravel 5.5?

I have following code in my Request class that returns custom messages.
public function formatErrors(\Illuminate\Contracts\Validation\Validator $validator) {
if($validator->fails()) {
$validator->errors()->add('Message', "Validation failed");
}
return parent::formatErrors($validator);
}
It was returning the error messages in Laravel 5.4 but seems like this function is no more working in Laravel 5.5
Did anybody face this issue in Laravel 5.5?
In Upgrade guide you can read:
In Laravel 5.5, all exceptions, including validation exceptions, are
converted into HTTP responses by the exception handler. In addition,
the default format for JSON validation errors has changed. The new
format conforms to the following convention:
...
So what you should do is add to app\Exceptions\Handler.php file the following method:
protected function invalidJson($request, ValidationException $exception)
{
return response()->json([
'message' => 'Validation failed',
'errors' => $exception->errors(),
], $exception->status);
}
obviously you might want to adjust this method more because in previous Laravel versions it was by default like this:
return response()->json($exception->errors(), $exception->status);

I want to redirect Laravel version 5.2 default 404 page to my template page of 404, from where and what to change

I am using Laravel version 5.2 and don't know how to redirect Laravel default page to my template 404 page
use abort(404);
Some exceptions describe HTTP error codes from the server. For
example, this may be a "page not found" error (404), an "unauthorized
error" (401) or even a developer generated 500 error. In order to
generate such a response from anywhere in your application, use the
following:
abort(404);
If you invoke abort(404); anywhere in your route or controller it will throw HTTPNotFoundException which looks for a blade template to display in resources/views/errors/ directory with the filename same as the error code.
Example:
in your app/Http/routes.php
Route::get('/test', function(){
return abort(404);
});
in your resources/views/errors/ directory create 404.blade.php, notice the name of the file corresponds with the abort(404);
Reference: https://laravel.com/docs/5.2/errors#http-exceptions
Modify app/Exceptions/Handler.php and add some lines as below:-
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
{
return redirect('/');
}
return parent::render($request, $e); }
Use your route name in place of / where you want to redirect.
Create a view and set this code in app/Exceptions/Handler.php :
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
.
.
.
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException)
{
return response()->view('missing', [], 404);
}
return parent::render($request, $e);
}
in the /resources/views/errors directory there is a 404 page
edit this page and you can make it look like you want

Custom 404 page in Lumen

I'm new to Lumen and want to create an app with this framework. Now I have the problem that if some user enters a wrong url => http://www.example.com/abuot (wrong) => http://www.example.com/about (right), I want to present a custom error page and it would be ideal happen within the middleware level.
Furthermore, I am able to check if the current url is valid or not, but I am not sure how can I "make" the view within the middleware, the response()->view() won't work.
Would be awesome if somebody can help.
Seeing as errors are handled in App\Exceptions\Handler, this is the best place to deal with them.
If you are only after a custom 404 error page, then you could do this quite easily:
Add this line up the top of the Handler file:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Alter the render function to look like so:
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException){
return response(view("errors.404"), 404);
}
return parent::render($request, $e);
}
This assumes your custom 404 page is stored in an errors folder within your views, and will return the custom error page along with a 404 status code.
You may want to add this so that when for example blade blows up the error page hander will not throw a PHP error.
public function render($request, Exception $exception)
{
if (method_exists('Exception','getStatusCode')){
if($exception->getStatusCode() == 404){
return response(view("errors.404"), 404);
}
if($exception->getStatusCode() == 500){
return response(view("errors.500"), 404);
}
}
return parent::render($request, $exception);
}
I am using Lumen 8.x version and below solution worked for me:
File path: ‎⁨▸ ⁨app⁩ ▸ ⁨Exceptions⁩ ▸ ⁨Handler.php
public function render($request, Throwable $exception)
{
// start custom code
if($exception->getStatusCode() == 404){
return response(view("errors.404"), 404);
}
if($exception->getStatusCode() == 500){
return response(view("errors.500"), 404);
}
// end custom code
return parent::render($request, $exception);
}
Do not forget to create errors folder at /resources/views/errors and create the below 2 new files in errors folder:
404.blade.php
500.blade.php
and add html tags and messages in those files what you want to add.
Happy to help you. Thanks for asking this question.
I faced the same situation. response(view("errors.404"), 404)did not work for me, so I changed it as follows:
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException){
return response(view('errors.404')->render(), 404);
}
return parent::render($request, $exception);
}

Resources