status(); if ($response->failed()) { $body = $response->body(); $error = Str::limit($body, 1000); $responseBody = $this->truncate($body); } return $response; } catch (Throwable $e) { $error = $e->getMessage(); // RequestException carries the response, ConnectionException // doesn't. Pull the body when it's available. if ($e instanceof RequestException) { $responseBody = $this->truncate($e->response->body()); } throw $e; } finally { ApiLog::create([ 'service' => $service, 'method' => strtoupper($method), 'url' => $url, 'status_code' => $statusCode, 'duration_ms' => (int) round((microtime(true) - $start) * 1000), 'error' => $error, 'response_body' => $responseBody, ]); } } private function truncate(string $body): string { return strlen($body) > self::RESPONSE_BODY_CAP ? substr($body, 0, self::RESPONSE_BODY_CAP) : $body; } }