Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Illuminate/Contracts/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ interface ExceptionHandler
*
* @param \Throwable $e
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
* @return void
*
* @throws \Throwable
*/
public function report(Throwable $e, array $context = []);
public function report(Throwable $e, array $context = [], ?string $level = null);

/**
* Determine if the exception should be reported.
Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,31 +419,33 @@ public function level($type, $level)
*
* @param \Throwable $e
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
* @return void
*
* @throws \Throwable
*/
public function report(Throwable $e, array $context = [])
public function report(Throwable $e, array $context = [], ?string $level = null)
{
$e = $this->mapException($e);

if ($this->shouldntReport($e)) {
return;
}

$this->reportThrowable($e, $context);
$this->reportThrowable($e, $context, $level);
}

/**
* Reports error based on report method on exception or to logger.
*
* @param \Throwable $e
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
* @return void
*
* @throws \Throwable
*/
protected function reportThrowable(Throwable $e, array $context = []): void
protected function reportThrowable(Throwable $e, array $context = [], ?string $level = null): void
{
$this->reportedExceptionMap[$e] = true;

Expand All @@ -466,7 +468,7 @@ protected function reportThrowable(Throwable $e, array $context = []): void
throw $e;
}

$level = $this->mapLogLevel($e);
$level ??= $this->mapLogLevel($e);

$originallyReporting = $this->currentlyReporting;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ public function __construct($originalHandler, $except = [])
*
* @param \Throwable $e
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
* @return void
*
* @throws \Exception
*/
public function report(Throwable $e, array $context = [])
public function report(Throwable $e, array $context = [], ?string $level = null)
{
//
}
Expand Down
15 changes: 9 additions & 6 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,15 @@ function redirect($to = null, $status = 302, $headers = [], $secure = null): Red
*
* @param \Throwable|string $exception
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
*/
function report($exception, array $context = []): void
function report($exception, array $context = [], ?string $level = null): void
{
if (is_string($exception)) {
$exception = new Exception($exception);
}

app(ExceptionHandler::class)->report($exception, $context);
app(ExceptionHandler::class)->report($exception, $context, $level);
}
}

Expand All @@ -738,11 +739,12 @@ function report($exception, array $context = []): void
* @param bool $boolean
* @param \Throwable|string $exception
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
*/
function report_if($boolean, $exception, array $context = []): void
function report_if($boolean, $exception, array $context = [], ?string $level = null): void
{
if ($boolean) {
report($exception, $context);
report($exception, $context, $level);
}
}
}
Expand All @@ -754,11 +756,12 @@ function report_if($boolean, $exception, array $context = []): void
* @param bool $boolean
* @param \Throwable|string $exception
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
*/
function report_unless($boolean, $exception, array $context = []): void
function report_unless($boolean, $exception, array $context = [], ?string $level = null): void
{
if (! $boolean) {
report($exception, $context);
report($exception, $context, $level);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @method static bool shouldStopRetries(\Throwable $e)
* @method static \Illuminate\Foundation\Exceptions\Handler dontFlash(array|string $attributes)
* @method static \Illuminate\Foundation\Exceptions\Handler level(string $type, string $level)
* @method static void report(\Throwable $e, array $context = [])
* @method static void report(\Throwable $e, array $context = [], ?string $level = null)
* @method static bool isReporting(\Throwable $e)
* @method static bool shouldReport(\Throwable $e)
* @method static \Illuminate\Foundation\Exceptions\Handler throttleUsing(callable $throttleUsing)
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Testing/Fakes/ExceptionHandlerFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ public function assertNothingReported()
*
* @param \Throwable $e
* @param array<string, mixed> $context
* @param \Psr\Log\LogLevel::*|null $level
* @return void
*
* @throws \Throwable
*/
public function report($e, array $context = [])
public function report($e, array $context = [], ?string $level = null)
{
if (! $this->isFakedException($e)) {
$this->handler->report($e, $context);
$this->handler->report($e, $context, $level);

return;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ public function testHandlerReportsExceptionWithCustomLogLevel()
$this->handler->report(new OutOfRangeException('Custom message'));
}

public function testHandlerReportsExceptionWithInlineLogLevel()
{
$logger = Mockery::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);

$logger->expects('warning')->withArgs([
'Warning message',
Mockery::subset(['from' => 'user@example.com']),
]);

$this->handler->level(InvalidArgumentException::class, LogLevel::CRITICAL);

$this->handler->report(
new InvalidArgumentException('Warning message'),
['from' => 'user@example.com'],
LogLevel::WARNING,
);
}

public function testHandlerIgnoresNotReportableExceptions()
{
$logger = Mockery::mock(LoggerInterface::class);
Expand Down
21 changes: 20 additions & 1 deletion tests/Integration/Foundation/FoundationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@
use Illuminate\Support\Str;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;
use Psr\Log\LogLevel;

class FoundationHelpersTest extends TestCase
{
public function testReportHelpersForwardContextAndLevel()
{
$handler = new FakeHandler;
$this->app->instance(ExceptionHandler::class, $handler);

report($first = new Exception('First'), ['id' => 1], LogLevel::WARNING);
report_if(true, $second = new Exception('Second'), ['id' => 2], LogLevel::NOTICE);
report_unless(false, $third = new Exception('Third'), ['id' => 3], LogLevel::INFO);

$this->assertSame([$first, $second, $third], $handler->reported);
$this->assertSame([['id' => 1], ['id' => 2], ['id' => 3]], $handler->contexts);
$this->assertSame([LogLevel::WARNING, LogLevel::NOTICE, LogLevel::INFO], $handler->levels);
}

public function testRescue()
{
$this->assertSame(
Expand Down Expand Up @@ -164,10 +179,14 @@ protected function makeManifest($directory = '')
class FakeHandler
{
public $reported = [];
public $contexts = [];
public $levels = [];

public function report($exception)
public function report($exception, array $context = [], ?string $level = null)
{
$this->reported[] = $exception;
$this->contexts[] = $context;
$this->levels[] = $level;
}

public function render($exception)
Expand Down
2 changes: 1 addition & 1 deletion tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public function getConnectionName()

class ShouldntRetryExceptionHandler implements ExceptionHandler
{
public function report(\Throwable $e, array $context = [])
public function report(\Throwable $e, array $context = [], ?string $level = null)
{
//
}
Expand Down
Loading