Customize JupyterHub error handling for unregistered AD users with ldapauthenticator

I’m using ldapauthenticator. From the GitHub issue #38, I know that if a user with the same username doesn’t exist on the system, the JupyterHub service will return a 500: Internal Server Error along with a Python error saying “KeyError: “getpwnam(): name not found: ””.


In my case, this means that the user has an Active Directory (AD) account but has not been registered in this system.

I want to change this error behavior and instead redirect the user to a “Not Registered Yet” page, informing them to register first. I want to catch the getpwnam error and redirect the user to the “Not Registered” page. How to add this functionality inside the jupyterhub_config.py file? Or, if there is a better way to handle this situation?

One option is to override the check_allowed method:

e.g. something along the lines of

class CustomLdapAuthenticator(LDAPAuthenticator):
    def check_allowed(self, username, authentication=None):
        if <user does not exist>:
            raise web.HTTPError(403, reason="custom message")