Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Standardize error messages for sd-export
Browse files Browse the repository at this point in the history
All errors return codes are now prefixed with `ERROR_`
  • Loading branch information
emkll committed Jul 25, 2019
1 parent 9763192 commit f511756
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
23 changes: 9 additions & 14 deletions securedrop_export/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def exit_gracefully(self, msg, e=False):
sys.stderr.write("\n")
# exit with 0 return code otherwise the os will attempt to open
# the file with another application
self.popup_message("Export error: {}".format(msg))
sys.exit(0)


Expand Down Expand Up @@ -172,7 +171,7 @@ def unlock_luks_volume(self, encryption_key):
p.communicate(input=str.encode(encryption_key, "utf-8"))
rc = p.returncode
if rc != 0:
msg = "Bad passphrase or luks error."
msg = "USB_BAD_PASSPHRASE"
self.exit_gracefully(msg)


Expand All @@ -199,7 +198,7 @@ def mount_volume(self):
except subprocess.CalledProcessError as e:
# clean up
subprocess.check_call(["sudo", "cryptsetup", "luksClose", self.encrypted_device])
msg = "An error occurred while mounting disk: "
msg = "ERROR_USB_MOUNT"
self.exit_gracefully(msg, e=e)


Expand All @@ -214,7 +213,7 @@ def copy_submission(self):
subprocess.check_call(["cp", "-r", export_data, target_path])
self.popup_message("Files exported successfully to disk.")
except (subprocess.CalledProcessError, OSError) as e:
msg = "Error writing to disk:"
msg = "ERROR_USB_WRITE"
self.exit_gracefully(msg, e=e)
finally:
# Finally, we sync the filesystem, unmount the drive and lock the
Expand Down Expand Up @@ -254,7 +253,7 @@ def get_printer_uri(self):
try:
output = subprocess.check_output(["sudo", "lpinfo", "-v"])
except subprocess.CalledProcessError as e:
msg = "Error retrieving printer uri."
msg = "ERROR_PRINTER_URI"
self.exit_gracefully(msg, e=e)

# fetch the usb printer uri
Expand All @@ -265,12 +264,12 @@ def get_printer_uri(self):
# verify that the printer is supported, else exit
if printer_uri == "":
# No usb printer is connected
self.exit_gracefully("USB Printer not found")
self.exit_gracefully("ERROR_PRINTER_NOT_FOUND")
elif "Brother" in printer_uri:
return printer_uri
else:
# printer url is a make that is unsupported
self.exit_gracefully("USB Printer not supported")
self.exit_gracefully("ERROR_PRINTER_NOT_SUPPORTED")


def install_printer_ppd(self, uri):
Expand All @@ -281,7 +280,7 @@ def install_printer_ppd(self, uri):
["sudo", "ppdc", self.brlaser_driver, "-d", "/usr/share/cups/model/"]
)
except subprocess.CalledProcessError as e:
msg = "Error installing ppd file for printer {}.".format(uri)
msg = "ERROR_PRINTER_DRIVER_INSTALL"
self.exit_gracefully(msg, e=e)
return self.brlaser_ppd
# Here, we could support ppd drivers for other makes or models in the future
Expand Down Expand Up @@ -309,9 +308,7 @@ def setup_printer(self, printer_uri, printer_ppd):
["sudo", "lpadmin", "-p", self.printer_name, "-u", "allow:user"]
)
except subprocess.CalledProcessError as e:
msg = "Error setting up printer {} at {} using {}.".format(
self.printer_name, printer_uri, printer_ppd
)
msg = "ERROR_PRINTER_INSTALL"
self.exit_gracefully(msg, e=e)


Expand Down Expand Up @@ -354,9 +351,7 @@ def print_file(self, file_to_print):

subprocess.check_call(["xpp", "-P", self.printer_name, file_to_print])
except subprocess.CalledProcessError as e:
msg = "Error printing file {} with printer {}.".format(
file_to_print, self.printer_name
)
msg = "ERROR_PRINT"
self.exit_gracefully(msg, e=e)

## class ends here
Expand Down
4 changes: 2 additions & 2 deletions securedrop_export/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __main__(submission):
try:
submission.archive_metadata = export.Metadata(submission.tmpdir)
except Exception as e:
msg = "Error parsing metadata: "
msg = "ERROR_METADATA_PARSING"
submission.exit_gracefully(msg, e=e)

if submission.archive_metadata.is_valid():
Expand All @@ -36,6 +36,6 @@ def __main__(submission):
submission.setup_printer(printer_uri, printer_ppd)
submission.print_test_page()
else:
submission.exit_gracefully("Archive metadata is invalid")
submission.exit_gracefully("ERROR_ARCHIVE_METADATA")


2 changes: 1 addition & 1 deletion tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_get_good_printer_uri(mocked_call):
@mock.patch("subprocess.check_output", return_value=SAMPLE_OUTPUT_NO_PRINTER)
def test_get_bad_printer_uri(mocked_call, capsys):
submission = export.SDExport("testfile")
expected_message = "USB Printer not found"
expected_message = "ERROR_PRINTER_NOT_FOUND"
mocked_exit = mock.patch("export.exit_gracefully", return_value=0)

with pytest.raises(SystemExit) as sysexit:
Expand Down

0 comments on commit f511756

Please sign in to comment.