You have to throw an exception. Any exception should prevent the process from going further and will return an error to the page.
Now if you want to show a message in the front-end, an easy option would be to use a validation exception, that way the error is shown as a validation error message. Other exceptions will be shown as "an error occured" only.
Maybe there are other exceptions that have a special built-in rendering inside Flarum front-end, but I don't know any other.
You could also use a custom exception, and add a middleware to catch and format that exception into a response. Then use some code in the front-end to handle that error response separately.