Skip to content

API Exceptions

Petr Stehlík edited this page Nov 13, 2017 · 1 revision

If you need to report an error to the frontend you must subclass the ApiException class which implements the needed functionality to provide you with unified error reporting to the frontend.

ApiException

Liberouter GUI provides you with the ApiException class which you should use for reporting errors back to frontend. You should subclass the ApiException in order to provide useful and straightforward logging. In your module's backend/ folder, create file error.py:

from liberouterapi.error import ApiException

class ExampleException(ApiException):
    status_code = 500

The status_code can be changed in order to specify the Error which happened. For more info see the list of standard HTTP codes.

Now whenever you want to throw an exception, throw ExampleException, preferably with the reason:

raise ExampleException("An error occurred")

This will return the HTTP 500 status code and error message to the frontend. You can specify status_code in the throw as well.

raise ExampleException("A 403 error occurred", status_code=403)