Skip to content

Commit a7ba61f

Browse files
committed
fix(support): Improve error handling with rescue function
- Introduce a new `rescue` function for streamlined error handling. - Wrap reporting methods in the `rescue` function to catch exceptions. - Utilize `rescue` in `Channel`, `ExceptionNotifyManager`, and other relevant files for improved exception logging and management.
1 parent cebcc44 commit a7ba61f

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

config/exception-notify.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@
202202
'class' => Guanguans\Notify\Bark\Messages\Message::class,
203203
'title' => AbstractChannel::TITLE_TEMPLATE,
204204
'body' => AbstractChannel::CONTENT_TEMPLATE,
205+
'options' => [
206+
'title' => AbstractChannel::TITLE_TEMPLATE,
207+
'body' => AbstractChannel::CONTENT_TEMPLATE,
208+
],
205209
],
206210
'pipes' => [
207211
LimitLengthPipe::with(4096),

rector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ static function (array $carry, mixed $value, string $name) use ($class): array {
210210
RenameFunctionRector::class,
211211
[
212212
'faker' => 'fake',
213-
'Guanguans\LaravelExceptionNotify\Support\rescue' => 'rescue',
214-
'Guanguans\Notify\Foundation\Support\rescue' => 'rescue',
213+
'Guanguans\Notify\Foundation\Support\rescue' => 'Guanguans\LaravelExceptionNotify\Support\rescue',
215214
'Pest\Faker\fake' => 'fake',
216215
'Pest\Faker\faker' => 'faker',
216+
'rescue' => 'Guanguans\LaravelExceptionNotify\Support\rescue',
217217
'test' => 'it',
218218
] + array_reduce(
219219
[

src/Channels/Channel.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,24 @@ public function __construct(
3838

3939
public function report(\Throwable $throwable): void
4040
{
41-
if ($this->shouldntReport($throwable)) {
42-
return;
43-
}
41+
\Guanguans\LaravelExceptionNotify\Support\rescue(function () use ($throwable): void {
42+
if ($this->shouldntReport($throwable)) {
43+
return;
44+
}
4445

45-
$this->channelContract->report($throwable);
46+
$this->channelContract->report($throwable);
47+
});
4648
}
4749

4850
public function reportContent(string $content): mixed
4951
{
50-
Event::dispatch(new ReportingEvent($this->channelContract, $content));
51-
$result = $this->channelContract->reportContent($content);
52-
Event::dispatch(new ReportedEvent($this->channelContract, $result));
52+
return \Guanguans\LaravelExceptionNotify\Support\rescue(function () use ($content): mixed {
53+
Event::dispatch(new ReportingEvent($this->channelContract, $content));
54+
$result = $this->channelContract->reportContent($content);
55+
Event::dispatch(new ReportedEvent($this->channelContract, $result));
5356

54-
return $result;
57+
return $result;
58+
});
5559
}
5660

5761
public function reporting(mixed $listener): void

src/ExceptionNotifyManager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ public function channel(?string $channel = null): Channel
5353

5454
public function report(\Throwable $throwable): void
5555
{
56-
$this->driver()->report($throwable);
56+
\Guanguans\LaravelExceptionNotify\Support\rescue(function () use ($throwable): void {
57+
$this->driver()->report($throwable);
58+
});
5759
}
5860

5961
public function reportContent(string $content): mixed
6062
{
61-
return $this->driver()->reportContent($content);
63+
return \Guanguans\LaravelExceptionNotify\Support\rescue(fn (): mixed => $this->driver()->reportContent($content));
6264
}
6365

6466
public function getDefaultDriver(): string

src/Support/helpers.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException;
1717
use Illuminate\Support\Arr;
18+
use Illuminate\Support\Facades\Log;
1819

1920
if (!\function_exists('Guanguans\LaravelExceptionNotify\Support\make')) {
2021
/**
@@ -56,6 +57,27 @@ function make(array|string $abstract, array $parameters = []): mixed
5657
}
5758
}
5859

60+
if (!\function_exists('Guanguans\LaravelExceptionNotify\Support\rescue')) {
61+
/**
62+
* @param null|\Closure $rescue
63+
* @param bool|\Closure $log
64+
*
65+
* @see rescue()
66+
*/
67+
function rescue(callable $callback, mixed $rescue = null, mixed $log = true): mixed
68+
{
69+
try {
70+
return $callback();
71+
} catch (\Throwable $throwable) {
72+
if (value($log, $throwable)) {
73+
Log::error($throwable->getMessage(), ['exception' => $throwable]);
74+
}
75+
76+
return value($rescue, $throwable);
77+
}
78+
}
79+
}
80+
5981
if (!\function_exists('Guanguans\LaravelExceptionNotify\Support\env_explode')) {
6082
/**
6183
* @noinspection LaravelFunctionsInspection

0 commit comments

Comments
 (0)