Skip to content

Commit b5e870b

Browse files
committed
Merge branch 'master' of github.com:BinarCode/laravel-developer into slack_channels
2 parents afb83cd + 719585f commit b5e870b

File tree

7 files changed

+91
-2
lines changed

7 files changed

+91
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ devLog('Called Fedex API with:', $payload)
369369

370370
Now you will have an entry in your database table with the payload and the associated name.
371371

372+
373+
## Telescope support
374+
375+
Package will automaticaly store exceptions into [telescope](https://laravel.com/docs/telescope).
376+
377+
Simply install telescope and mark the `developer.telescope` on `true`.
378+
379+
Telescope exceptions will be automatically stored on each `slack($e)->persist()` call, or custom using the `telescopeException($e, 'custom message')` helper.
380+
372381
## Testing
373382

374383
``` bash

config/laravel-developer.php renamed to config/developer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@
2323
'auth' => [
2424
'bearer' => 'testing',
2525
],
26+
27+
/**
28+
* If this is true, you should ensure you have telescope installed and the package will store exceptions in the telescope as well.
29+
*/
30+
'telescope' => env('DEV_TELESCOPE'),
2631
];

src/LaravelDeveloperServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function boot()
1212
{
1313
if ($this->app->runningInConsole()) {
1414
$this->publishes([
15-
__DIR__ . '/../config/laravel-developer.php' => config_path('developer.php'),
15+
__DIR__ . '/../config/developer.php' => config_path('developer.php'),
1616
], 'developer-config');
1717

1818
$migrationFileName = 'create_laravel_developer_table.php';
@@ -30,7 +30,7 @@ public function boot()
3030

3131
public function register()
3232
{
33-
$this->mergeConfigFrom(__DIR__ . '/../config/laravel-developer.php', 'developer');
33+
$this->mergeConfigFrom(__DIR__ . '/../config/developer.php', 'developer');
3434
$this->registerMacros();
3535
}
3636

src/Notifications/Slack.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Binarcode\LaravelDeveloper\Dtos\DevNotificationDto;
66
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
7+
use Binarcode\LaravelDeveloper\Telescope\TelescopeDev;
8+
use Binarcode\LaravelDeveloper\Telescope\TelescopeException;
79
use Illuminate\Support\Collection;
810
use Illuminate\Support\Facades\Notification as NotificationFacade;
911
use Throwable;
@@ -21,6 +23,8 @@ class Slack
2123

2224
protected ?string $channel = null;
2325

26+
protected bool $telescope = true;
27+
2428
public function __construct($args = null)
2529
{
2630
$this->queue = collect($args)->flatten();
@@ -66,6 +70,10 @@ private function send($item)
6670
$dto = DevNotificationDto::makeFromExceptionLog(
6771
tap(ExceptionLog::makeFromException($item), fn (ExceptionLog $log) => $log->save())
6872
);
73+
74+
if ($this->telescope && TelescopeDev::allow()) {
75+
TelescopeException::record($item);
76+
}
6977
} else {
7078
$dto = DevNotificationDto::makeFromException($item);
7179
}
@@ -97,6 +105,13 @@ private function send($item)
97105
return $this;
98106
}
99107

108+
public function withoutTelescope(): self
109+
{
110+
$this->telescope = false;
111+
112+
return $this;
113+
}
114+
100115
public static function notifyUsing(?callable $notificator)
101116
{
102117
Slack::$notifyUsing = $notificator;

src/Telescope/TelescopeDev.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Telescope;
4+
5+
class TelescopeDev
6+
{
7+
public static function allow(): bool
8+
{
9+
if (!class_exists('Laravel\\Telescope\\Telescope')) {
10+
logger('Telescope is not installed.');
11+
return false;
12+
}
13+
14+
return config('developer.telescope');
15+
}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Telescope;
4+
5+
use Illuminate\Support\Arr;
6+
use Laravel\Telescope\ExceptionContext;
7+
use Laravel\Telescope\IncomingExceptionEntry;
8+
use Laravel\Telescope\Telescope;
9+
use Throwable;
10+
11+
class TelescopeException
12+
{
13+
public static function record(Throwable $exception, $message = null): void
14+
{
15+
$trace = collect($exception->getTrace())->map(function ($item) {
16+
return Arr::only($item, ['file', 'line']);
17+
})->toArray();
18+
19+
try {
20+
Telescope::recordException(
21+
IncomingExceptionEntry::make($exception, [
22+
'class' => get_class($exception),
23+
'file' => $exception->getFile(),
24+
'line' => $exception->getLine(),
25+
'message' => $message ?? $exception->getMessage(),
26+
'context' => null,
27+
'trace' => $trace,
28+
'line_preview' => ExceptionContext::get($exception),
29+
])
30+
);
31+
} catch (Throwable $e) {
32+
}
33+
}
34+
}

src/helpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Binarcode\LaravelDeveloper\Notifications\Slack;
55
use Binarcode\LaravelDeveloper\Profiling\ServerMemory;
66
use Binarcode\LaravelDeveloper\Profiling\ServerTiming;
7+
use Binarcode\LaravelDeveloper\Telescope\TelescopeException;
78

89
if (! function_exists('measure_memory')) {
910
function measure_memory($callable = null, string $key = 'action', string $unit = 'mb')
@@ -54,3 +55,12 @@ function devLog(...$args): DevLog
5455
return DevLog::make(...$args);
5556
}
5657
}
58+
59+
if (! function_exists('telescopeException')) {
60+
function telescopeException(Throwable $exception, $message = null): void
61+
{
62+
if (config('developer.interacts_telescope')) {
63+
TelescopeException::record($exception, $message);
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)