Skip to content

Commit

Permalink
+ Release 5.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmondDantes committed Oct 27, 2024
1 parent fb0c64f commit ab673c6
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 30 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [5.1.0] - 2024-10-27

### Added

- Added interface `Throwable` to `BaseExceptionInterface`.
- Class `Error` was extended from `ErrorException`.
- Added `MonologProcessor` for Monolog.

### Fixed

- Fixed the PHPDoc type hints for `PHPStan`.
- Fixed the `ResourceException` class and sibling classes.

### Changed

- Rename method `BaseExceptionInterface::template()` to `BaseExceptionInterface::getTemplate()`.
- Update documentation (English and Russian).
- Exceptions are no longer added to the `exception registry`.
This functionality is disabled by default.

### Removed

27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ BaseException [![PHP Composer](https://github.com/EdmondDantes/amphp-pool/action

=============

Base Exception Library for PHP 8.2+
(The latest version: 5.0.0)
Base Exception Library for PHP 8.3+
(The latest version: 5.1.0)

Missions:

Expand Down Expand Up @@ -87,6 +87,29 @@ List of parameters:

```

## Monolog integration

The processor for `Monolog` allows populating exception metadata in the log.

```php
<?php

use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use IfCastle\Exceptions\LoggableException;
use IfCastle\Exceptions\MonologProcessor;

$log = new Logger('name');
$log->pushProcessor(new MonologProcessor());

// Put some records to the log
$log->error(
'this is a loggable exception',
['exception' => new LoggableException('this is a loggable exception')]
);
```

## Exception Container

```php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"phpunit/phpunit": "^11.2",
"rector/rector": "^1.2",
"ifcastle/codestyle": "^0.1.7",
"phpstan/phpstan": "^1.12"
"phpstan/phpstan": "^1.12",
"monolog/monolog": "^3.7"
},
"scripts": {
"test": "vendor/bin/phpunit --coverage-clover=coverage/clover.xml --coverage-filter=src ./tests",
Expand Down
103 changes: 102 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function __construct(BaseExceptionInterface|\Throwable|array|string $exce
* Returns template message.
*/
#[\Override]
public function template(): string
public function getTemplate(): string
{
return $this->template;
}
Expand Down Expand Up @@ -413,14 +413,14 @@ public function toArray(): array
}

// override the exception message if the template was defined
$message = $this->template() !== '' ? $this->getExceptionData()['message'] ?? '' : $this->getMessage();
$message = $this->getTemplate() !== '' ? $this->getExceptionData()['message'] ?? '' : $this->getMessage();

return
[
'type' => static::class,
'source' => $this->getSource(),
'message' => $message,
'template' => $this->template(),
'template' => $this->getTemplate(),
'tags' => $this->getTags(),
'code' => $this->getCode(),
'data' => $this->getExceptionData(),
Expand Down
2 changes: 1 addition & 1 deletion src/BaseExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getTraceAsString(): string;
/**
* Template message.
*/
public function template(): string;
public function getTemplate(): string;

/**
* @return string[]
Expand Down
2 changes: 1 addition & 1 deletion src/Errors/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function appendData(array $data): static
}

#[\Override]
public function template(): string
public function getTemplate(): string
{
return '';
}
Expand Down
27 changes: 27 additions & 0 deletions src/MonologProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace IfCastle\Exceptions;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

final class MonologProcessor implements ProcessorInterface
{
#[\Override]
public function __invoke(LogRecord $record)
{
if(false === $record->context['exception'] instanceof BaseExceptionInterface) {
return $record;
}

$exception = $record->context['exception'];

$record->extra['exception_template'] = $exception->getTemplate();
$record->extra['exception_data'] = $exception->getExceptionData();
$record->extra['exception_debug'] = $exception->getDebugData();
$record->extra['tags'] = array_merge($record->extra['tags'] ?? [], $exception->getTags());

return $record;
}
}
1 change: 0 additions & 1 deletion src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Registry
{
/**
* Flag for global handler.
* @var bool
*/
public static bool $isActive = false;

Expand Down
6 changes: 5 additions & 1 deletion src/Resource/FileSystem/FileCloseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
class FileCloseError extends ResourceCloseError implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;


/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileLockFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileLockFailed extends ResourceLockFailed implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileNotExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileNotExists extends ResourceNotExists implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileNotReadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileNotReadable extends ResourceNotReadable implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, string $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileNotWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileNotWritable extends ResourceNotWritable implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, string $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileOpenError.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileOpenError extends ResourceOpenError implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, string $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileReadError.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileReadError extends ResourceReadError implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, string $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Resource/FileSystem/FileUnLockFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
class FileUnLockFailed extends ResourceUnLockFailed implements FileSystemExceptionInterface
{
protected string $system = self::SYSTEM;

public function __construct($resource, $type = 'file')

/**
* @param string|object|resource|array<string, scalar|scalar[]> $resource
* @param string $type
*/
public function __construct(mixed $resource, string $type = 'file')
{
parent::__construct($resource, $type);
}
Expand Down
Loading

0 comments on commit ab673c6

Please sign in to comment.