npv2k1 commited on
Commit
b68547c
·
1 Parent(s): 2b36902

Improve error handling in screenshot capture: return None for failures and add error message display in Gradio app

Browse files
Files changed (1) hide show
  1. src/modules/apps/__init__.py +24 -3
src/modules/apps/__init__.py CHANGED
@@ -62,7 +62,7 @@ def capture_and_show(url: str):
62
  return temp_path
63
  except Exception as e:
64
  print(f"Error in capture_and_show: {str(e)}") # Add detailed logging
65
- return f"Error capturing page: {str(e)}"
66
 
67
  def create_gradio_app():
68
  """Create the main Gradio application with all components"""
@@ -83,11 +83,32 @@ def create_gradio_app():
83
  type="filepath"
84
  )
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  # Connect the components
87
  capture_btn.click(
88
- fn=capture_and_show,
89
  inputs=[url_input],
90
- outputs=[output_image]
91
  )
92
 
93
  return app
 
62
  return temp_path
63
  except Exception as e:
64
  print(f"Error in capture_and_show: {str(e)}") # Add detailed logging
65
+ return None # Return None instead of error string to handle gracefully
66
 
67
  def create_gradio_app():
68
  """Create the main Gradio application with all components"""
 
83
  type="filepath"
84
  )
85
 
86
+ error_output = gr.Textbox(
87
+ label="Error Message",
88
+ visible=False,
89
+ interactive=False
90
+ )
91
+
92
+ def capture_with_error(url):
93
+ try:
94
+ # Basic URL validation
95
+ if not url:
96
+ return None, gr.update(visible=True, value="Please enter a URL")
97
+ if not url.startswith(('http://', 'https://')):
98
+ return None, gr.update(visible=True, value="Please enter a valid URL starting with http:// or https://")
99
+
100
+ result = capture_and_show(url)
101
+ if result is None:
102
+ return None, gr.update(visible=True, value="Failed to capture screenshot. Please check the URL and try again.")
103
+ return result, gr.update(visible=False, value="")
104
+ except Exception as e:
105
+ return None, gr.update(visible=True, value=f"Error: {str(e)}")
106
+
107
  # Connect the components
108
  capture_btn.click(
109
+ fn=capture_with_error,
110
  inputs=[url_input],
111
+ outputs=[output_image, error_output]
112
  )
113
 
114
  return app