# Buggy code
def _onGCodePrefixMessage(self, message: Arcus.PythonMessage) -> None:
    self._scene.gcode_dict[self._start_slice_job_build_plate].insert(
        0, message.data.decode("utf-8", "replace")
    )  # type: ignore #Because we generate this attribute dynamically.


# Traceback (KeyError)
2019-08-22 21:56:47,130 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [61]: An uncaught error has occurred!
2019-08-22 21:56:47,133 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]: Traceback (most recent call last):
2019-08-22 21:56:47,134 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/cura/CuraApplication.py", line 990, in event
2019-08-22 21:56:47,134 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     return super().event(event)
2019-08-22 21:56:47,134 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Qt/QtApplication.py", line 428, in event
2019-08-22 21:56:47,135 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     event._function_event.call()
2019-08-22 21:56:47,135 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Event.py", line 189, in call
2019-08-22 21:56:47,135 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     self._function(*self._args, **self._kwargs)
2019-08-22 21:56:47,136 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Signal.py", line 315, in __performEmit
2019-08-22 21:56:47,136 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     func(dest, *args, **kwargs)
2019-08-22 21:56:47,136 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Backend/Backend.py", line 191, in _onMessageReceived
2019-08-22 21:56:47,136 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     self._message_handlers[message.getTypeName()](message)
2019-08-22 21:56:47,137 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/cura/plugins/CuraEngineBackend/CuraEngineBackend.py", line 674, in _onGCodeLayerMessage
2019-08-22 21:56:47,137 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     self._scene.gcode_dict[self._start_slice_job_build_plate].append(message.data.decode("utf-8", "replace")) #type: ignore #Because we generate this attribute dynamically.
2019-08-22 21:56:47,137 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]: KeyError: 0
2019-08-22 21:56:47,157 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [61]: An uncaught error has occurred!
2019-08-22 21:56:47,157 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]: Traceback (most recent call last):
2019-08-22 21:56:47,158 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/cura/CuraApplication.py", line 990, in event
2019-08-22 21:56:47,158 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     return super().event(event)
2019-08-22 21:56:47,158 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Qt/QtApplication.py", line 428, in event
2019-08-22 21:56:47,159 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     event._function_event.call()
2019-08-22 21:56:47,159 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Event.py", line 189, in call
2019-08-22 21:56:47,159 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     self._function(*self._args, **self._kwargs)
2019-08-22 21:56:47,160 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Signal.py", line 315, in __performEmit
2019-08-22 21:56:47,160 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     func(dest, *args, **kwargs)
2019-08-22 21:56:47,161 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/python3/dist-packages/UM/Backend/Backend.py", line 191, in _onMessageReceived
2019-08-22 21:56:47,161 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     self._message_handlers[message.getTypeName()](message)
2019-08-22 21:56:47,161 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:   File "/opt/cura/lib/cura/plugins/CuraEngineBackend/CuraEngineBackend.py", line 674, in _onGCodeLayerMessage
2019-08-22 21:56:47,162 - CRITICAL - [MainThread] cura.CrashHandler.__init__ [64]:     self._scene.gcode_dict[self._start_slice_job_build_plate].append(message.data.decode("utf-8", "replace")) #type: ignore #Because we generate this attribute dynamically.

# Fixed code
def _onGCodePrefixMessage(self, message: Arcus.PythonMessage) -> None:
    try:
        self._scene.gcode_dict[self._start_slice_job_build_plate].insert(
            0, message.data.decode("utf-8", "replace")
        )  # type: ignore #Because we generate this attribute dynamically.
    except KeyError:  # Can occur if the g-code has been cleared while a slice message is still arriving from the other end.
        pass  # Throw the message away.