According to the Laravel handling error documentation I've put this code snippet in one of my functions:
try {
// Send an email...
} catch (Throwable $e) {
report($e);
return false;
}
Now I want to check out the error messages but I have no idea where they are stored.
I've tried both storage/logs/laravel.log and var/log/apache2/error.log but I found nothing.
Where does report() store the error message ?
It depends on your config/logging.php.
By default it should be the stack channel.
If your channel is set up to daily it willl create a file per day (e.g: laravel-2022-05-16.log
Otherwise you can leave the single channel it will put everything in storage/logs/laravel.log
Related
I'm firing a status update event to broadcast on pusher. That's all working fine, however the code after the event fire doesnt work. At all.
Even if i put dd("hello"); nothing comes up. If i remove the event fire, things work as expected. I need this to work as i have a different redirect based on object parameter as shown below.
I can't figure out why this is happening, thanks for the help!
{
$room->start($connector);
event(new RoomStatusUpdated($room)); //problem here
if($room->autoLogin==1)
{
return redirect(route("room.join", $room->id));
}
else
{
notify()->info("","Meeting has started","bottomRight");
return back();
}
}
event(new RoomStatusUpdated($room)); //problem here
if your QUEUE_DRIVE=sync, Better you need to double check your event call as the event needs to be successful. Best practice is to include a temporary Logger by putting try-catch to your event
try{
//your event call
}catch(\Exception e){
Log::info("Code got excited: Line: {$e->getLine()} with error message: {$e->getMessage()}");
}
if queue driver is using a external like sqs or azure put back to sync first to trace back your event.
My question is whenever there is a 403 error I should redirect to my own custom page or show the flash message(on the same page) something like that.How can I achieve this in Laravel?.
You can use app/Exceptions/Handler.php for this
public function render($request, Exception $e)
{
if ($e->getStatusCode() == 403) {
return redirect('yourpage'); // this will be on a 403 exception
}
return parent::render($request, $e); // all the other exceptions
}
You can create a view for specific HTTP error codes. If you set up a Blade template at resources/views/errors/403.blade.php, it will get used for all 403 error responses.
Source
Alternatively you can set up a custom exception handler for 403 responses if you need something more involved. I found a good example of this here.
you can make you own page in inside the view .resources/views/errors/yourblade.blade.php
after that just return your page to that page.
You can use app/Exceptions/Handler.php for this as Thomas Moors stated above or you can use a try catch for basic error handling.
try {
do Something //
}
catch (Exception $e) {
//log error in log file or dv
return redirect->('home');
}
I have a Route defined as:
$app->post('api/v1/Subject','SubjectController#createSubject');
And in the Controller I have the following code:
public function createSubject(Request $request){
$Subject = Subject::create($request->all());
return response()->json($Subject);
}
Now, when someone sends incorrect data, it triggers a Query Exception - "SQLSTATE[23000]: Integrity constraint violation:" which is understood.
However, what I want is: I want do not want Lumen to send its own default Error Page in API Response. I want to capture this error event and send my own customized response. How can I do that?
Since I could not find a solution, I tried to add my own view at: /resources/views/errors/500.blade.php but Lumen is even ignoring this template. Please help. Ideally, I would want to capture this error event and send my own customized response.
EDIT:
Lumen was reporting two exceptions - PDOException and QueryException at the 500 error response. So, to get a custom error message, I put the following code in side function render() in app\Exceptions\Handler:
public function render($request, Exception $e)
{
if($e instanceof PDOException){
return response('It is my Custom response for PDOException that caused 500 error response.');
}
if($e instanceof QueryException){
return response('It is my Custom response for QueryException that cuased 500 error response.');
}
return parent::render($request, $e);
}
In my package, before I perform a query on my database I check if some of the params provided are valid:
//check
if(!$this->checkId($id)) //error
//do the query
$user = User::find($id);
What's the best way to handle this in a laravel package?
Throw an error? If so, which kind? Do an app abort?
Using findOrFail()
There is a pretty good method that Laravel provides just for this case called findOrFail().
What you can do is use that and then catch the exception.
try {
$user = User::findOrFail($queryParam);
} catch(ModelNotFoundException $e) {
// Do things that should happen here if your query parameter does not exist.
}
If you do not wish to catch it here, you can setup a listener for this exception. You can place it anywhere you wish as long as it's being autoloaded.
use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
return Response::make('Not Found', 404);
});
Throwing an exception
If all you want to do is throw an error if a query param isn't available, you can throw any of the exceptions found here... https://github.com/symfony/symfony/tree/master/src/Symfony/Component/HttpKernel/Exception
If none suit you, you can create your own and just extend Symfony\Component\HttpKernel\Exception\HttpException and throw new YourCustomException($statusCode, $message);. Just follow what they've done in the other exception classes and it should be fairly easy.
Then you can modify the the "exception listener" to catch YourCustomException.
Here is a tutorial I found... http://fideloper.com/laravel4-error-handling
I would use Event Listeners for the most control.
Create an events.php in your app directory and include it in app/start/global.php
Or, you can register anywhere in your package.
Create a listener:
Event::listen('paramCheck.noId', function($id)
{
// do some logic, log something, return something, throw an error...
});
Then fire the event from whenever you need to:
if(!$this->checkId($id))
$returnFromEvent = Event::fire('paramCheck.noId', array($id));
I have developed the new custom module with Admin panel Config Section.
Also i have write the event observer for my custom config section save config button.
In this case i have validate the some data's to show the error message in my config section.
By Default when i click the custom config section it show default success message "The configuration has been saved." . But i need to show the Error Message instead of success message.
So can any one help me this situation.
Any Help Much Appreciated.
Thanks
Use try - catch construction to handle your errors correctly. On success - put in the session success message which you already have. On some core error you are able to use:
$session = Mage::getSingleton('adminhtml/session');
try {
....
} catch (Mage_Core_Exception $e) {
foreach(explode("\n", $e->getMessage()) as $message) {
$session->addError($message);
}
}
On PHP exception:
catch (Exception $e) {
$session->addException($e,
Mage::helper('adminhtml')->__('An error occurred while saving this configuration:') . ' '
. $e->getMessage());
}
If you wrote an observer method on "admin_system_config_changed_section_{$section}" event, you just need to throw Mage_Core_Exception with validation message in the method. See Mage_Adminhtml_System_ConfigController::saveAction() method for more information.