Lockhart, who had so often assured them that all danger had passed, only to be proved wrong straight away, was now wholeheartedly convinced that it was hardly worth the trouble to see them safely down the corridors. His hair wasn't as sleek as usual; it seemed he had been up most of the night, patrolling the fourth floor.
I'm not sure what role "only to be proved wrong straight away" is playing in the sentence. How should we understand it in this context?
-- From Harry Potter.