Skip to content

Commit e746a42

Browse files
committed
fix: do not show internal model names in user facing error messages.
See #613 for a possible solution to the pain renaming many distributed user facing model names.
1 parent f02ba1e commit e746a42

18 files changed

+124
-122
lines changed

src/mavedb/lib/permissions/collection.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def has_permission(user_data: Optional[UserData], entity: Collection, action: Ac
7373
if action not in handlers:
7474
supported_actions = ", ".join(a.value for a in handlers.keys())
7575
raise NotImplementedError(
76-
f"Action '{action.value}' is not supported for Collection entities. "
76+
f"Action '{action.value}' is not supported for collection entities. "
7777
f"Supported actions: {supported_actions}"
7878
)
7979

@@ -129,7 +129,7 @@ def _handle_read_action(
129129
if roles_permitted(active_roles, [UserRole.admin]):
130130
return PermissionResponse(True)
131131

132-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
132+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
133133

134134

135135
def _handle_update_action(
@@ -169,7 +169,7 @@ def _handle_update_action(
169169
if roles_permitted(active_roles, [UserRole.admin]):
170170
return PermissionResponse(True)
171171

172-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
172+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
173173

174174

175175
def _handle_delete_action(
@@ -210,7 +210,7 @@ def _handle_delete_action(
210210
if user_is_owner and private:
211211
return PermissionResponse(True)
212212

213-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
213+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
214214

215215

216216
def _handle_publish_action(
@@ -249,7 +249,7 @@ def _handle_publish_action(
249249
if roles_permitted(active_roles, [UserRole.admin]):
250250
return PermissionResponse(True)
251251

252-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
252+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
253253

254254

255255
def _handle_add_experiment_action(
@@ -290,7 +290,7 @@ def _handle_add_experiment_action(
290290
if roles_permitted(active_roles, [UserRole.admin]):
291291
return PermissionResponse(True)
292292

293-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
293+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
294294

295295

296296
def _handle_add_score_set_action(
@@ -330,7 +330,7 @@ def _handle_add_score_set_action(
330330
if roles_permitted(active_roles, [UserRole.admin]):
331331
return PermissionResponse(True)
332332

333-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
333+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
334334

335335

336336
def _handle_add_role_action(
@@ -369,7 +369,7 @@ def _handle_add_role_action(
369369
if roles_permitted(active_roles, [UserRole.admin]):
370370
return PermissionResponse(True)
371371

372-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
372+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")
373373

374374

375375
def _handle_add_badge_action(
@@ -402,4 +402,4 @@ def _handle_add_badge_action(
402402
if roles_permitted(active_roles, [UserRole.admin]):
403403
return PermissionResponse(True)
404404

405-
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner)
405+
return deny_action_for_entity(entity, private, user_data, bool(collection_roles) or user_is_owner, "collection")

src/mavedb/lib/permissions/experiment.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def has_permission(user_data: Optional[UserData], entity: Experiment, action: Ac
5858
if action not in handlers:
5959
supported_actions = ", ".join(a.value for a in handlers.keys())
6060
raise NotImplementedError(
61-
f"Action '{action.value}' is not supported for Experiment entities. "
61+
f"Action '{action.value}' is not supported for experiment entities. "
6262
f"Supported actions: {supported_actions}"
6363
)
6464

@@ -108,7 +108,7 @@ def _handle_read_action(
108108
if roles_permitted(active_roles, [UserRole.admin, UserRole.mapper]):
109109
return PermissionResponse(True)
110110

111-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
111+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment")
112112

113113

114114
def _handle_update_action(
@@ -143,7 +143,7 @@ def _handle_update_action(
143143
if roles_permitted(active_roles, [UserRole.admin]):
144144
return PermissionResponse(True)
145145

146-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
146+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment")
147147

148148

149149
def _handle_delete_action(
@@ -179,7 +179,7 @@ def _handle_delete_action(
179179
if user_is_owner and private:
180180
return PermissionResponse(True)
181181

182-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
182+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment")
183183

184184

185185
def _handle_add_score_set_action(
@@ -218,4 +218,4 @@ def _handle_add_score_set_action(
218218
if not private and user_data is not None:
219219
return PermissionResponse(True)
220220

221-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
221+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment")

src/mavedb/lib/permissions/experiment_set.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def has_permission(user_data: Optional[UserData], entity: ExperimentSet, action:
5858
if action not in handlers:
5959
supported_actions = ", ".join(a.value for a in handlers.keys())
6060
raise NotImplementedError(
61-
f"Action '{action.value}' is not supported for ExperimentSet entities. "
61+
f"Action '{action.value}' is not supported for experiment set entities. "
6262
f"Supported actions: {supported_actions}"
6363
)
6464

@@ -108,7 +108,7 @@ def _handle_read_action(
108108
if roles_permitted(active_roles, [UserRole.admin, UserRole.mapper]):
109109
return PermissionResponse(True)
110110

111-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
111+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment set")
112112

113113

114114
def _handle_update_action(
@@ -143,7 +143,7 @@ def _handle_update_action(
143143
if roles_permitted(active_roles, [UserRole.admin]):
144144
return PermissionResponse(True)
145145

146-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
146+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment set")
147147

148148

149149
def _handle_delete_action(
@@ -179,7 +179,7 @@ def _handle_delete_action(
179179
if user_is_owner and private:
180180
return PermissionResponse(True)
181181

182-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
182+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment set")
183183

184184

185185
def _handle_add_experiment_action(
@@ -215,4 +215,4 @@ def _handle_add_experiment_action(
215215
if roles_permitted(active_roles, [UserRole.admin]):
216216
return PermissionResponse(True)
217217

218-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
218+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "experiment set")

src/mavedb/lib/permissions/score_calibration.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def has_permission(user_data: Optional[UserData], entity: ScoreCalibration, acti
6464
if action not in handlers:
6565
supported_actions = ", ".join(a.value for a in handlers.keys())
6666
raise NotImplementedError(
67-
f"Action '{action.value}' is not supported for ScoreCalibration entities. "
67+
f"Action '{action.value}' is not supported for score calibration entities. "
6868
f"Supported actions: {supported_actions}"
6969
)
7070

@@ -119,7 +119,7 @@ def _handle_read_action(
119119
return PermissionResponse(True)
120120

121121
user_may_view_private = user_is_owner or (entity.investigator_provided and user_is_contributor_to_score_set)
122-
return deny_action_for_entity(entity, private, user_data, user_may_view_private)
122+
return deny_action_for_entity(entity, private, user_data, user_may_view_private, "score calibration")
123123

124124

125125
def _handle_update_action(
@@ -162,7 +162,7 @@ def _handle_update_action(
162162
return PermissionResponse(True)
163163

164164
user_may_view_private = user_is_owner or (entity.investigator_provided and user_is_contributor_to_score_set)
165-
return deny_action_for_entity(entity, private, user_data, user_may_view_private)
165+
return deny_action_for_entity(entity, private, user_data, user_may_view_private, "score calibration")
166166

167167

168168
def _handle_delete_action(
@@ -198,7 +198,7 @@ def _handle_delete_action(
198198
return PermissionResponse(True)
199199

200200
user_may_view_private = user_is_owner or (entity.investigator_provided and user_is_contributor_to_score_set)
201-
return deny_action_for_entity(entity, private, user_data, user_may_view_private)
201+
return deny_action_for_entity(entity, private, user_data, user_may_view_private, "score calibration")
202202

203203

204204
def _handle_publish_action(
@@ -235,7 +235,7 @@ def _handle_publish_action(
235235
return PermissionResponse(True)
236236

237237
user_may_view_private = user_is_owner or (entity.investigator_provided and user_is_contributor_to_score_set)
238-
return deny_action_for_entity(entity, private, user_data, user_may_view_private)
238+
return deny_action_for_entity(entity, private, user_data, user_may_view_private, "score calibration")
239239

240240

241241
def _handle_change_rank_action(
@@ -274,4 +274,4 @@ def _handle_change_rank_action(
274274
return PermissionResponse(True)
275275

276276
user_may_view_private = user_is_owner or (entity.investigator_provided and user_is_contributor_to_score_set)
277-
return deny_action_for_entity(entity, private, user_data, user_may_view_private)
277+
return deny_action_for_entity(entity, private, user_data, user_may_view_private, "score calibration")

src/mavedb/lib/permissions/score_set.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def has_permission(user_data: Optional[UserData], entity: ScoreSet, action: Acti
5959
if action not in handlers:
6060
supported_actions = ", ".join(a.value for a in handlers.keys())
6161
raise NotImplementedError(
62-
f"Action '{action.value}' is not supported for ScoreSet entities. "
62+
f"Action '{action.value}' is not supported for score set entities. "
6363
f"Supported actions: {supported_actions}"
6464
)
6565

@@ -109,7 +109,7 @@ def _handle_read_action(
109109
if roles_permitted(active_roles, [UserRole.admin, UserRole.mapper]):
110110
return PermissionResponse(True)
111111

112-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
112+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "score set")
113113

114114

115115
def _handle_update_action(
@@ -144,7 +144,7 @@ def _handle_update_action(
144144
if roles_permitted(active_roles, [UserRole.admin]):
145145
return PermissionResponse(True)
146146

147-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
147+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "score set")
148148

149149

150150
def _handle_delete_action(
@@ -180,7 +180,7 @@ def _handle_delete_action(
180180
if user_is_owner and private:
181181
return PermissionResponse(True)
182182

183-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
183+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "score set")
184184

185185

186186
def _handle_publish_action(
@@ -216,7 +216,7 @@ def _handle_publish_action(
216216
if roles_permitted(active_roles, [UserRole.admin]):
217217
return PermissionResponse(True)
218218

219-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
219+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "score set")
220220

221221

222222
def _handle_set_scores_action(
@@ -252,4 +252,4 @@ def _handle_set_scores_action(
252252
if roles_permitted(active_roles, [UserRole.admin]):
253253
return PermissionResponse(True)
254254

255-
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner)
255+
return deny_action_for_entity(entity, private, user_data, user_is_contributor or user_is_owner, "score set")

src/mavedb/lib/permissions/user.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def has_permission(user_data: Optional[UserData], entity: User, action: Action)
5656
if action not in handlers:
5757
supported_actions = ", ".join(a.value for a in handlers.keys())
5858
raise NotImplementedError(
59-
f"Action '{action.value}' is not supported for User entities. " f"Supported actions: {supported_actions}"
59+
f"Action '{action.value}' is not supported for user profile entities. "
60+
f"Supported actions: {supported_actions}"
6061
)
6162

6263
return handlers[action](
@@ -101,7 +102,7 @@ def _handle_read_action(
101102
if roles_permitted(active_roles, [UserRole.admin]):
102103
return PermissionResponse(True)
103104

104-
return deny_action_for_entity(entity, False, user_data, False)
105+
return deny_action_for_entity(entity, False, user_data, False, "user profile")
105106

106107

107108
def _handle_lookup_action(
@@ -129,7 +130,7 @@ def _handle_lookup_action(
129130
if user_data is not None and user_data.user is not None:
130131
return PermissionResponse(True)
131132

132-
return deny_action_for_entity(entity, False, user_data, False)
133+
return deny_action_for_entity(entity, False, user_data, False, "user profile")
133134

134135

135136
def _handle_update_action(
@@ -160,7 +161,7 @@ def _handle_update_action(
160161
if roles_permitted(active_roles, [UserRole.admin]):
161162
return PermissionResponse(True)
162163

163-
return deny_action_for_entity(entity, False, user_data, False)
164+
return deny_action_for_entity(entity, False, user_data, False, "user profile")
164165

165166

166167
def _handle_add_role_action(
@@ -188,4 +189,4 @@ def _handle_add_role_action(
188189
if roles_permitted(active_roles, [UserRole.admin]):
189190
return PermissionResponse(True)
190191

191-
return deny_action_for_entity(entity, False, user_data, False)
192+
return deny_action_for_entity(entity, False, user_data, False, "user profile")

src/mavedb/lib/permissions/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def deny_action_for_entity(
8686
private: bool,
8787
user_data: Optional[UserData],
8888
user_may_view_private: bool,
89+
user_facing_model_name: str = "entity",
8990
) -> PermissionResponse:
9091
"""
9192
Generate appropriate denial response for entity permission checks.
@@ -118,14 +119,14 @@ def _identifier_for_entity(entity: EntityType) -> tuple[str, str]:
118119
field, identifier = _identifier_for_entity(entity)
119120
# Do not acknowledge the existence of a private score set.
120121
if private and not user_may_view_private:
121-
return PermissionResponse(False, 404, f"{entity.__class__.__name__} with {field} '{identifier}' not found")
122+
return PermissionResponse(False, 404, f"{user_facing_model_name} with {field} '{identifier}' not found")
122123
# No authenticated user is present.
123124
if user_data is None or user_data.user is None:
124125
return PermissionResponse(
125-
False, 401, f"authentication required to access {entity.__class__.__name__} with {field} '{identifier}'"
126+
False, 401, f"authentication required to access {user_facing_model_name} with {field} '{identifier}'"
126127
)
127128

128129
# The authenticated user lacks sufficient permissions.
129130
return PermissionResponse(
130-
False, 403, f"insufficient permissions on {entity.__class__.__name__} with {field} '{identifier}'"
131+
False, 403, f"insufficient permissions on {user_facing_model_name} with {field} '{identifier}'"
131132
)

src/mavedb/routers/experiment_sets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def fetch_experiment_set(
5757
# the exception is raised, not returned - you will get a validation
5858
# error otherwise.
5959
logger.debug(msg="The requested resources does not exist.", extra=logging_context())
60-
raise HTTPException(status_code=404, detail=f"Experiment set with URN {urn} not found")
60+
raise HTTPException(status_code=404, detail=f"experiment set with URN {urn} not found")
6161
else:
6262
item.experiments.sort(key=attrgetter("urn"))
6363

src/mavedb/routers/experiments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def fetch_experiment(
155155

156156
if not item:
157157
logger.debug(msg="The requested experiment does not exist.", extra=logging_context())
158-
raise HTTPException(status_code=404, detail=f"Experiment with URN {urn} not found")
158+
raise HTTPException(status_code=404, detail=f"experiment with URN {urn} not found")
159159

160160
assert_permission(user_data, item, Action.READ)
161161
return enrich_experiment_with_num_score_sets(item, user_data)
@@ -247,7 +247,7 @@ async def create_experiment(
247247
)
248248
raise HTTPException(
249249
status_code=404,
250-
detail=f"ExperimentSet with URN '{item_create.experiment_set_urn}' not found.",
250+
detail=f"experiment set with URN '{item_create.experiment_set_urn}' not found.",
251251
)
252252

253253
save_to_logging_context({"experiment_set": experiment_set.urn})

src/mavedb/routers/score_calibrations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def get_score_calibrations_for_score_set(
8484

8585
if not score_set:
8686
logger.debug("ScoreSet not found", extra=logging_context())
87-
raise HTTPException(status_code=404, detail=f"ScoreSet with URN '{score_set_urn}' not found")
87+
raise HTTPException(status_code=404, detail=f"score set with URN '{score_set_urn}' not found")
8888

8989
assert_permission(user_data, score_set, Action.READ)
9090

@@ -124,7 +124,7 @@ async def get_primary_score_calibrations_for_score_set(
124124
score_set = db.query(ScoreSet).filter(ScoreSet.urn == score_set_urn).one_or_none()
125125
if not score_set:
126126
logger.debug("ScoreSet not found", extra=logging_context())
127-
raise HTTPException(status_code=404, detail=f"ScoreSet with URN '{score_set_urn}' not found")
127+
raise HTTPException(status_code=404, detail=f"score set with URN '{score_set_urn}' not found")
128128

129129
assert_permission(user_data, score_set, Action.READ)
130130

@@ -184,7 +184,7 @@ async def create_score_calibration_route(
184184
score_set = db.query(ScoreSet).filter(ScoreSet.urn == calibration.score_set_urn).one_or_none()
185185
if not score_set:
186186
logger.debug("ScoreSet not found", extra=logging_context())
187-
raise HTTPException(status_code=404, detail=f"ScoreSet with URN '{calibration.score_set_urn}' not found")
187+
raise HTTPException(status_code=404, detail=f"score set with URN '{calibration.score_set_urn}' not found")
188188

189189
# TODO#539: Allow any authenticated user to upload a score calibration for a score set, not just those with
190190
# permission to update the score set itself.
@@ -222,7 +222,7 @@ async def modify_score_calibration_route(
222222
if not score_set:
223223
logger.debug("ScoreSet not found", extra=logging_context())
224224
raise HTTPException(
225-
status_code=404, detail=f"ScoreSet with URN '{calibration_update.score_set_urn}' not found"
225+
status_code=404, detail=f"score set with URN '{calibration_update.score_set_urn}' not found"
226226
)
227227

228228
# TODO#539: Allow any authenticated user to upload a score calibration for a score set, not just those with

0 commit comments

Comments
 (0)