Close app/print to console on exception in app

Hi,
I sometimes get exceptions during the building of main document of the server. In such cases, the browser launches, and shows a page that has the non-indicative writing “500: Internal Server Error”.
In such cases, I want my app to fail and raise an exception in case that it didn’t load correctly. So is it possible to raise an exception in the main thread where the server was run?
If not, is there a way to show the contents of the errors (i.e. the message and trace) in (preferably) the console, or in the webpage?

The way I open the app is:

   def raise_exception():
        raise Exception('This is the error message.')

   def run(port: Union[None, int] = 5005, stop_on_interrupt: bool = True):

        _wrapped_app_func = raise_exception

        self._server = Server({'/': _wrapped_app_func}, num_procs=1, port=port)
        self._server.start()

        try:
            self._server.io_loop.add_callback(self._server.show, "/")
            self._server.io_loop.start()
        except KeyboardInterrupt:
            if stop_on_interrupt:
                self.stop()
            else:
                raise

Thanks in advance!

OK, I figured it out. Because of torando’s RequestHandler handling of exceptions, What I did is to create a new base-exception. Such exceptions pass-through the mechanism and can be caught outside.

The new code is:

   def raise_exception():
        try:
            raise Exception('This is the error message.')
        except Exception as ex:
            raise FatalServerError from ex

   def run(port: Union[None, int] = 5005, stop_on_interrupt: bool = True):

        _wrapped_app_func = raise_exception

        self._server = Server({'/': _wrapped_app_func}, num_procs=1, port=port)
        self._server.start()

        try:
            self._server.io_loop.add_callback(self._server.show, "/")
            self._server.io_loop.start()
        except KeyboardInterrupt:
            if stop_on_interrupt:
                self.stop()
            else:
                raise
        except FatalServerError:
            self.stop()
            raise

   class FatalServerError(BaseException):
       pass
1 Like