# Buggy code
def send_email(success, service_name, doctype, email_field, error_status=None):
    recipients = get_recipients(service_name, email_field)
    if not recipients:
        frappe.log_error(
            "No Email Recipient found for {0}".format(service_name),
            "{0}: Failed to send backup status email".format(service_name),
        )
        return

    if success:
        if not frappe.db.get_value(doctype, None, "send_email_for_successful_backup"):
            return

        subject = "Backup Upload Successful"
        message = """
<h3>Backup Uploaded Successfully!</h3>
<p>Hi there, this is just to inform you that your backup was successfully uploaded to your {0} bucket. So relax!</p>""".format(
            service_name
        )

    else:
        subject = "[Warning] Backup Upload Failed"
        message = """
<h3>Backup Upload Failed!</h3>
<p>Oops, your automated backup to {0} failed.</p>
<p>Error message: {1}</p>
<p>Please contact your system manager for more information.</p>""".format(
            service_name, error_status
        )

    frappe.sendmail(recipients=recipients, subject=subject, message=message)


# Traceback (KeyError)
[2020-04-23 16:08:24 +0000] [3664] [ERROR] Error handling request /
Traceback (most recent call last):
File "/home/ubuntu/frappe-bench/apps/frappe/frappe/app.py", line 53, in application
init_request(request)
File "/home/ubuntu/frappe-bench/apps/frappe/frappe/app.py", line 108, in init_request
frappe.init(site=site, sites_path=_sites_path)
File "/home/ubuntu/frappe-bench/apps/frappe/frappe/__init__.py", line 150, in init
local.conf = _dict(get_site_config())
File "/home/ubuntu/frappe-bench/apps/frappe/frappe/__init__.py", line 224, in get_site_config
sys.exit(1)
SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 72, in __getattr__
return self.__storage__[self.__ident_func__()][name]
KeyError: 'conf'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 309, in _get_current_object
return getattr(self.__local, self.__name__)
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 74, in __getattr__
raise AttributeError(name)
AttributeError: conf

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 231, in application
return ClosingIterator(app(environ, start_response), self.cleanup)
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/wrappers/base_request.py", line 237, in application
resp = f(*args[:-2] + (request,))
File "/home/ubuntu/frappe-bench/apps/frappe/frappe/app.py", line 96, in application
frappe.monitor.stop(response)
File "/home/ubuntu/frappe-bench/apps/frappe/frappe/monitor.py", line 26, in stop
if frappe.conf.monitor and hasattr(frappe.local, "monitor"):
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 348, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/ubuntu/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 311, in _get_current_object
raise RuntimeError("no object bound to %s" % self.__name__)
RuntimeError: no object bound to conf

# Fixed code
def send_email(success, service_name, doctype, email_field, error_status=None):
    recipients = get_recipients(doctype, email_field)
    if not recipients:
        frappe.log_error(
            "No Email Recipient found for {0}".format(service_name),
            "{0}: Failed to send backup status email".format(service_name),
        )
        return

    if success:
        if not frappe.db.get_value(doctype, None, "send_email_for_successful_backup"):
            return

        subject = "Backup Upload Successful"
        message = """
<h3>Backup Uploaded Successfully!</h3>
<p>Hi there, this is just to inform you that your backup was successfully uploaded to your {0} bucket. So relax!</p>""".format(
            service_name
        )

    else:
        subject = "[Warning] Backup Upload Failed"
        message = """
<h3>Backup Upload Failed!</h3>
<p>Oops, your automated backup to {0} failed.</p>
<p>Error message: {1}</p>
<p>Please contact your system manager for more information.</p>""".format(
            service_name, error_status
        )

    frappe.sendmail(recipients=recipients, subject=subject, message=message)