State machine returns error instead of panicking

Return a boolean success status and error object with detailed message
in case the state machine encounters an unknown state while running.