GuglielmoTor commited on
Commit
a7b157f
·
verified ·
1 Parent(s): 57d921c

Create error_handling.py

Browse files
Files changed (1) hide show
  1. error_handling.py +42 -0
error_handling.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import traceback
2
+ import gradio as gr
3
+ import requests
4
+ import html
5
+
6
+ def display_error(message, e=None):
7
+ """Formats an error message for display in Gradio. Returns a gr.update object."""
8
+ error_prefix = "❌ Error: "
9
+ full_message = f"{error_prefix}{message}"
10
+ if e:
11
+ tb = traceback.format_exc()
12
+ print(f"--- ERROR ---")
13
+ print(f"Message: {message}")
14
+ print(f"Exception Type: {type(e)}")
15
+ print(f"Exception: {e}")
16
+ # Avoid printing traceback for simple Warnings like scope changes unless debugging deep
17
+ if not isinstance(e, Warning):
18
+ print(f"Traceback:\n{tb}")
19
+ print(f"-------------")
20
+
21
+ # Try to get more details from response if it's a requests error
22
+ if isinstance(e, requests.exceptions.RequestException) and e.response is not None:
23
+ try:
24
+ error_details = e.response.json()
25
+ details_str = json.dumps(error_details, indent=2)
26
+ full_message += f"\nStatus Code: {e.response.status_code}\nDetails:\n```json\n{details_str}\n```"
27
+ except json.JSONDecodeError:
28
+ full_message += f"\nStatus Code: {e.response.status_code}\nResponse Text:\n```\n{e.response.text}\n```"
29
+ elif hasattr(e, 'description'): # Handle OAuthLib errors which often have a description
30
+ full_message += f"\nDetails: {getattr(e, 'description', str(e))}"
31
+ else:
32
+ # Display the specific warning/error message directly
33
+ full_message += f"\nDetails: {str(e)}"
34
+ else:
35
+ print(f"Error: {message}") # Log simple message
36
+
37
+ # Use Markdown for better formatting in Gradio output
38
+ # Ensure it's wrapped in a way that Gradio Markdown understands as an error block if possible
39
+ # Simple red text might be best cross-platform
40
+ error_html = f"<p style='color: red; white-space: pre-wrap;'>{html.escape(full_message)}</p>"
41
+
42
+ return gr.update(value=error_html, visible=True)