# Buggy code
def index(self, locale):
    session["locale"] = locale
    refresh()
    self.update_redirect()
    return redirect(self.get_redirect())


# Traceback (babel.core.UnknownLocaleError)
Traceback (most recent call last):
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\views.py", line 41, in index
return self.render_template(self.index_template, appbuilder=self.appbuilder)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\baseviews.py", line 280, in render_template
return render_template(
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\templating.py", line 137, in render_template
return _render(
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask\templating.py", line 120, in _render
rv = template.render(context)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\jinja2\environment.py", line 1090, in render
self.environment.handle_exception()
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\jinja2\environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\jinja2\_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\app\templates\index.html", line 1, in top-level template code
{% extends "appbuilder/base.html" %}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\templates\appbuilder\base.html", line 1, in top-level template code
{% extends base_template %}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\app\templates\mybase.html", line 1, in top-level template code
{% extends 'appbuilder/baselayout.html' %}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\templates\appbuilder\baselayout.html", line 2, in top-level template code
{% import 'appbuilder/baselib.html' as baselib %}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\templates\appbuilder\init.html", line 46, in top-level template code
{% block body %}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\templates\appbuilder\baselayout.html", line 5, in block "body"
{% include 'appbuilder/general/confirm.html' %}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_appbuilder\templates\appbuilder\general\confirm.html", line 6, in top-level template code
{{_('User confirmation needed')}}
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\jinja2\ext.py", line 144, in _gettext_alias
return __context.call(__context.resolve("gettext"), *args, **kwargs)
File "C:\Users\myuserPycharmProjects\FAB-test-site\venv\Lib\site-packages\jinja2\ext.py", line 150, in gettext
rv = __context.call(func, __string)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_babel\__init__.py", line 110, in <lambda>
lambda x: get_translations().ugettext(x),
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_babel\__init__.py", line 221, in get_translations
[get_locale()],
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\flask_babel\__init__.py", line 255, in get_locale
locale = Locale.parse(rv)
File "C:\Users\myuser\PycharmProjects\FAB-test-site\venv\Lib\site-packages\babel\core.py", line 331, in parse
raise UnknownLocaleError(input_id)
babel.core.UnknownLocaleError: unknown locale 'nonexistentlanguage'

# Fixed code
def index(self, locale):
    if locale not in self.appbuilder.bm.languages:
        abort(404, description="Locale not supported.")
    session["locale"] = locale
    refresh()
    self.update_redirect()
    return redirect(self.get_redirect())