@Alva I'm sorry but I don't know how I can help you with your problem. If it helps you, I am working to create a macro that allows me to save the current line that is being executed from the gcode in a global variable and allows me to restart the layer if something goes wrong.
When I have it if you think it can be interesting I can share it with you.
Best regards!