Verified Commit 97d39e49 authored by M. Sandoval's avatar M. Sandoval 🥑
Browse files

Improve GError handling

if e is of type GLib.Error e.message providers a short message, but we
can use e instead for a more complete description. We also make use of
try when errors might happen.
parent d8e26d22
Pipeline #258180 passed with stages
in 10 minutes and 7 seconds
......@@ -280,11 +280,17 @@ class DatabaseManager(GObject.GObject):
def create_keyfile_hash(self, keyfile_path: str) -> str:
"""Computes the SHA-1 hash of keyfile."""
gfile = Gio.File.new_for_path(keyfile_path)
gbytes, _stream = gfile.load_bytes()
return GLib.compute_checksum_for_bytes(
GLib.ChecksumType.SHA1, gbytes
)
try:
gbytes, _stream = gfile.load_bytes()
return GLib.compute_checksum_for_bytes(
GLib.ChecksumType.SHA1, gbytes
)
except GLib.Error as err:
logging.warning(
"Could not compute hash of %s: %s", keyfile_path, err.message
)
return ""
# Set keyfile hash
def set_keyfile_hash(self, keyfile_path):
......
......@@ -3,6 +3,7 @@ from __future__ import annotations
import typing
from gettext import gettext as _
from logging import warning
from gi.repository import Gio, GLib, GObject, Gtk
......@@ -349,10 +350,17 @@ class EntryPage(Gtk.ScrolledWindow):
if response == Gtk.ResponseType.ACCEPT:
safe_entry: SafeEntry = self.unlocked_database.current_element
for attachment in dialog.get_files():
byte_buffer = attachment.load_bytes()[0].get_data()
filename = attachment.get_basename()
new_attachment = safe_entry.add_attachment(byte_buffer, filename)
self.add_attachment_row(new_attachment)
try:
byte_buffer = attachment.load_bytes()[0].get_data()
filename = attachment.get_basename()
new_attachment = safe_entry.add_attachment(byte_buffer, filename)
self.add_attachment_row(new_attachment)
except GLib.Error as err:
warning(
"Could not create new keyfile %s: %s",
filename,
err.message,
)
def add_attachment_row(self, attachment):
builder = Gtk.Builder()
......
......@@ -39,11 +39,10 @@ def generate_keyfile(gfile: Gio.File, callback: Callable[[], None]) -> None:
ciphertext, tag = cipher.encrypt_and_digest(create_random_data(96)) # type: ignore
contents = cipher.nonce + tag + ciphertext # type: ignore
success, _ = gfile.replace_contents(
contents, None, False, Gio.FileCreateFlags.REPLACE_DESTINATION, None
)
if not success:
logging.debug("Could not create keyfile")
callback()
try:
gfile.replace_contents(
contents, None, False, Gio.FileCreateFlags.REPLACE_DESTINATION, None
)
callback()
except GLib.Error as err:
logging.debug("Could not create keyfile: %s", err.message)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment