From b9cc63feedff846913b77137e74b71ecf16e11df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 19 Feb 2025 12:02:58 +0100 Subject: [PATCH] handle exceptions in Tenancy:run() --- src/Tenancy.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Tenancy.php b/src/Tenancy.php index 31947dbe..5ab6400d 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -77,20 +77,23 @@ class Tenancy public function run(Tenant $tenant, Closure $callback): mixed { $originalTenant = $this->tenant; + $result = null; - $this->initialize($tenant); - $result = $callback($tenant); + try { + $this->initialize($tenant); + $result = $callback($tenant); + } finally { + if ($originalTenant) { + $this->initialize($originalTenant); + } else { + $this->end(); + } + } if ($result instanceof PendingDispatch) { // #1277 $result = null; } - if ($originalTenant) { - $this->initialize($originalTenant); - } else { - $this->end(); - } - return $result; }