Skip to content

Instantly share code, notes, and snippets.

@Faks
Forked from RowlandOti/global.php
Created April 15, 2018 10:12
Show Gist options
  • Save Faks/57d4683801cf4b48f357e0a56df5c1dc to your computer and use it in GitHub Desktop.
Save Faks/57d4683801cf4b48f357e0a56df5c1dc to your computer and use it in GitHub Desktop.
Laravel 4.2 Example of how to log errors and exceptions
<?php
/*
|--------------------------------------------------------------------------
| Register The Laravel Class Loader
|--------------------------------------------------------------------------
|
| In addition to using Composer, you may use the Laravel class loader to
| load your controllers and models. This is useful for keeping all of
| your classes in the "global" namespace without Composer updating.
|
*/
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/library',
));
/*
|--------------------------------------------------------------------------
| Application Error Handler
|--------------------------------------------------------------------------
|
| Here you may handle any errors that occur in your application, including
| logging them or displaying custom views for specific errors. You may
| even register several error handlers to handle different types of
| exceptions. If nothing is returned, the default error view is
| shown, which includes a detailed stack trace during debug.
|
*/
App::error(function(Exception $exception, $code)
{
Log::error($exception);
$message = $exception->getMessage();
// switch statements provided in case you need to add
// additional logic for specific error code.
switch ($code) {
case 401:
$message = (!$message ? $message = 'you are unauthorized to view this' : $message);
return Response::json(array(
'code' => 401,
'message' => $message
), 401);
case 404:
$message = (!$message ? $message = 'the requested resource was not found' : $message);
return Response::json(array(
'code' => 404,
'message' => $message
), 404);
case 405:
$message = (!$message ? $message = 'the method is not allowed' : $message);
return Response::json(array(
'code' => 405,
'message' => $message
), 405);
case 500:
$message = (!$message ? $message = 'internal server error' : $message);
return Response::json(array(
'code' => 500,
'message' => $message
), 500);
}
});
/*
|--------------------------------------------------------------------------
| Maintenance Mode Handler
|--------------------------------------------------------------------------
|
| The "down" Artisan command gives you the ability to put an application
| into maintenance mode. Here, you will define what is displayed back
| to the user if maintenance mode is in effect for the application.
|
*/
App::down(function()
{
return Response::make("Be right back!", 503);
});
/*
|--------------------------------------------------------------------------
| Require The Filters File
|--------------------------------------------------------------------------
|
| Next we will load the filters file for the application. This gives us
| a nice separate location to store our route and application filter
| definitions instead of putting them all in the main routes file.
|
*/
require app_path().'/filters.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment