Laravel 5 Custom Fatal Error Exception - php

I am building a web service in Laravel 5. However if there's a fatal error, Laravel always return the generic 'Whoops, looks like something went wrong' page.
How can change it to return JSON / XML instead?
Note
I know that you can put other exception in app\Exceptions\Handler.php but this doesn't work for Fatal Error Exception
I read for Laravel 4 you can do App::fatal(function(){...}) Is this still the valid way to do in Laravel 5? If yes where should I put it?
Thanks heaps

Related

findOrFail Laravel 8 method not found

I am working on Laravel 8 , I have tried to use findOrFail method in order to set redirection to 404 error not found instead of displaying error but it is showing this error method findOrFail not found,
this is the line of code in ProfilesController that is causing the error
$user = User::findOrFail($user);
this is the output of the error
enter image description here
Thanks in advance,
Look like your code works fine. The issue Php Strom not able to identify methods.
I always use query() method to get autocomplete model methods
$user=User::query()->findOrFail($user);
or you might need to install any extension which can able to identify laravel methods

Laravel - Creating Custom Classes throws error of undefined

I've been following this short tutorial and wanted to make my own class and call it simple like classname::methodname, but without injection.
I get the following error:
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with
message 'Call to undefined method
App\Facades\LiveSearch::getServiceList()'
Link to Tutorial: here
It makes sense and that is why i cannot figure out where the problem lies ..
Hey you also need to do the follow up steps to create laravel classes.
I hope this reference on How to create a laravel class is helpful.

Laravel Handle Exceptions

I am using the Stripe API and Laravel together. If Stripe detects an error charging the card (such as using a test credit card number that throws an invalid security code error), the API bindings are supposed to throw an exception, which they do. The problem is, I am having issues catching the exception before Laravel throws up the error 500 page (I am trying to perform a redirect with an error message instead).
The code I've written is available on Pastebin: http://pastebin.com/ZaW2xbbt
The behavior I'm expecting is for the catch to fire and the redirect to be performed, but instead, I get the stack trace with the message and "Unhandled Exception". That's confusing me because I am handling the exception.
Variables such as $customer are valid and have been defined previously. Any ideas what's going on?
For any future viewers, here's an article on error handling in laravel 4.
Laravel 4 lets you catch Exceptions by exception type. For instance, you can handle Symfony's HttpException and of its sub-classes by adding this to your code:
// Catch HttpException, NotFoundHttpException, etc etc
App::error(function(HttpException $exception, $code, $fromConsole)
{
...
});
Symfony HttpExceptions (used in Laravel) can be found here.
You can also throw this in a ServiceProvider:
<?php namespace My\Namespace;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpKernel\Exception\HttpException;
class MyServiceProvider extends ServiceProvider {
public function register()
{
$this->app->error(function(HttpException $exception, $code, $fromConsole)
{
...
});
}
}
Hope that helps!
Generally, all errors logged by Laravel are logged under storage/logs folder
Anyway, the 500 error could be a syntax/parse error, in such case the Laravel framework could be not yet loaded when the error occurs and if so, the exception is not lhandled by Laravel.
In this case you should access the apache/vargrant/whatif php error log in some way (dependently on your server capabilities and configuration), in my personal cases I have configured the server to put that logs in a /storage/logs/error_log.txt file such that I can access them as other Laravel server logs
Note that in Laravel 5, you have app/Exceptions/Handler.php as entry point for customize exception handling/reporting
https://laravel.com/docs/5.7/errors#the-exception-handler

Kohana 404 custom page

I have been looking around and following each tutorials,there is one which stands out. http://blog.lysender.com/2011/02/kohana-3-1-migration-custom-error-pages/ <-- i followed this tutorial and everything went smoothly
the error is being detected
the exception is being handled
but there has been an exception that i cant seem to find. im currently having this exception
Fatal error: Exception thrown without a stack frame in Unknown on line 0
all of my codes are thesame to the site-link. please help me.. im bugging around for this since ever, i've looked through here also Kohana 3 - redirect to 404 page but since im a beginner, its really hard understanding it. I've also found out that there is a major revamp from KO 3.0 to 3.1 how about KO 3.2? Thank you for your help guys :)
From the kohana source-code.
- > If you receive *Fatal error: Exception thrown without a stack frame in Unknown on line 0*, it means there was an error within your exception handler. If using the example above, be sure *404.php* exists under */application/views/error/*.
Maybe it helps. This probably has been fixed, but I'm not following the kohana development that much. It's related to pull request #246: https://github.com/kohana/core/pull/246 and this is the source: https://github.com/kohana/core/pull/246/files#L208L76
here is how I do it with Kohana 3.2
Add exceptions handling stuff in index.php
try
{
$request = $request->execute();
}
catch(Kohana_HTTP_Exception_404 $e)
{
$request = Request::factory('errors/404')->execute();
}
catch(Exception $e)
{
$request = Request::factory('errors/500')->execute();
}
echo $request->send_headers()->body();
Then write Errors controller
class Controller_Errors extends Controller
{
public function __construct($request, $response)
{
parent::__construct($request, $response);
}
public function action_404()
{
$this->response->body(View::factory('errors/404'));
}
public function action_500()
{
$this->response->body(View::factory('errors/500'));
}
}
Create 2 corresponding error pages (404.php and 500.php in views/errors)
Add new route to your bootstrap.php or use default one (depends on you project's structure), just make sure Controller_Errors can be reached when exception is thrown
Now every time you throws the exception in your controller, it will display the custom error page, like this
throw new HTTP_Exception_404;

kohana 3.0.12 how to log

i am working on an website on kohana 3.0.12 and i have installed a module that loggs me some errors. All works fine, except that, when i want to effectively log an error, i get an error and i don;t know how to manage it.
Here is the messy code:
public static function handler(Exception $e)
{
// It's a nice time to log :)
Kohana::$log->add(Kohana::ERROR, Kohana_Exception::text($e));
etc code here
well that Kohana_Exception::text($e) causes an exception like: Call to undefined method Kohana_Exception::text() ? i guess it is a framework bug. any idea of how i can solve the problem? (i guess i should use another instance but Kohana_Exception:: but what instance?)
thank you
You get this error because neither Kohana_Exception nor Exception classes don't have text() method. I think the author of the module wanted to write like this:
Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
I belive the exception handling in 3.0 is located in the Kohana class. Try Kohana::exception(), or look in api guide if that isn't it.

Categories