grifart/suppressed-exceptions

添加了对Java中已知的抑制异常的支持。https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions

v0.2.2 2022-03-17 14:18 UTC

This package is auto-updated.

Last update: 2024-09-17 20:05:06 UTC


README

抑制异常对于从不可靠资源中聚合更多异常很有用。

当您想通过一组导致错误的同级异常列表来传达进程失败时,这很有用。

仓库

示例用法

$remoteSources = []; // classes representing unreliable remote sources

$exceptions = [];
foreach ($remoteSoures as $remoteSource) {
	try {
		$remoteSource->fetch(); // unsafe operation
	} catch (FetchingFailed $e) {
		$exceptions[] = $e;
		continue;
	}
}

if (count($exceptions) > 0) {
	$e = new ProcessingFailed();
	$e->addSuppressed(...$exceptions);
	throw $e;
}

您还可以覆盖异常构造函数以提供更好的API

final class EventPropagationFailedException extends \RuntimeException implements \Grifart\SuppressedExceptions\WithSuppressedExceptions
{
	use \Grifart\SuppressedExceptions\SuppressedExceptions;

	public function __construct(\Throwable ...$suppressed)
	{
		parent::__construct('Saving succeeded, but some listeners failed to complete their job. Please check suppressed exceptions for more information.');
		$this->addSuppressed(...$suppressed);
	}
}

使用方法如下

throw new EventPropagationFailedException(...$suppressedExceptions);

待办:调试器中的截图 显示捕获时的外观

更多阅读