From e7c5ba2e32499d5bb3e8de4f391c0c41c31b8426 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:35:27 +0000 Subject: [PATCH 01/12] fix: dont throw on log push failure --- src/Logger/Adapter/AppSignal.php | 4 +--- src/Logger/Adapter/LogOwl.php | 4 +--- src/Logger/Adapter/Raygun.php | 4 +--- src/Logger/Adapter/Sentry.php | 4 +--- tests/e2e/Adapter/AppSignalTest.php | 1 + tests/e2e/Adapter/LogOwlTest.php | 1 + tests/e2e/Adapter/RaygunTest.php | 1 + tests/e2e/Adapter/SentryTest.php | 1 + tests/e2e/AdapterBase.php | 12 ++++++++++++ 9 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Logger/Adapter/AppSignal.php b/src/Logger/Adapter/AppSignal.php index 2f64bfb..fade51c 100644 --- a/src/Logger/Adapter/AppSignal.php +++ b/src/Logger/Adapter/AppSignal.php @@ -42,8 +42,6 @@ public static function getName(): string * * @param Log $log * @return int - * - * @throws Exception */ public function push(Log $log): int { @@ -130,7 +128,7 @@ public function push(Log $log): int $error = \curl_error($ch); if ($response >= 400 || $response === 0) { - throw new Exception("Log could not be pushed with status code {$response}: {$result} ({$error})"); + error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); } \curl_close($ch); diff --git a/src/Logger/Adapter/LogOwl.php b/src/Logger/Adapter/LogOwl.php index 7916bac..6db59a1 100644 --- a/src/Logger/Adapter/LogOwl.php +++ b/src/Logger/Adapter/LogOwl.php @@ -75,8 +75,6 @@ public static function getAdapterVersion(): string * * @param Log $log * @return int - * - * @throws Exception */ public function push(Log $log): int { @@ -149,7 +147,7 @@ public function push(Log $log): int $error = \curl_error($ch); if ($response >= 400 || $response === 0) { - throw new Exception("Log could not be pushed with status code {$response}: {$result} ({$error})"); + error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); } \curl_close($ch); diff --git a/src/Logger/Adapter/Raygun.php b/src/Logger/Adapter/Raygun.php index ff73330..6daf8ad 100644 --- a/src/Logger/Adapter/Raygun.php +++ b/src/Logger/Adapter/Raygun.php @@ -42,8 +42,6 @@ public static function getName(): string * * @param Log $log * @return int - * - * @throws Exception */ public function push(Log $log): int { @@ -115,7 +113,7 @@ public function push(Log $log): int $error = \curl_error($ch); if ($response >= 400 || $response === 0) { - throw new Exception("Log could not be pushed with status code {$response}: {$result} ({$error})"); + error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); } \curl_close($ch); diff --git a/src/Logger/Adapter/Sentry.php b/src/Logger/Adapter/Sentry.php index b717aa7..29c9457 100644 --- a/src/Logger/Adapter/Sentry.php +++ b/src/Logger/Adapter/Sentry.php @@ -61,8 +61,6 @@ public static function getName(): string * * @param Log $log * @return int - * - * @throws Exception */ public function push(Log $log): int { @@ -156,7 +154,7 @@ public function push(Log $log): int $error = \curl_error($ch); if ($response >= 400 || $response === 0) { - throw new Exception("Log could not be pushed with status code {$response}: {$result} ({$error})"); + error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); } \curl_close($ch); diff --git a/tests/e2e/Adapter/AppSignalTest.php b/tests/e2e/Adapter/AppSignalTest.php index 2db4752..a486acd 100644 --- a/tests/e2e/Adapter/AppSignalTest.php +++ b/tests/e2e/Adapter/AppSignalTest.php @@ -14,5 +14,6 @@ protected function setUp(): void parent::setUp(); $appSignalKey = \getenv('TEST_APPSIGNAL_KEY'); $this->adapter = new AppSignal($appSignalKey ? $appSignalKey : ''); + $this->invalidAdapter = new AppSignal(''); } } diff --git a/tests/e2e/Adapter/LogOwlTest.php b/tests/e2e/Adapter/LogOwlTest.php index 541b475..97a2036 100644 --- a/tests/e2e/Adapter/LogOwlTest.php +++ b/tests/e2e/Adapter/LogOwlTest.php @@ -12,5 +12,6 @@ protected function setUp(): void parent::setUp(); $logOwlKey = \getenv('TEST_LOGOWL_KEY'); $this->adapter = new LogOwl($logOwlKey ? $logOwlKey : ''); + $this->invalidAdapter = new LogOwl(''); } } diff --git a/tests/e2e/Adapter/RaygunTest.php b/tests/e2e/Adapter/RaygunTest.php index ddcc770..605de99 100644 --- a/tests/e2e/Adapter/RaygunTest.php +++ b/tests/e2e/Adapter/RaygunTest.php @@ -12,6 +12,7 @@ protected function setUp(): void parent::setUp(); $raygunKey = \getenv('TEST_RAYGUN_KEY'); $this->adapter = new Raygun($raygunKey ? $raygunKey : ''); + $this->invalidAdapter = new Raygun(''); $this->expected = 202; } } diff --git a/tests/e2e/Adapter/SentryTest.php b/tests/e2e/Adapter/SentryTest.php index 2b3936c..4ffc3ea 100644 --- a/tests/e2e/Adapter/SentryTest.php +++ b/tests/e2e/Adapter/SentryTest.php @@ -19,5 +19,6 @@ protected function setUp(): void $url = $scheme.'://'.$host; $this->adapter = new Sentry($path, $user, $url); + $this->invalidAdapter = new Sentry('', '', ''); } } diff --git a/tests/e2e/AdapterBase.php b/tests/e2e/AdapterBase.php index beb2e10..fa96ccb 100755 --- a/tests/e2e/AdapterBase.php +++ b/tests/e2e/AdapterBase.php @@ -14,6 +14,7 @@ abstract class AdapterBase extends TestCase protected ?Log $log = null; protected ?Adapter $adapter = null; + protected ?Adapter $invalidAdapter = null; protected int $expected = 200; @@ -86,4 +87,15 @@ public function testSampler(): void $this->assertGreaterThan(85, $zeroPercentage); } + + public function testAdapterFailure(): void + { + $logger = new Logger($this->invalidAdapter); + + // Should return an error status code without throwing + $response = $logger->addLog($this->log); + + // Should return 401, 403, or 500 depending on the service + $this->assertContains($response, [401, 403, 500]); + } } From ee43327b866a58e94774c6d6ef70e67505c9fb7e Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:42:20 +0000 Subject: [PATCH 02/12] fix: logowl tests --- tests/e2e/Adapter/LogOwlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/LogOwlTest.php b/tests/e2e/Adapter/LogOwlTest.php index 97a2036..4c64d7f 100644 --- a/tests/e2e/Adapter/LogOwlTest.php +++ b/tests/e2e/Adapter/LogOwlTest.php @@ -12,6 +12,6 @@ protected function setUp(): void parent::setUp(); $logOwlKey = \getenv('TEST_LOGOWL_KEY'); $this->adapter = new LogOwl($logOwlKey ? $logOwlKey : ''); - $this->invalidAdapter = new LogOwl(''); + $this->invalidAdapter = new LogOwl('abc'); } } From 5bf539bbf390b70491bd110f7b065925b377c048 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:42:33 +0000 Subject: [PATCH 03/12] chore: fmt --- src/Logger/Adapter/AppSignal.php | 1 - src/Logger/Adapter/LogOwl.php | 1 - src/Logger/Adapter/Raygun.php | 1 - tests/e2e/AdapterBase.php | 5 +++-- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Logger/Adapter/AppSignal.php b/src/Logger/Adapter/AppSignal.php index fade51c..e1cb9a4 100644 --- a/src/Logger/Adapter/AppSignal.php +++ b/src/Logger/Adapter/AppSignal.php @@ -2,7 +2,6 @@ namespace Utopia\Logger\Adapter; -use Exception; use Utopia\Logger\Adapter; use Utopia\Logger\Log; use Utopia\Logger\Logger; diff --git a/src/Logger/Adapter/LogOwl.php b/src/Logger/Adapter/LogOwl.php index 6db59a1..ec42529 100644 --- a/src/Logger/Adapter/LogOwl.php +++ b/src/Logger/Adapter/LogOwl.php @@ -2,7 +2,6 @@ namespace Utopia\Logger\Adapter; -use Exception; use Utopia\Logger\Adapter; use Utopia\Logger\Log; use Utopia\Logger\Logger; diff --git a/src/Logger/Adapter/Raygun.php b/src/Logger/Adapter/Raygun.php index 6daf8ad..b1c5b53 100644 --- a/src/Logger/Adapter/Raygun.php +++ b/src/Logger/Adapter/Raygun.php @@ -2,7 +2,6 @@ namespace Utopia\Logger\Adapter; -use Exception; use Utopia\Logger\Adapter; use Utopia\Logger\Log; use Utopia\Logger\Logger; diff --git a/tests/e2e/AdapterBase.php b/tests/e2e/AdapterBase.php index fa96ccb..208dfbd 100755 --- a/tests/e2e/AdapterBase.php +++ b/tests/e2e/AdapterBase.php @@ -14,6 +14,7 @@ abstract class AdapterBase extends TestCase protected ?Log $log = null; protected ?Adapter $adapter = null; + protected ?Adapter $invalidAdapter = null; protected int $expected = 200; @@ -91,10 +92,10 @@ public function testSampler(): void public function testAdapterFailure(): void { $logger = new Logger($this->invalidAdapter); - + // Should return an error status code without throwing $response = $logger->addLog($this->log); - + // Should return 401, 403, or 500 depending on the service $this->assertContains($response, [401, 403, 500]); } From a20a788c337ab703205d38e5c65de0fb2bfb799c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:43:13 +0000 Subject: [PATCH 04/12] chore: composer update --- composer.lock | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/composer.lock b/composer.lock index 771153c..3ead8a5 100644 --- a/composer.lock +++ b/composer.lock @@ -478,12 +478,12 @@ "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "0dfb69d79d0964b8a80bfa92c07f50e3e8d73542" + "reference": "acc12399c90611e3cb478d0ec72f2c2ebbc429d1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/0dfb69d79d0964b8a80bfa92c07f50e3e8d73542", - "reference": "0dfb69d79d0964b8a80bfa92c07f50e3e8d73542", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/acc12399c90611e3cb478d0ec72f2c2ebbc429d1", + "reference": "acc12399c90611e3cb478d0ec72f2c2ebbc429d1", "shasum": "" }, "require": { @@ -541,7 +541,7 @@ "type": "tidelift" } ], - "time": "2024-06-20T19:34:15+00:00" + "time": "2024-09-30T20:32:32+00:00" }, { "name": "felixfbecker/advanced-json-rpc", @@ -641,7 +641,7 @@ ], "support": { "issues": "https://github.com/felixfbecker/php-language-server-protocol/issues", - "source": "https://github.com/felixfbecker/php-language-server-protocol/tree/master" + "source": "https://github.com/felixfbecker/php-language-server-protocol/tree/v1.5.3" }, "time": "2024-04-30T00:40:11+00:00" }, @@ -829,12 +829,12 @@ "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "0ed4c8949a32986043e977dbe14776c14d644c45" + "reference": "715f4d25e225bc47b293a8b997fe6ce99bf987d2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/0ed4c8949a32986043e977dbe14776c14d644c45", - "reference": "0ed4c8949a32986043e977dbe14776c14d644c45", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/715f4d25e225bc47b293a8b997fe6ce99bf987d2", + "reference": "715f4d25e225bc47b293a8b997fe6ce99bf987d2", "shasum": "" }, "require": { @@ -843,7 +843,7 @@ }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" }, "bin": [ "bin/php-parse" @@ -877,7 +877,7 @@ "issues": "https://github.com/nikic/PHP-Parser/issues", "source": "https://github.com/nikic/PHP-Parser/tree/4.x" }, - "time": "2024-09-17T19:36:00+00:00" + "time": "2024-09-29T15:01:53+00:00" }, { "name": "openlss/lib-array2xml", @@ -1230,16 +1230,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.30.1", + "version": "1.32.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "51b95ec8670af41009e2b2b56873bad96682413e" + "reference": "6ca22b154efdd9e3c68c56f5d94670920a1c19a4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/51b95ec8670af41009e2b2b56873bad96682413e", - "reference": "51b95ec8670af41009e2b2b56873bad96682413e", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/6ca22b154efdd9e3c68c56f5d94670920a1c19a4", + "reference": "6ca22b154efdd9e3c68c56f5d94670920a1c19a4", "shasum": "" }, "require": { @@ -1271,9 +1271,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.30.1" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.32.0" }, - "time": "2024-09-07T20:13:05+00:00" + "time": "2024-09-26T07:23:32+00:00" }, { "name": "phpstan/phpstan", @@ -1659,12 +1659,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa" + "reference": "857d98630470c48a353f673060dec98e8c0686b2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa", - "reference": "de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/857d98630470c48a353f673060dec98e8c0686b2", + "reference": "857d98630470c48a353f673060dec98e8c0686b2", "shasum": "" }, "require": { @@ -1754,7 +1754,7 @@ "type": "tidelift" } ], - "time": "2024-09-19T10:50:18+00:00" + "time": "2024-10-08T05:58:25+00:00" }, { "name": "psr/container", @@ -2830,12 +2830,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "5b5a0aa66e3296e303e22490f90f521551835a83" + "reference": "108d436c2af470858bdaba3257baab3a74172017" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/5b5a0aa66e3296e303e22490f90f521551835a83", - "reference": "5b5a0aa66e3296e303e22490f90f521551835a83", + "url": "https://api.github.com/repos/symfony/console/zipball/108d436c2af470858bdaba3257baab3a74172017", + "reference": "108d436c2af470858bdaba3257baab3a74172017", "shasum": "" }, "require": { @@ -2921,7 +2921,7 @@ "type": "tidelift" } ], - "time": "2024-09-20T07:56:40+00:00" + "time": "2024-10-08T07:27:17+00:00" }, { "name": "symfony/deprecation-contracts", @@ -3561,12 +3561,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "f8a1ccebd0997e16112dfecfd74220b78e5b284b" + "reference": "38371c60c71c72b3d64d8d76f6b1bb81a2cc3627" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/f8a1ccebd0997e16112dfecfd74220b78e5b284b", - "reference": "f8a1ccebd0997e16112dfecfd74220b78e5b284b", + "url": "https://api.github.com/repos/symfony/string/zipball/38371c60c71c72b3d64d8d76f6b1bb81a2cc3627", + "reference": "38371c60c71c72b3d64d8d76f6b1bb81a2cc3627", "shasum": "" }, "require": { @@ -3639,7 +3639,7 @@ "type": "tidelift" } ], - "time": "2024-09-20T08:15:52+00:00" + "time": "2024-09-25T14:18:03+00:00" }, { "name": "theseer/tokenizer", From 7e29744d4a6f605bdff52c1ed3764265c0026f52 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:45:31 +0000 Subject: [PATCH 05/12] fix: logowltest --- tests/e2e/Adapter/LogOwlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/LogOwlTest.php b/tests/e2e/Adapter/LogOwlTest.php index 4c64d7f..bc7b6df 100644 --- a/tests/e2e/Adapter/LogOwlTest.php +++ b/tests/e2e/Adapter/LogOwlTest.php @@ -12,6 +12,6 @@ protected function setUp(): void parent::setUp(); $logOwlKey = \getenv('TEST_LOGOWL_KEY'); $this->adapter = new LogOwl($logOwlKey ? $logOwlKey : ''); - $this->invalidAdapter = new LogOwl('abc'); + $this->invalidAdapter = new LogOwl('abc', 'https://api.invalid.io/logging/'); } } From 63004e9b83ed1375aa09cd2fdc9be224e5467371 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:46:26 +0000 Subject: [PATCH 06/12] fix: codeql --- .github/workflows/code-ql-analysis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-ql-analysis.yml b/.github/workflows/code-ql-analysis.yml index 734db04..3417c30 100644 --- a/.github/workflows/code-ql-analysis.yml +++ b/.github/workflows/code-ql-analysis.yml @@ -16,5 +16,5 @@ jobs: - name: Run CodeQL run: | - docker run --rm -v $PWD:/app composer sh -c \ + docker run --rm -v $PWD:/app composer:2.6 sh -c \ "composer install --profile --ignore-platform-reqs && composer check" \ No newline at end of file From fb4214e4d7aefaf1372b44eb9efae2b85dc61240 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:47:55 +0000 Subject: [PATCH 07/12] fix: codeql --- tests/e2e/AdapterBase.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/e2e/AdapterBase.php b/tests/e2e/AdapterBase.php index 208dfbd..8ab3719 100755 --- a/tests/e2e/AdapterBase.php +++ b/tests/e2e/AdapterBase.php @@ -91,6 +91,10 @@ public function testSampler(): void public function testAdapterFailure(): void { + if (empty($this->log) || empty($this->invalidAdapter)) { + throw new \Exception('Log or adapter not set'); + } + $logger = new Logger($this->invalidAdapter); // Should return an error status code without throwing From be2533124502ce58d28580b1ae275ae54445d2ac Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:56:12 +0000 Subject: [PATCH 08/12] chore: more robust approach --- src/Logger/Adapter/AppSignal.php | 21 ++++++++++++--------- src/Logger/Adapter/LogOwl.php | 21 ++++++++++++--------- src/Logger/Adapter/Raygun.php | 21 ++++++++++++--------- src/Logger/Adapter/Sentry.php | 21 ++++++++++++--------- tests/e2e/AdapterBase.php | 6 +++--- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/Logger/Adapter/AppSignal.php b/src/Logger/Adapter/AppSignal.php index e1cb9a4..edc2d6e 100644 --- a/src/Logger/Adapter/AppSignal.php +++ b/src/Logger/Adapter/AppSignal.php @@ -122,17 +122,20 @@ public function push(Log $log): int \curl_setopt_array($ch, $optArray); // execute request and get response - $result = \curl_exec($ch); - $response = \curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $error = \curl_error($ch); - - if ($response >= 400 || $response === 0) { - error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); - } - + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); + $curlError = \curl_error($ch); \curl_close($ch); - return $response; + if ($curlError !== CURLE_OK) { + error_log("AppSignal push failed with curl error ({$curlError}): {$response}"); + return 500; + } + + if ($httpCode >= 400 || $httpCode === 0) { + error_log("AppSignal push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; + } } public function getSupportedTypes(): array diff --git a/src/Logger/Adapter/LogOwl.php b/src/Logger/Adapter/LogOwl.php index ec42529..1a3e6af 100644 --- a/src/Logger/Adapter/LogOwl.php +++ b/src/Logger/Adapter/LogOwl.php @@ -141,17 +141,20 @@ public function push(Log $log): int \curl_setopt_array($ch, $optArray); // execute request and get response - $result = curl_exec($ch); - $response = curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $error = \curl_error($ch); - - if ($response >= 400 || $response === 0) { - error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); - } - + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); + $curlError = \curl_error($ch); \curl_close($ch); - return $response; + if ($curlError !== CURLE_OK) { + error_log("LogOwl push failed with curl error ({$curlError}): {$response}"); + return 500; + } + + if ($httpCode >= 400 || $httpCode === 0) { + error_log("LogOwl push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; + } } public function getSupportedTypes(): array diff --git a/src/Logger/Adapter/Raygun.php b/src/Logger/Adapter/Raygun.php index b1c5b53..78dbcb0 100644 --- a/src/Logger/Adapter/Raygun.php +++ b/src/Logger/Adapter/Raygun.php @@ -107,17 +107,20 @@ public function push(Log $log): int \curl_setopt_array($ch, $optArray); // execute request and get response - $result = \curl_exec($ch); - $response = \curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $error = \curl_error($ch); - - if ($response >= 400 || $response === 0) { - error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); - } - + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); + $curlError = \curl_error($ch); \curl_close($ch); - return $response; + if ($curlError !== CURLE_OK) { + error_log("Raygun push failed with curl error ({$curlError}): {$response}"); + return 500; + } + + if ($httpCode >= 400 || $httpCode === 0) { + error_log("Raygun push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; + } } public function getSupportedTypes(): array diff --git a/src/Logger/Adapter/Sentry.php b/src/Logger/Adapter/Sentry.php index 29c9457..e6cd830 100644 --- a/src/Logger/Adapter/Sentry.php +++ b/src/Logger/Adapter/Sentry.php @@ -149,17 +149,20 @@ public function push(Log $log): int \curl_setopt_array($ch, $optArray); // execute request and get response - $result = curl_exec($ch); - $response = curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $error = \curl_error($ch); - - if ($response >= 400 || $response === 0) { - error_log("Log could not be pushed with status code {$response}: {$result} ({$error})"); - } - + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); + $curlError = \curl_error($ch); \curl_close($ch); - return $response; + if ($curlError !== CURLE_OK) { + error_log("Sentry push failed with curl error ({$curlError}): {$response}"); + return 500; + } + + if ($httpCode >= 400 || $httpCode === 0) { + error_log("Sentry push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; + } } public function getSupportedTypes(): array diff --git a/tests/e2e/AdapterBase.php b/tests/e2e/AdapterBase.php index 8ab3719..97a6e9d 100755 --- a/tests/e2e/AdapterBase.php +++ b/tests/e2e/AdapterBase.php @@ -98,9 +98,9 @@ public function testAdapterFailure(): void $logger = new Logger($this->invalidAdapter); // Should return an error status code without throwing - $response = $logger->addLog($this->log); + $statusCode = $logger->addLog($this->log); - // Should return 401, 403, or 500 depending on the service - $this->assertContains($response, [401, 403, 500]); + // Should return > 400 status code + $this->assertGreaterThan(400, $statusCode); } } From 9fc2f6cc9452d74b1e60d589cad5a73cd9ca9cd1 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 17:59:06 +0000 Subject: [PATCH 09/12] chore: fmt --- .github/workflows/linter.yml | 2 +- src/Logger/Adapter/AppSignal.php | 6 +++++- src/Logger/Adapter/LogOwl.php | 6 +++++- src/Logger/Adapter/Raygun.php | 6 +++++- src/Logger/Adapter/Sentry.php | 6 +++++- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index bca79de..f190eab 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -16,5 +16,5 @@ jobs: - name: Run Linter run: | - docker run --rm -v $PWD:/app composer sh -c \ + docker run --rm -v $PWD:/app composer:2.6 sh -c \ "composer install --profile --ignore-platform-reqs && composer lint" diff --git a/src/Logger/Adapter/AppSignal.php b/src/Logger/Adapter/AppSignal.php index edc2d6e..f859de3 100644 --- a/src/Logger/Adapter/AppSignal.php +++ b/src/Logger/Adapter/AppSignal.php @@ -129,13 +129,17 @@ public function push(Log $log): int if ($curlError !== CURLE_OK) { error_log("AppSignal push failed with curl error ({$curlError}): {$response}"); + return 500; } - + if ($httpCode >= 400 || $httpCode === 0) { error_log("AppSignal push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; } + + return $httpCode; } public function getSupportedTypes(): array diff --git a/src/Logger/Adapter/LogOwl.php b/src/Logger/Adapter/LogOwl.php index 1a3e6af..d7d92db 100644 --- a/src/Logger/Adapter/LogOwl.php +++ b/src/Logger/Adapter/LogOwl.php @@ -148,13 +148,17 @@ public function push(Log $log): int if ($curlError !== CURLE_OK) { error_log("LogOwl push failed with curl error ({$curlError}): {$response}"); + return 500; } - + if ($httpCode >= 400 || $httpCode === 0) { error_log("LogOwl push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; } + + return $httpCode; } public function getSupportedTypes(): array diff --git a/src/Logger/Adapter/Raygun.php b/src/Logger/Adapter/Raygun.php index 78dbcb0..c5309c6 100644 --- a/src/Logger/Adapter/Raygun.php +++ b/src/Logger/Adapter/Raygun.php @@ -114,13 +114,17 @@ public function push(Log $log): int if ($curlError !== CURLE_OK) { error_log("Raygun push failed with curl error ({$curlError}): {$response}"); + return 500; } - + if ($httpCode >= 400 || $httpCode === 0) { error_log("Raygun push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; } + + return $httpCode; } public function getSupportedTypes(): array diff --git a/src/Logger/Adapter/Sentry.php b/src/Logger/Adapter/Sentry.php index e6cd830..4a0ff47 100644 --- a/src/Logger/Adapter/Sentry.php +++ b/src/Logger/Adapter/Sentry.php @@ -156,13 +156,17 @@ public function push(Log $log): int if ($curlError !== CURLE_OK) { error_log("Sentry push failed with curl error ({$curlError}): {$response}"); + return 500; } - + if ($httpCode >= 400 || $httpCode === 0) { error_log("Sentry push failed with status code {$httpCode}: {$curlError} ({$response})"); + return $httpCode ?: 500; } + + return $httpCode; } public function getSupportedTypes(): array From 8aa6c0f8e5d10944b93af3356a5c217546c7f82a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 18:09:33 +0000 Subject: [PATCH 10/12] chore: fmt --- src/Logger/Adapter/AppSignal.php | 8 +++----- src/Logger/Adapter/LogOwl.php | 9 ++++----- src/Logger/Adapter/Raygun.php | 8 +++----- src/Logger/Adapter/Sentry.php | 8 +++----- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/Logger/Adapter/AppSignal.php b/src/Logger/Adapter/AppSignal.php index f859de3..dcb3632 100644 --- a/src/Logger/Adapter/AppSignal.php +++ b/src/Logger/Adapter/AppSignal.php @@ -124,19 +124,17 @@ public function push(Log $log): int // execute request and get response $response = curl_exec($ch); $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $curlError = \curl_error($ch); + $curlError = \curl_errno($ch); \curl_close($ch); - if ($curlError !== CURLE_OK) { + if ($curlError !== CURLE_OK || $httpCode === 0) { error_log("AppSignal push failed with curl error ({$curlError}): {$response}"); return 500; } - if ($httpCode >= 400 || $httpCode === 0) { + if ($httpCode >= 400) { error_log("AppSignal push failed with status code {$httpCode}: {$curlError} ({$response})"); - - return $httpCode ?: 500; } return $httpCode; diff --git a/src/Logger/Adapter/LogOwl.php b/src/Logger/Adapter/LogOwl.php index d7d92db..e0770c7 100644 --- a/src/Logger/Adapter/LogOwl.php +++ b/src/Logger/Adapter/LogOwl.php @@ -140,22 +140,21 @@ public function push(Log $log): int // apply those options \curl_setopt_array($ch, $optArray); + // execute request and get response // execute request and get response $response = curl_exec($ch); $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $curlError = \curl_error($ch); + $curlError = \curl_errno($ch); \curl_close($ch); - if ($curlError !== CURLE_OK) { + if ($curlError !== CURLE_OK || $httpCode === 0) { error_log("LogOwl push failed with curl error ({$curlError}): {$response}"); return 500; } - if ($httpCode >= 400 || $httpCode === 0) { + if ($httpCode >= 400) { error_log("LogOwl push failed with status code {$httpCode}: {$curlError} ({$response})"); - - return $httpCode ?: 500; } return $httpCode; diff --git a/src/Logger/Adapter/Raygun.php b/src/Logger/Adapter/Raygun.php index c5309c6..311da6d 100644 --- a/src/Logger/Adapter/Raygun.php +++ b/src/Logger/Adapter/Raygun.php @@ -109,19 +109,17 @@ public function push(Log $log): int // execute request and get response $response = curl_exec($ch); $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $curlError = \curl_error($ch); + $curlError = \curl_errno($ch); \curl_close($ch); - if ($curlError !== CURLE_OK) { + if ($curlError !== CURLE_OK || $httpCode === 0) { error_log("Raygun push failed with curl error ({$curlError}): {$response}"); return 500; } - if ($httpCode >= 400 || $httpCode === 0) { + if ($httpCode >= 400) { error_log("Raygun push failed with status code {$httpCode}: {$curlError} ({$response})"); - - return $httpCode ?: 500; } return $httpCode; diff --git a/src/Logger/Adapter/Sentry.php b/src/Logger/Adapter/Sentry.php index 4a0ff47..e61b73e 100644 --- a/src/Logger/Adapter/Sentry.php +++ b/src/Logger/Adapter/Sentry.php @@ -151,19 +151,17 @@ public function push(Log $log): int // execute request and get response $response = curl_exec($ch); $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); - $curlError = \curl_error($ch); + $curlError = \curl_errno($ch); \curl_close($ch); - if ($curlError !== CURLE_OK) { + if ($curlError !== CURLE_OK || $httpCode === 0) { error_log("Sentry push failed with curl error ({$curlError}): {$response}"); return 500; } - if ($httpCode >= 400 || $httpCode === 0) { + if ($httpCode >= 400) { error_log("Sentry push failed with status code {$httpCode}: {$curlError} ({$response})"); - - return $httpCode ?: 500; } return $httpCode; From bab55f2d8f49a4790cce9b89cf9716138ad06dde Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 18:13:01 +0000 Subject: [PATCH 11/12] chore: remove dup line --- src/Logger/Adapter/LogOwl.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Logger/Adapter/LogOwl.php b/src/Logger/Adapter/LogOwl.php index e0770c7..ef84be1 100644 --- a/src/Logger/Adapter/LogOwl.php +++ b/src/Logger/Adapter/LogOwl.php @@ -140,7 +140,6 @@ public function push(Log $log): int // apply those options \curl_setopt_array($ch, $optArray); - // execute request and get response // execute request and get response $response = curl_exec($ch); $httpCode = curl_getinfo($ch, \CURLINFO_HTTP_CODE); From 569fa05b0af6bf3440303df8009761fa71409b34 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 13 Dec 2024 20:48:53 +0000 Subject: [PATCH 12/12] chore: add DS_store to gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8fc95d5..748576b 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ /.idea/ .env -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.DS_Store \ No newline at end of file