Skip to content

Commit 48a9eba

Browse files
authored
catch auth error (#1239)
1 parent ed3346c commit 48a9eba

File tree

1 file changed

+15
-13
lines changed
  • compose/backend/neurosynth_compose/resources

1 file changed

+15
-13
lines changed

compose/backend/neurosynth_compose/resources/auth.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
from contextlib import contextmanager
33
from urllib.request import urlopen
44

5-
from flask import jsonify, request
5+
from flask import current_app, has_app_context, jsonify, request
66
from jose import jwt
7-
from flask import current_app
87
from werkzeug.local import LocalProxy
98
from connexion.security import NO_VALUE
109
from ..database import db
@@ -73,23 +72,26 @@ def init_app(app):
7372

7473

7574
def _get_current_app():
76-
try:
75+
if has_app_context():
7776
return current_app._get_current_object()
78-
except RuntimeError as exc: # pragma: no cover - defensive
79-
if _flask_app is not None and "application context" in str(exc).lower():
80-
return _flask_app
81-
raise
77+
if _flask_app is not None: # pragma: no cover - defensive
78+
return _flask_app
79+
raise RuntimeError("No Flask application is configured for authentication helpers.")
8280

8381

8482
@contextmanager
8583
def _ensure_app_context():
86-
try:
84+
if has_app_context():
8785
yield current_app._get_current_object()
88-
except RuntimeError as exc:
89-
if _flask_app is None or "application context" not in str(exc).lower():
90-
raise
91-
with _flask_app.app_context():
92-
yield _flask_app
86+
return
87+
88+
if _flask_app is None: # pragma: no cover - defensive
89+
raise RuntimeError(
90+
"No Flask application is configured for authentication helpers."
91+
)
92+
93+
with _flask_app.app_context():
94+
yield _flask_app
9395

9496

9597
app = LocalProxy(_get_current_app)

0 commit comments

Comments
 (0)