Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11762ca

Browse files
authoredDec 17, 2021
RichRecordConverter don't show filename and line on exceptions (fix #23)
1 parent cff9b97 commit 11762ca

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed
 

‎CHANGELOG.md

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

33
All Notable changes to `laravel-discord-logger` will be documented in this file.
44

5+
## [Unreleased]
6+
7+
### Fixed
8+
- RichRecordConverter don't show filename and line on exceptions (#22).
9+
510
## 1.0.0 - 2019/05/30
611

712
- Initial release

‎src/DiscordLogger/Converters/AbstractRecordConverter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ protected function getStacktraceFilename(array $record): ?string
5555

5656
protected function getStacktrace(array $record): ?string
5757
{
58-
if (empty($record['context'])
59-
|| empty($record['context']['exception'])
60-
|| !is_a($record['context']['exception'], Throwable::class))
58+
if (!is_a($record['context']['exception'] ?? '', Throwable::class))
6159
{
6260
return null;
6361
}
6462

6563
/** @var \Throwable $exception */
6664
$exception = $record['context']['exception'];
6765

68-
return $exception->getTraceAsString();
66+
return "On {$exception->getFile()}:{$exception->getLine()} (code {$exception->getCode()})\n" .
67+
"Stacktrace:\n" .
68+
$exception->getTraceAsString();
6969
}
7070

7171
protected function getRecordColor(array $record): int

‎src/DiscordLogger/Converters/RichRecordConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function addInlineMessageStacktrace(Message $message, array $record, s
8888
{
8989
$message->embed(Embed::make()
9090
->color($this->getRecordColor($record))
91-
->title('Stacktrace')
91+
->title('Exception')
9292
->description("`$stacktrace`"));
9393
}
9494
}

‎tests/Converters/RichLoggerMessagesTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public function sends_a_simple_message_for_log()
3737
], $message);
3838
}
3939

40+
/** @test */
41+
public function includes_error_filename_and_line()
42+
{
43+
$this->config->set('discord-logger.stacktrace', 'inline');
44+
45+
$exception = new Exception();
46+
$message = $this->exception('This is a test', $exception)[0];
47+
48+
$this->assertStringContainsString($exception->getFile(), $message->embeds[1]->description);
49+
$this->assertStringContainsString($exception->getLine(), $message->embeds[1]->description);
50+
}
51+
4052
/** @test */
4153
public function includes_stacktrace_in_content_when_attachment_disabled()
4254
{
@@ -68,8 +80,8 @@ public function includes_stacktrace_as_file_when_attachment_enabled()
6880
], $messages[0]);
6981

7082
MessageAssertions::assertMessagePartialMatch([
71-
'file' => ['filename' => '20000101121314_stacktrace.txt',
72-
'contents' => $exception->getTraceAsString(),],
83+
'file' => ['filename' => '20000101121314_stacktrace.txt'],
7384
], $messages[1]);
85+
$this->assertStringContainsString($exception->getTraceAsString(), $messages[1]->file['contents']);
7486
}
7587
}

‎tests/Converters/SimpleLoggerMessagesTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public function does_not_include_emoji_if_disabled()
3939
], $message);
4040
}
4141

42+
/** @test */
43+
public function includes_error_filename_and_line()
44+
{
45+
$this->config->set('discord-logger.stacktrace', 'inline');
46+
47+
$exception = new Exception();
48+
$message = $this->exception('This is a test', $exception)[0];
49+
50+
$this->assertStringContainsString($exception->getFile(), $message->content);
51+
$this->assertStringContainsString($exception->getLine(), $message->content);
52+
}
53+
4254
/** @test */
4355
public function includes_stacktrace_in_content_when_attachment_disabled()
4456
{
@@ -62,8 +74,8 @@ public function includes_stacktrace_as_file_when_attachment_enabled()
6274
$this->assertStringContainsString('[2000-01-01 12:13:14] Laravel.CRITICAL: This is a test', $message->content);
6375

6476
MessageAssertions::assertMessagePartialMatch([
65-
'file' => ['filename' => '20000101121314_stacktrace.txt',
66-
'contents' => $exception->getTraceAsString(),],
77+
'file' => ['filename' => '20000101121314_stacktrace.txt'],
6778
], $message);
79+
$this->assertStringContainsString($exception->getTraceAsString(), $message->file['contents']);
6880
}
6981
}

0 commit comments

Comments
 (0)
Please sign in to comment.