-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #947 from oat-sa/release-15.11.0
Release 15.11.0
- Loading branch information
Showing
6 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
common/oatbox/log/logger/extender/ExceptionContextExtender.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2021 (original work) Open Assessment Technologies SA | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\oatbox\log\logger\extender; | ||
|
||
use Throwable; | ||
|
||
class ExceptionContextExtender implements ContextExtenderInterface | ||
{ | ||
public function extend(array $context): array | ||
{ | ||
if (isset($context[self::CONTEXT_EXCEPTION]) && $context[self::CONTEXT_EXCEPTION] instanceof Throwable) { | ||
$context[self::CONTEXT_EXCEPTION] = $this->buildLogMessage($context[self::CONTEXT_EXCEPTION]); | ||
} | ||
|
||
return $context; | ||
} | ||
|
||
private function buildLogMessage(Throwable $exception): string | ||
{ | ||
$message = $this->createMessage($exception); | ||
|
||
if ($exception->getPrevious()) { | ||
$message = sprintf( | ||
'%s, previous: %s', | ||
$message, | ||
$this->buildLogMessage($exception->getPrevious()) | ||
); | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
private function createMessage(Throwable $exception): string | ||
{ | ||
return sprintf( | ||
'"%s", code: %s, file: "%s", line: %s', | ||
$exception->getMessage(), | ||
$exception->getCode(), | ||
$exception->getFile(), | ||
$exception->getLine() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
test/unit/common/oatbox/log/logger/extender/ExceptionContextExtenderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2021 (original work) Open Assessment Technologies SA; | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\generis\test\unit\common\oatbox\log\logger\extender; | ||
|
||
use Exception; | ||
use Throwable; | ||
use oat\generis\test\TestCase; | ||
use oat\oatbox\log\logger\extender\ContextExtenderInterface; | ||
use oat\oatbox\log\logger\extender\ExceptionContextExtender; | ||
|
||
class ExceptionContextExtenderTest extends TestCase | ||
{ | ||
/** @var ExceptionContextExtender */ | ||
private $sut; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->sut = new ExceptionContextExtender(); | ||
} | ||
|
||
public function testExtend(): void | ||
{ | ||
$this->assertSame( | ||
[ | ||
ContextExtenderInterface::CONTEXT_EXCEPTION => '"Last error", code: 200, file: "' | ||
. __FILE__ . '", line: 67, previous: "Original error", code: 100, file: "' | ||
. __FILE__ . '", line: 70', | ||
], | ||
$this->sut->extend( | ||
[ | ||
ContextExtenderInterface::CONTEXT_EXCEPTION => $this->createException(), | ||
] | ||
) | ||
); | ||
} | ||
|
||
public function testExtendWithoutException(): void | ||
{ | ||
$this->assertSame( | ||
[], | ||
$this->sut->extend([]) | ||
); | ||
} | ||
|
||
private function createException(): Throwable | ||
{ | ||
return new Exception( | ||
'Last error', | ||
200, | ||
new Exception( | ||
'Original error', | ||
100 | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters