@@ -82,7 +82,7 @@ def get_universe(universe_id):
82
82
universe = Universe .get_by_id (session , universe_id )
83
83
84
84
if not universe :
85
- raise ValidationError ('Universe not found' )
85
+ raise NotFoundError ('Universe not found' )
86
86
87
87
# Check if user has access (owner or public universe)
88
88
if not (universe .is_public or str (universe .user_id ) == str (current_user_id )):
@@ -94,7 +94,7 @@ def get_universe(universe_id):
94
94
95
95
return jsonify (response_data )
96
96
97
- except (ValidationError , AuthorizationError ) as e :
97
+ except (ValidationError , AuthorizationError , NotFoundError ) as e :
98
98
session .rollback ()
99
99
raise
100
100
except Exception as e :
@@ -110,6 +110,15 @@ def update_universe(universe_id):
110
110
if not data :
111
111
raise ValidationError ('No input data provided' )
112
112
113
+ # Check for invalid fields
114
+ allowed_fields = {'name' , 'description' , 'is_public' }
115
+ invalid_fields = set (data .keys ()) - allowed_fields
116
+ if invalid_fields :
117
+ return jsonify ({
118
+ 'error_code' : 'ValidationError' ,
119
+ 'message' : f'Invalid fields: { ", " .join (invalid_fields )} '
120
+ }), 400
121
+
113
122
with get_db () as db :
114
123
universe = Universe .get_by_id (db , universe_id )
115
124
if not universe :
@@ -121,7 +130,6 @@ def update_universe(universe_id):
121
130
raise AuthorizationError ('Not authorized to modify this universe' )
122
131
123
132
# Update allowed fields
124
- allowed_fields = {'name' , 'description' , 'is_public' }
125
133
update_data = {k : v for k , v in data .items () if k in allowed_fields }
126
134
127
135
for key , value in update_data .items ():
@@ -187,27 +195,50 @@ def update_physics(universe_id):
187
195
188
196
data = request .get_json ()
189
197
if not data or 'physics_params' not in data :
190
- raise ValidationError ( 'Invalid physics parameters' )
191
-
192
- # Update physics parameters
193
- universe . update_physics ( data [ 'physics_params' ])
198
+ return jsonify ({
199
+ 'error_code' : 'ValidationError' ,
200
+ 'message' : 'Invalid physics parameters'
201
+ }), 400
194
202
195
- # Add universe to session and commit
196
- db .add (universe )
197
- db .commit ()
198
-
199
- # Return complete universe data with user role
200
- response_data = universe .to_dict ()
201
- response_data ['user_role' ] = 'owner'
202
- return jsonify (response_data )
203
-
204
- except (ValidationError , AuthorizationError ) as e :
203
+ try :
204
+ # Update physics parameters
205
+ universe .update_physics (data ['physics_params' ])
206
+
207
+ # Add universe to session and commit
208
+ db .add (universe )
209
+ db .commit ()
210
+
211
+ # Return complete universe data with user role
212
+ response_data = universe .to_dict ()
213
+ response_data ['user_role' ] = 'owner'
214
+ return jsonify (response_data )
215
+
216
+ except ValueError as e :
217
+ db .rollback ()
218
+ return jsonify ({
219
+ 'error_code' : 'ValidationError' ,
220
+ 'message' : str (e )
221
+ }), 400
222
+
223
+ except ValidationError as e :
205
224
db .rollback ()
206
- raise
225
+ return jsonify ({
226
+ 'error_code' : 'ValidationError' ,
227
+ 'message' : str (e )
228
+ }), 400
229
+ except AuthorizationError as e :
230
+ db .rollback ()
231
+ return jsonify ({
232
+ 'error_code' : 'AuthorizationError' ,
233
+ 'message' : str (e )
234
+ }), 403
207
235
except Exception as e :
208
236
db .rollback ()
209
237
logger .error (f"Error updating physics: { str (e )} " , exc_info = True )
210
- raise ValidationError (f"Error updating physics: { str (e )} " )
238
+ return jsonify ({
239
+ 'error_code' : 'InternalError' ,
240
+ 'message' : 'An internal error occurred'
241
+ }), 500
211
242
212
243
@universe_bp .route ('/<uuid:universe_id>/harmony/' , methods = ['PUT' ])
213
244
@jwt_required ()
0 commit comments