# Buggy code
def handle_response(self, response: "Optional[Dict]"):
    global resolvable_completion_items

    if self.state == CompletionState.REQUESTING:
        items = []  # type: List[Dict]
        if isinstance(response, dict):
            items = response["items"]
        elif isinstance(response, list):
            items = response
        items = sorted(items, key=lambda item: item.get("sortText") or item["label"])
        self.completions = list(self.format_completion(item) for item in items)

        if self.has_resolve_provider:
            resolvable_completion_items = items

        # if insert_best_completion was just ran, undo it before presenting new completions.
        prev_char = self.view.substr(self.view.sel()[0].begin() - 1)
        if prev_char.isspace():
            if last_text_command == "insert_best_completion":
                self.view.run_command("undo")

        self.state = CompletionState.APPLYING
        self.view.run_command("hide_auto_complete")
        self.run_auto_complete()
    elif self.state == CompletionState.CANCELLING:
        if self.next_request:
            prefix, locations = self.next_request
            self.do_request(prefix, locations)
            self.state = CompletionState.IDLE
    else:
        debug("Got unexpected response while in state {}".format(self.state))


# Traceback (TypeError)
LSP:  --> textDocument/completion
LSP:      {'isIncomplete': False, 'items': None}
Error handling server payload
Traceback (most recent call last):
File "/Users/margus/Library/Application Support/Sublime Text 3/Packages/LSP/plugin/core/rpc.py", line 141, in receive_payload
self.response_handler(payload)
File "/Users/margus/Library/Application Support/Sublime Text 3/Packages/LSP/plugin/core/rpc.py", line 160, in response_handler
handler(response["result"])
File "/Users/margus/Library/Application Support/Sublime Text 3/Packages/LSP/plugin/completion.py", line 310, in handle_response
items = sorted(items, key=lambda item: item.get("sortText") or item["label"])
TypeError: 'NoneType' object is not iterable

# Fixed code
def handle_response(self, response: "Optional[Dict]"):
    global resolvable_completion_items

    if self.state == CompletionState.REQUESTING:
        items = []  # type: List[Dict]
        if isinstance(response, dict):
            items = response["items"] or []
        elif isinstance(response, list):
            items = response
        items = sorted(items, key=lambda item: item.get("sortText") or item["label"])
        self.completions = list(self.format_completion(item) for item in items)

        if self.has_resolve_provider:
            resolvable_completion_items = items

        # if insert_best_completion was just ran, undo it before presenting new completions.
        prev_char = self.view.substr(self.view.sel()[0].begin() - 1)
        if prev_char.isspace():
            if last_text_command == "insert_best_completion":
                self.view.run_command("undo")

        self.state = CompletionState.APPLYING
        self.view.run_command("hide_auto_complete")
        self.run_auto_complete()
    elif self.state == CompletionState.CANCELLING:
        if self.next_request:
            prefix, locations = self.next_request
            self.do_request(prefix, locations)
            self.state = CompletionState.IDLE
    else:
        debug("Got unexpected response while in state {}".format(self.state))