Skip to content

Commit b68547c

Browse files
committed
Improve error handling in screenshot capture: return None for failures and add error message display in Gradio app
1 parent 2b36902 commit b68547c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/modules/apps/__init__.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def capture_and_show(url: str):
6262
return temp_path
6363
except Exception as e:
6464
print(f"Error in capture_and_show: {str(e)}") # Add detailed logging
65-
return f"Error capturing page: {str(e)}"
65+
return None # Return None instead of error string to handle gracefully
6666

6767
def create_gradio_app():
6868
"""Create the main Gradio application with all components"""
@@ -83,11 +83,32 @@ def create_gradio_app():
8383
type="filepath"
8484
)
8585

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+
86107
# Connect the components
87108
capture_btn.click(
88-
fn=capture_and_show,
109+
fn=capture_with_error,
89110
inputs=[url_input],
90-
outputs=[output_image]
111+
outputs=[output_image, error_output]
91112
)
92113

93114
return app

0 commit comments

Comments
 (0)