Catching exceptions when using namespaces in plugin

Hi all,

I have a plugin which loads a few classes from within the ./classes directory inside my plugin. The classes live in their own namespaces.

I’m running into an issue where I’m unable to catch an error cause I’m not able to use two Exception classes with the same name. Below I have the class Notify with an init() method. When I call the method and e.g. something fails within the $kirby->email() part an exception is being thrown but it’s not being caught by the catch exception inside my init() method. From what I can see that’s cause phpmailer is using it’s own PHPMailer\PHPMailer\Exception. I could simply import the class but then if something goes wrong inside the page update logic after sending the email the same happens here where I’m unable to catch the exception. Is there a better way of doing this?

If I import both classes its throwing: Cannot use PHPMailer\PHPMailer\Exception as Exception because the name is already in use.

use Kirby\Exception\Exception;
use PHPMailer\PHPMailer\Exception;

Plugin directory

.
├── classes
│   └── Misc
│       └── Notify.php
├── index.php

Notify Class

<?php

namespace Misc;

use Kirby\Exception\Exception;

class Notify {

    private $alert = null;

    public function getAlert() {
        return $this->alert;
    }
    
    public function init(object $page, array $data) {
        $kirby = kirby();

        try {
            $kirby->email([
              'from' => 'welcome@supercompany.com',
              'replyTo' => 'no-reply@supercompany.com',
              'to' => 'someone@gmail.com',
              'cc' => 'anotherone@gmail.com',
              'bcc' => 'secret@gmail.com',
              'subject' => 'Welcome!',
              'body'=> 'It\'s great to have you with us',
            ]);

            // Update the page
            $newPage = $kirby->impersonate('kirby', function () use ($page, $data) {
                return $page->update($data);
            });

            return $newPage;
        } catch (Exception $e) {
            $this->alert = $e->getMessage();
        }
    }
}