$P@D$3RV£R commited on
Commit
5c6c8c9
·
1 Parent(s): 66c33cb

Fix 500 error: add comprehensive error handling for folder/image data access

Browse files
Files changed (1) hide show
  1. app.py +69 -22
app.py CHANGED
@@ -320,36 +320,83 @@ def tagger():
320
  </html>
321
  """, 500
322
 
323
- # Loop back to start if we've gone past the end
 
 
 
 
324
  if app.config["HEAD"] >= len(folder_sets):
325
  app.config["HEAD"] = 0
326
  app.config["IMAGE_SET_INDEX"] = 0
327
  print("Reached end of folders, looping back to first folder")
328
 
329
- directory = app.config.get('IMAGES', '')
330
- current_folder_set = folder_sets[app.config["HEAD"]]
331
-
332
- # Get current image set index (default to 0 if not set)
333
- image_set_index = app.config.get("IMAGE_SET_INDEX", 0)
 
 
 
 
 
 
 
 
 
334
 
335
- # Get image sets for current folder
336
- image_sets = current_folder_set['image_sets']
337
- max_sets = len(image_sets)
 
 
 
338
 
339
- # Ensure image_set_index is within bounds
340
- if image_set_index >= max_sets:
341
- image_set_index = 0
342
- app.config["IMAGE_SET_INDEX"] = 0
343
 
344
- # Get current set of 3 images (all with same file ID prefix)
345
- current_images = []
346
- if image_set_index < max_sets:
347
- current_set = image_sets[image_set_index]
348
- current_images = [
349
- current_set['sr_int_full'],
350
- current_set['tr_line'],
351
- current_set['tr_int_full']
352
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
 
354
  labels = app.config["LABELS"]
355
  has_prev_folder = app.config["HEAD"] > 0
 
320
  </html>
321
  """, 500
322
 
323
+ # Ensure HEAD is initialized and within bounds
324
+ if "HEAD" not in app.config:
325
+ app.config["HEAD"] = 0
326
+ if app.config["HEAD"] < 0:
327
+ app.config["HEAD"] = 0
328
  if app.config["HEAD"] >= len(folder_sets):
329
  app.config["HEAD"] = 0
330
  app.config["IMAGE_SET_INDEX"] = 0
331
  print("Reached end of folders, looping back to first folder")
332
 
333
+ # Safely access current folder set
334
+ try:
335
+ directory = app.config.get('IMAGES', '')
336
+ current_folder_set = folder_sets[app.config["HEAD"]]
337
+
338
+ # Validate folder set structure
339
+ if not isinstance(current_folder_set, dict) or 'image_sets' not in current_folder_set:
340
+ raise ValueError(f"Invalid folder set structure at index {app.config['HEAD']}")
341
+
342
+ # Get current image set index (default to 0 if not set)
343
+ image_set_index = app.config.get("IMAGE_SET_INDEX", 0)
344
+ if image_set_index < 0:
345
+ image_set_index = 0
346
+ app.config["IMAGE_SET_INDEX"] = 0
347
 
348
+ # Get image sets for current folder
349
+ image_sets = current_folder_set['image_sets']
350
+ if not isinstance(image_sets, list) or len(image_sets) == 0:
351
+ raise ValueError(f"No image sets found in folder {current_folder_set.get('folder', 'unknown')}")
352
+
353
+ max_sets = len(image_sets)
354
 
355
+ # Ensure image_set_index is within bounds
356
+ if image_set_index >= max_sets:
357
+ image_set_index = 0
358
+ app.config["IMAGE_SET_INDEX"] = 0
359
 
360
+ # Get current set of 3 images (all with same file ID prefix)
361
+ current_images = []
362
+ if image_set_index < max_sets:
363
+ current_set = image_sets[image_set_index]
364
+ if not isinstance(current_set, dict):
365
+ raise ValueError(f"Invalid image set structure at index {image_set_index}")
366
+
367
+ # Validate required keys exist
368
+ required_keys = ['sr_int_full', 'tr_line', 'tr_int_full']
369
+ for key in required_keys:
370
+ if key not in current_set:
371
+ raise ValueError(f"Missing required image key '{key}' in image set {image_set_index}")
372
+
373
+ current_images = [
374
+ current_set['sr_int_full'],
375
+ current_set['tr_line'],
376
+ current_set['tr_int_full']
377
+ ]
378
+ else:
379
+ raise ValueError(f"Image set index {image_set_index} out of bounds (max: {max_sets})")
380
+
381
+ except (IndexError, KeyError, ValueError) as e:
382
+ print(f"Error accessing folder/image data: {e}")
383
+ import traceback
384
+ traceback.print_exc()
385
+ return f"""
386
+ <!DOCTYPE html>
387
+ <html>
388
+ <head>
389
+ <title>Data Access Error</title>
390
+ <meta charset="UTF-8">
391
+ </head>
392
+ <body>
393
+ <h1>Data Access Error</h1>
394
+ <p>An error occurred while accessing folder/image data.</p>
395
+ <p>Error: {str(e)}</p>
396
+ <p>Please check the Space logs for more details.</p>
397
+ </body>
398
+ </html>
399
+ """, 500
400
 
401
  labels = app.config["LABELS"]
402
  has_prev_folder = app.config["HEAD"] > 0