# Buggy code
def process_message(
    message: message.Message, rcpt_to: Optional[Text] = None, pre_checked: bool = False
) -> None:
    subject_header = message.get("Subject", "(no subject)")
    encoded_subject, encoding = decode_header(subject_header)[0]
    if encoding is None:
        subject = force_text(
            encoded_subject
        )  # encoded_subject has type str when encoding is None
    else:
        try:
            subject = encoded_subject.decode(encoding)
        except (UnicodeDecodeError, LookupError):
            subject = "(unreadable subject)"

    debug_info = {}

    try:
        if rcpt_to is not None:
            to = rcpt_to
        else:
            to = find_emailgateway_recipient(message)
        debug_info["to"] = to

        if is_missed_message_address(to):
            process_missed_message(to, message, pre_checked)
        else:
            process_stream_message(to, subject, message, debug_info)
    except ZulipEmailForwardError as e:
        # TODO: notify sender of error, retry if appropriate.
        log_and_report(message, str(e), debug_info)


# Traceback (zerver.lib.exceptions.JsonableError)
Traceback (most recent call last):
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/worker/queue_processors.py", line 132, in consume_wrapper
self.consume(data)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/worker/queue_processors.py", line 423, in consume
rcpt_to=event["rcpt_to"], pre_checked=True)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/lib/email_mirror.py", line 339, in process_message
process_stream_message(to, subject, message, debug_info)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/lib/email_mirror.py", line 307, in process_stream_message
send_zulip(settings.EMAIL_GATEWAY_BOT, stream, subject, body)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/lib/email_mirror.py", line 200, in send_zulip
email_gateway=True)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/lib/actions.py", line 2021, in internal_send_message
realm=realm)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/lib/addressee.py", line 105, in legacy_build
return Addressee.for_stream(stream_name, topic_name)
File "/home/zulip/deployments/2018-04-23-18-38-07/zerver/lib/addressee.py", line 118, in for_stream
raise JsonableError(_("Topic can't be empty"))
zerver.lib.exceptions.JsonableError: Topic can't be empty

# Fixed code
def process_message(
    message: message.Message, rcpt_to: Optional[Text] = None, pre_checked: bool = False
) -> None:
    subject_header = str(message.get("Subject", "")).strip()
    if subject_header == "":
        subject_header = "(no topic)"
    encoded_subject, encoding = decode_header(subject_header)[0]
    if encoding is None:
        subject = force_text(
            encoded_subject
        )  # encoded_subject has type str when encoding is None
    else:
        try:
            subject = encoded_subject.decode(encoding)
        except (UnicodeDecodeError, LookupError):
            subject = "(unreadable subject)"

    debug_info = {}

    try:
        if rcpt_to is not None:
            to = rcpt_to
        else:
            to = find_emailgateway_recipient(message)
        debug_info["to"] = to

        if is_missed_message_address(to):
            process_missed_message(to, message, pre_checked)
        else:
            process_stream_message(to, subject, message, debug_info)
    except ZulipEmailForwardError as e:
        # TODO: notify sender of error, retry if appropriate.
        log_and_report(message, str(e), debug_info)