Skip to content

Commit 2121981

Browse files
authored
Merge pull request #7 from BinarCode/slack-reach
Adding more information to slack exceptions.
2 parents 61014f0 + df442ca commit 2121981

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

database/migrations/create_laravel_developer_table.php.stub

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class CreateLaravelDeveloperTable extends Migration
1414
$table->string('name')->nullable();
1515
$table->text('payload')->nullable();
1616
$table->text('exception')->nullable();
17+
$table->text('previous')->nullable();
18+
$table->string('file')->nullable();
19+
$table->string('line')->nullable();
20+
$table->string('code')->nullable();
1721

1822
$table->bigInteger('created_by')->nullable();
1923
$table->timestamps();

src/Dtos/DevNotificationDto.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* Class DevNotificationDto
1111
*
1212
* @property string message
13+
* @property string file
14+
* @property string line
15+
* @property string code
1316
*
1417
* @property string attachment_title
1518
* @property string attachment_link
@@ -19,6 +22,9 @@
1922
*/
2023
class DevNotificationDto implements JsonSerializable
2124
{
25+
public $file;
26+
public $line;
27+
public $code;
2228
public $message;
2329

2430
public $attachment_title;
@@ -62,6 +68,8 @@ public static function makeFromException(Throwable $t): self
6268
public function setException(Throwable $t): self
6369
{
6470
$this->message = $t->getMessage();
71+
$this->code = $t->getCode();
72+
$this->file = $t->getFile();
6573

6674
return $this;
6775
}
@@ -78,9 +86,33 @@ public static function makeFromExceptionLog(ExceptionLog $log): self
7886
return tap(new static, fn (self $dto) => $dto
7987
->setMessage($log->name)
8088
->setTitle($log->identifier)
89+
->setLine($log->line)
90+
->setFile($log->file)
91+
->setCode($log->code)
8192
->setAttachmentLink($log->getUrl()));
8293
}
8394

95+
protected function setLine($value): self
96+
{
97+
$this->line = $value;
98+
99+
return $this;
100+
}
101+
102+
protected function setFile($value): self
103+
{
104+
$this->file = $value;
105+
106+
return $this;
107+
}
108+
109+
protected function setCode($value): self
110+
{
111+
$this->code = $value;
112+
113+
return $this;
114+
}
115+
84116
protected function setTitle(string $title): self
85117
{
86118
$this->attachment_title = $title;
@@ -97,8 +129,22 @@ protected function setAttachmentLink($title = null): self
97129

98130
public function jsonSerialize()
99131
{
132+
$message = $this->message;
133+
134+
if ($this->code) {
135+
$message .= "| Code[{$this->code}]";
136+
}
137+
138+
if ($this->file) {
139+
$message .= "| File[{$this->file}]";
140+
}
141+
142+
if ($this->line) {
143+
$message .= "| Line[{$this->line}]";
144+
}
145+
100146
return [
101-
'message' => $this->message,
147+
'message' => $message,
102148
'attachment_title' => $this->attachment_title,
103149
'attachment_content' => $this->attachment_content,
104150
];

src/Models/ExceptionLog.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
* @property string $uuid
1818
* @property-read string $identifier
1919
* @property string $name
20+
* @property string $file
21+
* @property string $line
22+
* @property string $code
2023
* @property array $exception
24+
* @property array $previous
2125
* @property array|mixed $payload
2226
* @package App\Models
2327
*/
@@ -51,7 +55,11 @@ public static function makeFromException(Throwable $throwable, JsonSerializable
5155
{
5256
return new static([
5357
'name' => Str::substr($throwable->getMessage(), 0, 255),
58+
'file' => $throwable->getFile(),
59+
'line' => $throwable->getLine(),
60+
'code' => $throwable->getCode(),
5461
'exception' => $throwable->__toString(),
62+
'previous' => (string) $throwable->getPrevious(),
5563
'payload' => optional($payload)->jsonSerialize(),
5664
]);
5765
}

src/Notifications/Slack.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ private function send($item)
5454

5555
if ($item instanceof Throwable) {
5656
if ($this->persist) {
57-
ExceptionLog::makeFromException($item)->save();
57+
$dto = DevNotificationDto::makeFromExceptionLog(
58+
tap(ExceptionLog::makeFromException($item), fn (ExceptionLog $log) => $log->save())
59+
);
60+
} else {
61+
$dto = DevNotificationDto::makeFromException($item);
5862
}
59-
60-
$dto->setException($item);
6163
}
6264

6365
if (is_string($item)) {
@@ -74,6 +76,7 @@ private function send($item)
7476

7577
$notification = new $class($dto);
7678

79+
7780
if (is_callable($cb = static::$notifyUsing)) {
7881
return call_user_func($cb, $notification);
7982
}

tests/Helpers/SlackHelperTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binarcode\LaravelDeveloper\Tests\Helpers;
44

5+
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
56
use Binarcode\LaravelDeveloper\Notifications\DevNotification;
67
use Binarcode\LaravelDeveloper\Notifications\Slack;
78
use Binarcode\LaravelDeveloper\Tests\TestCase;
@@ -33,10 +34,18 @@ public function test_slack_helper_can_send_throwable_to_slack()
3334
{
3435
Notification::fake();
3536

36-
$this->assertInstanceOf(Slack::class, slack(new Exception('wrong'))->persist());
37+
config([
38+
'developer.exception_log_base_url' => 'app.test/{uuid}',
39+
]);
40+
41+
$this->assertInstanceOf(Slack::class, slack(new Exception('not found', 404))->persist());
3742

3843
$this->assertDatabaseCount('exception_logs', 1);
3944

40-
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class);
45+
$uuid = ExceptionLog::latest()->first()->uuid;
46+
47+
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class, function (DevNotification $class) use ($uuid) {
48+
return $class->notificationDto->attachment_link === "app.test/{$uuid}";
49+
});
4150
}
4251
}

0 commit comments

Comments
 (0)