A happy accident.
Wikipedia: Serendipity means a “fortunate happenstance” or “pleasant surprise”. It was coined by Horace Walpole in 1754. In a letter he wrote to a friend, Walpole explained an unexpected discovery he had made by reference to a Persian fairy tale,The Three Princes of Serendip. The princes, he told his correspondent, were “always making discoveries, by accidents and sagacity, of things which they were not in quest of”.