23
23
from .login .decorators import login_required
24
24
from .security_api import is_anonymous , remember
25
25
from .statics import INDEX_RESOURCE_NAME
26
+ from .utils import compose_error_msg
26
27
27
28
log = logging .getLogger (__name__ )
28
29
@@ -156,7 +157,9 @@ async def access_study(request: web.Request) -> web.Response:
156
157
157
158
- public studies are templates that are marked as published in the database
158
159
- if user is not registered, it creates a temporary guest account with limited resources and expiration
160
+ - this handler is NOT part of the API and therefore does NOT respond with json
159
161
"""
162
+ # TODO: implement nice error-page.html
160
163
project_id = request .match_info ["id" ]
161
164
162
165
template_project = await get_public_project (request .app , project_id )
@@ -166,25 +169,38 @@ async def access_study(request: web.Request) -> web.Response:
166
169
Please contact the data curators for more information."
167
170
)
168
171
172
+ # Get or create a valid user
169
173
user = None
170
174
is_anonymous_user = await is_anonymous (request )
171
- if is_anonymous_user :
172
- log .debug ("Creating temporary user ..." )
173
- user = await create_temporary_user (request )
174
- else :
175
+ if not is_anonymous_user :
176
+ # NOTE: covers valid cookie with unauthorized user (e.g. expired guest/banned)
177
+ # TODO: test if temp user overrides old cookie properly
175
178
user = await get_authorized_user (request )
176
179
177
180
if not user :
178
- raise RuntimeError ("Unable to start user session" )
181
+ log .debug ("Creating temporary user ..." )
182
+ user = await create_temporary_user (request )
183
+ is_anonymous_user = True
179
184
180
- log .debug (
181
- "Granted access to study '%s' for user %s. Copying study over ..." ,
182
- template_project .get ("name" ),
183
- user .get ("email" ),
184
- )
185
- copied_project_id = await copy_study_to_account (request , template_project , user )
185
+ try :
186
+ log .debug (
187
+ "Granted access to study '%s' for user %s. Copying study over ..." ,
188
+ template_project .get ("name" ),
189
+ user .get ("email" ),
190
+ )
191
+ copied_project_id = await copy_study_to_account (request , template_project , user )
192
+
193
+ log .debug ("Study %s copied" , copied_project_id )
186
194
187
- log .debug ("Study %s copied" , copied_project_id )
195
+ except Exception : # pylint: disable=broad-except
196
+ log .exception (
197
+ "Failed while copying project '%s' to '%s'" ,
198
+ template_project .get ("name" ),
199
+ user .get ("email" ),
200
+ )
201
+ raise web .HTTPInternalServerError (
202
+ reason = compose_error_msg ("Unable to copy project." )
203
+ )
188
204
189
205
try :
190
206
redirect_url = (
@@ -193,11 +209,11 @@ async def access_study(request: web.Request) -> web.Response:
193
209
.with_fragment ("/study/{}" .format (copied_project_id ))
194
210
)
195
211
except KeyError :
196
- log .error (
212
+ log .exception (
197
213
"Cannot redirect to website because route was not registered. Probably qx output was not ready and it was disabled (see statics.py)"
198
214
)
199
- raise RuntimeError (
200
- "Unable to serve front-end. Study has been anyway copied over to user."
215
+ raise web . HTTPInternalServerError (
216
+ reason = compose_error_msg ( "Unable to serve front-end." )
201
217
)
202
218
203
219
response = web .HTTPFound (location = redirect_url )
0 commit comments