summaryrefslogtreecommitdiff
path: root/lib/exceptions.py
blob: c79c771e83f3aff23be693af94bf89980de81843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.
import traceback
import sys
import platform
import subprocess
from glob import glob


class InkstitchException(Exception):
    pass


def get_os_version():
    if sys.platform == "win32":
        # To get the windows version, python functions are used
        # Using python subprocess with cmd.exe in windows is currently a security risk
        os_ver = "Windows " + platform.release() + " version: " + platform.version()
    if sys.platform == "darwin":
        # macOS command line progam provides accurate info than python functions
        mac_v1 = subprocess.run(["sw_vers"], capture_output=True, text=True)
        mac_v1 = str(mac_v1.stdout.strip())
        mac_v2 = subprocess.run(["uname",  "-m"], capture_output=True, text=True)
        mac_v2 = str(mac_v2.stdout.strip())
        os_ver = mac_v1 + "\nCPU:\t\t\t\t" + mac_v2
    if sys.platform == "linux":
        # Getting linux version method used here is for systemd and nonsystemd linux.
        try:
            ltmp = subprocess.run(["cat"] + glob("/etc/*-release"), capture_output=True, text=True)
            lnx_ver = ltmp.stdout.splitlines()
            lnx_ver = str(list(filter(lambda x: "PRETTY_NAME" in x, lnx_ver)))
            os_ver = lnx_ver[15:][:-3]
        except FileNotFoundError:
            os_ver = "Cannot get Linux distro version"

    return os_ver


def format_uncaught_exception():
    """Format the current exception as a request for a bug report.

    Call this inside an except block so that there is an exception that we can
    call traceback.format_exc() on.
    """

    # importing locally to avoid any possibility of circular import
    from lib.utils import version
    from .i18n import _

    message = ""
    message += _("Ink/Stitch experienced an unexpected error. This means it is a bug in Ink/Stitch.")
    message += "\n\n"
    # L10N this message is followed by a URL: https://github.com/inkstitch/inkstitch/issues/new
    message += _("If you'd like to help please\n"
                 "- copy the entire error message below\n"
                 "- save your SVG file and\n"
                 "- create a new issue at")
    message += " https://github.com/inkstitch/inkstitch/issues/new\n\n"
    message += _("Include the error description and also (if possible) the svg file.")
    message += '\n\n'
    message += get_os_version()
    message += '\n\n'
    message += version.get_inkstitch_version() + '\n'
    message += traceback.format_exc()

    return message