Display specific error message in the view after catching an exception

I have a Livewire controller which I use to submit a form.

After validating the form, I’m calling a service to actually do the action I want to accomplish. This service can throw a few exceptions in some cases.

public function submit()
    {
        $this->validate([
            'firstName' => 'required|max:255',
            'lastName' => 'required|max:255',
            'email' => 'required|email:rfc|max:255',
            'password' => 'required|max:255',
        ]);

        try {
            (new CreateAccount)->execute([
                'first_name' => $this->firstName,
                'last_name' => $this->lastName,
                'email' => $this->email,
                'password' => $this->password,
            ]);
        } catch (\Exception $e) {
            $this->addError('error', 'Dramatic error you will die.');
        };
}

When I catch one of these exceptions, how could I send back the custom error message to the view?

It looks like Livewire only sends error to the client when there is a ValidationException.

Just add a public property.

Livewire component:

public $createAccountError;

public function submit()
    {
        $this->validate([
            'firstName' => 'required|max:255',
            'lastName' => 'required|max:255',
            'email' => 'required|email:rfc|max:255',
            'password' => 'required|max:255',
        ]);

        try {
            (new CreateAccount)->execute([
                'first_name' => $this->firstName,
                'last_name' => $this->lastName,
                'email' => $this->email,
                'password' => $this->password,
            ]);
        } catch (\Exception $e) {
            $this->addError('error', 'Dramatic error you will die.');

            $this->createAccountError = 'Dramatic error you will die.';
        };
}

Livewire view:

...
    @if ($createAccountError)
        <div>{{ $createAccountError }}</div>
    @endif
...