Skip to content

Commit

Permalink
Log non displayed errors (#59)
Browse files Browse the repository at this point in the history
* For errors we do not wish to putput in the response when API debug is set to false, let's log them in laravel with debug level instead, just in case we need the underlying error message

* Apply fixes from StyleCI (#58)
  • Loading branch information
specialtactics authored Feb 28, 2023
1 parent 9e898f1 commit c73d93f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/APIBoilerplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Specialtactics\L5Api;

use Config;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class APIBoilerplate
Expand All @@ -14,6 +15,13 @@ class APIBoilerplate
const SNAKE_CASE = 'snake-case';
const DEFAULT_CASE = self::CAMEL_CASE;

/**
* The logger to be used by the boilerplate
*
* @var LoggerInterface $logger
*/
protected static $logger;

/**
* Case type config path
*/
Expand Down Expand Up @@ -136,4 +144,25 @@ public static function formatKeyCaseAccordingToReponseFormat($value)
{
return self::formatCaseAccordingToResponseFormat($value);
}

/**
* Set a custom logger
*
* @param LoggerInterface $logger
* @return void
*/
public static function setLogger(LoggerInterface $logger)
{
static::$logger = $logger;
}

/**
* Return the logger we are using
*
* @return LoggerInterface
*/
public static function getLogger(): LoggerInterface
{
return static::$logger;
}
}
3 changes: 3 additions & 0 deletions src/L5ApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function register()
if ($this->app->runningInConsole()) {
$this->commands(Console\Commands\MakeApiResource::class);
}

// Set the logger instance to the laravel's default to begin with
APIBoilerplate::setLogger(\Log::getLogger());
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Services/RestfulService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Specialtactics\L5Api\Services;

use Specialtactics\L5Api\APIBoilerplate;
use Validator;
use Config;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -87,16 +88,20 @@ public function patch($model, array $data)
if ($e instanceof QueryException) {
if (stristr($e->getMessage(), 'duplicate')) {
throw new ConflictHttpException('The resource already exists: ' . class_basename($model));
} elseif (Config::get('api.debug') === true) {
} elseif (config::get('api.debug') === true) {
throw $e;
} else {
APIBoilerplate::getLogger()->debug($e->getMessage());
}
}

// Default HTTP exception to use for storage errors
$errorMessage = 'Unexpected error trying to store this resource.';

if (Config::get('api.debug') === true) {
if (config::get('api.debug') === true) {
$errorMessage .= ' ' . $e->getMessage();
} else {
APIBoilerplate::getLogger()->debug($errorMessage . ' ' . $e->getMessage());
}

throw new UnprocessableEntityHttpException($errorMessage);
Expand Down Expand Up @@ -124,6 +129,8 @@ public function persistResource(RestfulModel $resource)
throw new ConflictHttpException('The resource already exists: ' . class_basename($resource));
} elseif (Config::get('api.debug') === true) {
throw $e;
} else {
APIBoilerplate::getLogger()->debug($e->getMessage());
}
}

Expand All @@ -132,6 +139,8 @@ public function persistResource(RestfulModel $resource)

if (Config::get('api.debug') === true) {
$errorMessage .= ' ' . $e->getMessage();
} else {
APIBoilerplate::getLogger()->debug($errorMessage . ' ' . $e->getMessage());
}

throw new UnprocessableEntityHttpException($errorMessage);
Expand Down

0 comments on commit c73d93f

Please sign in to comment.