Use new context to log the request id

This commit is contained in:
Lance Pioch 2024-03-23 08:07:00 -04:00
parent 03b1fc4636
commit 796a236ef4

View File

@ -5,6 +5,7 @@ namespace App\Exceptions\Http\Connection;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use App\Exceptions\DisplayException; use App\Exceptions\DisplayException;
use Illuminate\Support\Facades\Context;
/** /**
* @method \GuzzleHttp\Exception\GuzzleException getPrevious() * @method \GuzzleHttp\Exception\GuzzleException getPrevious()
@ -30,6 +31,8 @@ class DaemonConnectionException extends DisplayException
$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null; $response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;
$this->requestId = $response?->getHeaderLine('X-Request-Id'); $this->requestId = $response?->getHeaderLine('X-Request-Id');
Context::add('request_id', $this->requestId);
if ($useStatusCode) { if ($useStatusCode) {
$this->statusCode = is_null($response) ? $this->statusCode : $response->getStatusCode(); $this->statusCode = is_null($response) ? $this->statusCode : $response->getStatusCode();
// There are rare conditions where daemon encounters a panic condition and crashes the // There are rare conditions where daemon encounters a panic condition and crashes the
@ -63,17 +66,6 @@ class DaemonConnectionException extends DisplayException
parent::__construct($message, $previous, $level); parent::__construct($message, $previous, $level);
} }
/**
* Override the default reporting method for DisplayException by just logging immediately
* here and including the specific X-Request-Id header that was returned by the call.
*/
public function report()
{
logger()->{$this->getErrorLevel()}($this->getPrevious(), [
'request_id' => $this->requestId,
]);
}
/** /**
* Return the HTTP status code for this exception. * Return the HTTP status code for this exception.
*/ */
@ -81,9 +73,4 @@ class DaemonConnectionException extends DisplayException
{ {
return $this->statusCode; return $this->statusCode;
} }
public function getRequestId(): ?string
{
return $this->requestId;
}
} }