feat: handle errors

This commit is contained in:
0xMRTT 2022-10-02 12:20:02 +02:00
parent 0b3d390630
commit 58a25368d0
No known key found for this signature in database
GPG Key ID: AC9E06BF3DECB6FB
5 changed files with 81 additions and 59 deletions

View File

@ -383,7 +383,7 @@ def get_permission() -> bool:
return False
def upload_data(address: str, data: dict) -> bool:
def upload_data(address: str, data: dict) -> requests.Response:
"""Upload collected data to address via HTTP post request
@param address: HTTP address of recieving server
@ -391,62 +391,15 @@ def upload_data(address: str, data: dict) -> bool:
@return: False if error occured, True if successful
"""
try:
print("Uploading...")
print("Uploading...")
# ~ Send the data
r = requests.post(address, data=json.dumps(data))
# ~ Raise HTTPError if request returned an unsuccessful status code
r.raise_for_status()
except requests.HTTPError:
print(f"Status {r.status_code}: An HTTP error occured.")
print(f"Server message: {r.text}")
return False
except requests.ConnectionError:
print("Connection Error: Error connecting to the server.")
print("Please check your internet connection and try again.\n")
raise
except requests.Timeout:
print("Timeout error: Request timed out.")
print("Please check your internet connection and try again.\n")
return False
except Exception:
print("Unknown error: Sending data unsuccessful, please, try again.\n")
raise
else:
# ~ No errors, print server output
print(f"Status {r.status_code}: {r.text}")
# ~ Prevent user from double-sending
create_status_file()
return True
def main():
# ~ Address of a server to send the data to
ADDRESS = "https://gnome-info-collect-app.apps.openshift4.gnome.org"
check_already_uploaded()
output = GCollector().collect_data()
# ~ Validate the data and convert to dict-like format for better processing
try:
json.dumps(output)
except ValueError:
print("Error loading json data: invalid format.")
raise
present_collected_data(output)
if not get_permission():
print("Exiting...")
return
if upload_data(ADDRESS, output):
# ~ Data successfully uploaded, finish
print("Complete! Thank you for helping to improve GNOME.")
# ~ Send the data
r = requests.post(address, data=json.dumps(data))
# ~ Raise HTTPError if request returned an unsuccessful status code
r.raise_for_status()
return r
if __name__ == "__main__":
main()

View File

@ -31,7 +31,9 @@ import gi
import json
import os
from .client import GCollector, STATUS_FILE, upload_data
from .client import GCollector, STATUS_FILE, upload_data, create_status_file
import requests
import json
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
@ -73,8 +75,34 @@ class AccumulateApplication(Adw.Application):
def send_data(self, _unused, response):
if response == "send":
upload_data(self.server, self.data)
self.win.view_stack.set_visible_child(self.win.success)
try:
r = requests.post(self.server, data=json.dumps(self.data))
# ~ Raise HTTPError if request returned an unsuccessful status code
r.raise_for_status()
except requests.HTTPError:
self.win.error.set_title(f"{r.status_code}: An HTTP error occured")
self.win.error.set_description(f"Server message: {str(r.text)}")
self.win.error_content.set_label(str(r.content))
self.win.error_content.set_visible(True)
self.win.view_stack.set_visible_child(self.win.error_stack)
except requests.ConnectionError:
self.win.error.set_title("Error connecting to the server")
self.win.error.set_description("Please check your internet connection and try again")
self.win.view_stack.set_visible_child(self.win.error_stack)
except requests.Timeout:
self.win.error.set_title("Request timed out")
self.win.error.set_description("Please check your internet connection and try again")
self.win.view_stack.set_visible_child(self.win.error_stack)
except Exception:
self.win.error.set_title("Unknown error")
self.win.error.set_description("Sending data unsuccessful, please, try again")
self.win.view_stack.set_visible_child(self.win.error_stack)
else:
# ~ No errors, print server output
print(f"Status {r.status_code}: {r.text}")
# ~ Prevent user from double-sending
create_status_file()
self.win.view_stack.set_visible_child(self.win.success)
def do_activate(self):
"""Called when the application is activated.

View File

@ -32,6 +32,9 @@ class AccumulateWindow(Adw.ApplicationWindow):
view_stack = Gtk.Template.Child()
success = Gtk.Template.Child()
main = Gtk.Template.Child()
error = Gtk.Template.Child()
error_stack = Gtk.Template.Child()
error_content = Gtk.Template.Child()
content = Gtk.Template.Child()
already_submitted = Gtk.Template.Child()

View File

@ -222,6 +222,42 @@ template AccumulateWindow : Adw.ApplicationWindow {
}
}
Gtk.Stack error_stack {
hexpand: true;
overflow: hidden;
Adw.StatusPage error {
title: _("Error while sending data");
description: _("Please report an issue to the GNOME Team.");
icon-name: "error-symbolic";
Gtk.Box {
margin-end: 50;
margin-start: 50;
styles ["card"]
Gtk.ScrolledWindow {
margin-start: 12;
margin-end: 12;
min-content-height: 200;
overflow: hidden;
Gtk.Label error_content {
hexpand: true;
vexpand: true;
visible: false;
selectable: true;
wrap: true;
xalign: 0;
yalign: 0;
styles ["monospace", "terminal"]
}
}
}
}
}
Gtk.Stack already_submitted {
hexpand: true;
overflow: hidden;

View File

@ -6,4 +6,6 @@
# And move output file to build-aux directory:
# mv pypi-dependencies.json build-aux/flatpak/
#
# or more simply, just push and the bot will do this for you.
# or more simply, just push and the bot will do this for you.
requests