The
rescue
function executes the given closure and catches any exceptions that occur during its execution. All exceptions that are caught will be sent to your exception handler; however, the request will continue processing:<?php return rescue(function () {
return $this->method();
});
<?php if (app()->runningUnitTests()) {
rescue(function () {
return Storage::disk(static::disk());
}, function () {
return Storage::fake(static::disk());
});
}
return Storage::disk(static::disk());
<?php return true;
}
return rescue(fn () => with($this->throttle($e), function ($throttle) use ($e) {
if ($throttle instanceof Unlimited || $throttle === null) {
return false;
}
if ($throttle instanceof Lottery) {
return ! $throttle($e);
}
return ! $this->container->make(RateLimiter::class)->attempt(
with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? md5($key) : $key),
$throttle->maxAttempts,
fn () => true,
$throttle->decaySeconds
);
}), rescue: false, report: false);
}
<?php {
$this->assertEquals(
'rescued!',
rescue(function () {
throw new Exception;
}, 'rescued!')
);
$this->assertEquals(
<?php $this->assertEquals(
'rescued!',
rescue(function () {
throw new Exception;
}, function () {
return 'rescued!';
})
);
$this->assertEquals(
<?php $this->assertEquals(
'no need to rescue',
rescue(function () {
return 'no need to rescue';
}, 'rescued!')
);
$testClass = new class
<?php $this->assertEquals(
'rescued!',
rescue(function () use ($testClass) {
$testClass->test([]);
}, 'rescued!')
);
}
<?php ->withoutExceptionHandling();
Route::get('/', function () {
rescue(fn () => throw new Exception('Test exception'));
});
$this->expectException(Exception::class);
<?php ->withExceptionHandling();
Route::get('/', function () {
rescue(fn () => throw new Exception('Test exception'));
});
$this->expectException(Exception::class);
<?php $this->withoutExceptionHandling();
Route::get('/validation', function () {
rescue(fn () => Validator::validate(['name' => ''], ['name' => 'required']));
});
$this->get('/validation')->assertStatus(200);
<?php $this->get('/validation')->assertStatus(200);
Route::get('/model', function () {
rescue(fn () => throw new ModelNotFoundException());
});
$this->get('/model')->assertStatus(200);
<?php $this->get('/model')->assertStatus(200);
rescue(fn () => throw new ModelNotFoundException());
Exceptions::assertReportedCount(3);
}
<?php {
Exceptions::fake();
rescue(fn () => throw new Exception('Test exception'));
Exceptions::assertReported(Exception::class);
}
<?php {
Exceptions::fake();
rescue(fn () => throw new Exception('Test exception'), null, false);
Exceptions::assertNothingReported();
}
<?php assertType('null', transform('', fn () => 1));
assertType('null', transform([], fn () => 1));
assertType('int|null', rescue(fn () => 123));
assertType('int', rescue(fn () => 123, 345));
assertType('int', rescue(fn () => 123, fn () => 345));
<?php assertType('null', transform([], fn () => 1));
assertType('int|null', rescue(fn () => 123));
assertType('int', rescue(fn () => 123, 345));
assertType('int', rescue(fn () => 123, fn () => 345));