Added log levels to error object.

This commit is contained in:
abdulmohsen
2022-06-11 10:25:09 +03:00
parent 699f634f2e
commit d06859cce3
3 changed files with 76 additions and 2 deletions

View File

@@ -11,10 +11,11 @@ trait CommonTrait
*
* @param Context $context Context to associate the call with.
* @param callable():Response $fn Closure
* @param string|null $action the action name to personalize the message.
*
* @return Response We should Expand the catch to include common http errors. json decode failing.
*/
protected function tryResponse(Context $context, callable $fn): Response
protected function tryResponse(Context $context, callable $fn, string|null $action = null): Response
{
try {
return $fn();
@@ -22,8 +23,9 @@ trait CommonTrait
return new Response(
status: false,
error: new Error(
message: 'Unhandled exception was thrown in [%(client): %(backend)] context. %(message)',
message: 'Unhandled exception was thrown in [%(client): %(backend)] %(action). %(message)',
context: [
'action' => $action ?? 'context',
'backend' => $context->backendName,
'client' => $context->clientName,
'message' => $e->getMessage(),
@@ -34,6 +36,7 @@ trait CommonTrait
'message' => $e->getMessage(),
]
],
level: Levels::WARNING,
previous: $e
)
);

View File

@@ -14,11 +14,13 @@ final class Error
*
* @param string $message Error message.
* @param array $context Error message context.
* @param Levels $level Which log level the error should be logged into.
* @param Throwable|null $previous Previous exception stack trace.
*/
public function __construct(
public readonly string $message,
public readonly array $context = [],
public readonly Levels $level = Levels::ERROR,
public readonly Throwable|null $previous = null,
) {
}
@@ -43,6 +45,16 @@ final class Error
return true === str_contains($this->message, '%(');
}
/**
* Get which log level should this message be logged into.
*
* @return string
*/
public function level(): string
{
return $this->level->value;
}
/**
* This method convert our logging messages into an actual human-readable message.
* If no tags are found. we simply return the message as it is.

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Backends\Common;
enum Levels: string
{
/**
* Detailed debug information
*/
case DEBUG = 'DEBUG';
/**
* Interesting events
*
* Examples: User logs in, SQL logs.
*/
case INFO = 'INFO';
/**
* Uncommon events
*/
case NOTICE = 'NOTICE';
/**
* Exceptional occurrences that are not errors
*
* Examples: Use of deprecated APIs, poor use of an API,
* undesirable things that are not necessarily wrong.
*/
case WARNING = 'WARNING';
/**
* Runtime errors
*/
case ERROR = 'ERROR';
/**
* Critical conditions
*
* Example: Application component unavailable, unexpected exception.
*/
case CRITICAL = 'CRITICAL';
/**
* Action must be taken immediately
*
* Example: Entire website down, database unavailable, etc.
* This should trigger the SMS alerts and wake you up.
*/
case ALERT = 'ALERT';
/**
* Urgent alert.
*/
case EMERGENCY = 'EMERGENCY';
}