Skip to content

Commit

Permalink
Merge pull request #31 from TahsinGokalp/development
Browse files Browse the repository at this point in the history
Merge development to master
  • Loading branch information
TahsinGokalp authored Mar 18, 2023
2 parents 637407e + e85d4e0 commit 1e58d21
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 159 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0b3432d95fa080f1ceda922d0b95bd7a3cdcf0ee
2c9660debe53324156654726ddecf5bb04137c51
0b4771d854aa254e7e618e02e82d8f909aa3a003
3bd1a83d82ecd2ba335c084e85a837aa81bdf5ab
66cd5bd2cfb3463dfd7da224a15b043d0173a10e
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"extra": {
"laravel": {
"providers": [
"TahsinGokalp\\Lett\\LettServiceProvider"
"TahsinGokalp\\Lett\\ServiceProvider"
],
"aliases": {
"Lett": "TahsinGokalp\\Lett\\Facades\\Lett"
Expand Down
2 changes: 2 additions & 0 deletions config/lett.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@

'sleep' => env('LETT_SLEEP', 60),

'timeout' => env('LETT_CLIENT_TIMEOUT', 15),

/*
|--------------------------------------------------------------------------
| Skip exceptions
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 6
paths:
- src
- config
Expand Down
74 changes: 74 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace TahsinGokalp\Lett;

use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Psr\Http\Message\ResponseInterface;

class Client
{
protected PendingRequest|ClientInterface $client;

protected string $login;

protected string $project;

private int $timeout;

public function __construct(string $login, string $project, PendingRequest|ClientInterface $client = null)
{
$this->login = $login;
$this->project = $project;
$this->timeout = config('lett.timeout');
$this->client = $client ?: Http::timeout($this->timeout);
}

public function report(array $exception): PromiseInterface|ResponseInterface|null
{
try {
return $this->getHttpClient()
->withToken($this->login)
->asJson()
->acceptJson()
->withUserAgent('Lett-Package')
->when(
! config('lett.verify_ssl'),
function ($client) {
$client->withoutVerifying();
}
)
->post(
config('lett.server'),
array_merge(
[
'project' => $this->project,
'additional' => [],
],
$exception
)
)->toPsrResponse();
} catch (RequestException $e) {
return $e->getResponse();
} catch (GuzzleException|Exception) {
return null;
}
}

public function getHttpClient(): PendingRequest|ClientInterface
{
return $this->client;
}

public function setHttpClient(\GuzzleHttp\Client $client): self
{
$this->client = Http::timeout($this->timeout)->setClient($client)->buildClient();

return $this;
}
}
5 changes: 3 additions & 2 deletions src/Handler/LettHandler.php → src/Handler/LogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Level;
use Monolog\LogRecord;
use TahsinGokalp\Lett\Lett;
use Throwable;

class LettHandler extends AbstractProcessingHandler
class LogHandler extends AbstractProcessingHandler
{
protected Lett $lett;

Expand All @@ -18,7 +19,7 @@ public function __construct(Lett $lett, Level $level = Level::Error, bool $bubbl
parent::__construct($level, $bubble);
}

protected function write($record): void
protected function write(LogRecord $record): void
{
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof Throwable) {
$this->lett->handle(
Expand Down
74 changes: 0 additions & 74 deletions src/Http/Client.php

This file was deleted.

48 changes: 13 additions & 35 deletions src/Lett.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace TahsinGokalp\Lett;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Application;
Expand All @@ -15,7 +14,6 @@
use JsonException;
use Psr\Http\Message\ResponseInterface;
use TahsinGokalp\Lett\Concerns\Lettable;
use TahsinGokalp\Lett\Http\Client;
use Throwable;

class Lett
Expand All @@ -24,8 +22,6 @@ class Lett

private array $blacklist;

private ?string $lastExceptionId;

public function __construct(Client $client)
{
$this->client = $client;
Expand All @@ -35,7 +31,7 @@ public function __construct(Client $client)
}, config('lett.blacklist', []));
}

public function handle(Throwable $exception, string $fileType = 'php', array $customData = [])
public function handle(Throwable $exception, string $fileType = 'php', array $customData = []): mixed
{
$data = $this->getExceptionData($exception);

Expand All @@ -44,7 +40,7 @@ public function handle(Throwable $exception, string $fileType = 'php', array $cu
return false;
}

if ((string) $fileType === 'javascript') {
if ($fileType === 'javascript') {
$data['fullUrl'] = $customData['url'];
$data['file'] = $customData['file'];
$data['file_type'] = $fileType;
Expand Down Expand Up @@ -88,14 +84,10 @@ public function handle(Throwable $exception, string $fileType = 'php', array $cu

try {
$response = json_decode($rawResponse->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
} catch (JsonException) {
return false;
}

if (isset($response->id)) {
$this->setLastExceptionId($response->id);
}

if (config('lett.sleep') !== 0) {
$this->addExceptionToSleep($data);
}
Expand All @@ -116,11 +108,6 @@ public function isSkipEnvironment(): bool
return true;
}

public function getLastExceptionId(): ?string
{
return $this->lastExceptionId ?? null;
}

public function getExceptionData(Throwable $exception): array
{
$data = [];
Expand Down Expand Up @@ -165,12 +152,12 @@ public function getExceptionData(Throwable $exception): array
}

for ($i = -1 * abs($count); $i <= abs($count); $i++) {
$data['executor'][] = $this->getLineInfo($lines, $data['line'], $i);
$data['executor'][] = $this->getLineInfo($lines, (int) $data['line'], $i);
}
$data['executor'] = array_filter($data['executor']);

// Get project version
$data['project_version'] = config('lett.project_version', null);
$data['project_version'] = config('lett.project_version');

// to make symfony exception more readable
if ($data['class'] === 'Symfony\Component\Debug\Exception\FatalErrorException') {
Expand Down Expand Up @@ -199,7 +186,7 @@ public function shouldParameterValueBeFiltered(mixed $value): bool
return $value instanceof UploadedFile;
}

public function filterVariables($variables): array
public function filterVariables(mixed $variables): array
{
if (is_array($variables)) {
array_walk($variables, function ($val, $key) use (&$variables) {
Expand All @@ -219,7 +206,7 @@ public function filterVariables($variables): array
return [];
}

public function isSkipException($exceptionClass): bool
public function isSkipException(string $exceptionClass): bool
{
return in_array((string) $exceptionClass, config('lett.except'), true);
}
Expand Down Expand Up @@ -257,12 +244,7 @@ public function addExceptionToSleep(array $data): bool
return Cache::put($exceptionString, $exceptionString, config('lett.sleep'));
}

private function setLastExceptionId(?string $id): void
{
$this->lastExceptionId = $id;
}

private function getLineInfo($lines, $line, $i): array
private function getLineInfo(array $lines, int $line, int $i): array
{
$currentLine = $line + $i;

Expand All @@ -285,15 +267,11 @@ private function createExceptionString(array $data): string
. $data['file'] . '_' . $data['class']);
}

private function logError($exception): PromiseInterface|ResponseInterface|null
private function logError(array $exception): PromiseInterface|ResponseInterface|null
{
try {
return $this->client->report([
'exception' => $exception,
'user' => $this->getUser(),
]);
} catch (GuzzleException $e) {
return null;
}
return $this->client->report([
'exception' => $exception,
'user' => $this->getUser(),
]);
}
}
7 changes: 3 additions & 4 deletions src/LettServiceProvider.php → src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
use Monolog\Logger;
use TahsinGokalp\Lett\Commands\DoctorCommand;
use TahsinGokalp\Lett\Commands\TestCommand;
use TahsinGokalp\Lett\Handler\LettHandler;
use TahsinGokalp\Lett\Http\Client;
use TahsinGokalp\Lett\Handler\LogHandler;

class LettServiceProvider extends BaseServiceProvider
class ServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
Expand Down Expand Up @@ -48,7 +47,7 @@ public function register(): void

if ($this->app['log'] instanceof LogManager) {
$this->app['log']->extend('lett', function ($app) {
$handler = new LettHandler(
$handler = new LogHandler(
$app['lett']
);

Expand Down
Loading

0 comments on commit 1e58d21

Please sign in to comment.