Two months after the attempted coup in Turkey, the president, Recep Tayyip Erdogan, appears stronger than ever. He received warm words from Barack Obama at the G20 summit in China. He has reforged his alliance with Vladimir Putin, which had been significantly damaged by the Turkish downing of a Russian bomber last November. He is undeniably central to both the future of Syria and controlling the migration flow to Europe. And at home, his popularity appears to be higher than ever: a recent poll found that 68 per cent approved of his handling of the coup and its aftermath.

Mr Erdogan has a strong authoritarian streak. It is undeniable that the space for debate and dissent has steadily shrunk under his rule, leading many who once thought of him as a model of a Muslim democrat to warn that Turkey risks descending into one-party rule and worse.

But it is equally true, as a profile in The Guardian recently pointed out, that “after leading his party to victory in one presidential election and five general elections, he is arguably the world’s most successful democratic politician”.

So, did much of the world get it wrong when it reacted to the rebellion – one that cost 250 lives and could have plunged the country into civil war – less with congratulation and more with admonishment?

Turkey has had too many coups – roughly once a decade since around 1960 – and one of the significant achievements of the AKP (Mr Erdogan’s party) was facing down the army on their last attempt to strong-arm the government over the appointment of Abdullah Gul as president in 2007 (they objected to his wife wearing a hijab). The army has an honoured place in Turkey, but that does not mean it can regard itself as the self-appointed guardian of the state with the right to intervene in the political process whenever it deems necessary. Mr Erdogan appeared to have engineered a shift in the military mentality. The fact that the top generals didn’t support the coup is evidence to confirm that.

So on hearing that this latest attempted coup had failed, and the democratically elected government had prevailed, one would have thought that western observers and friends of Turkey would have overwhelmingly cheered the outcome.

Instead, there immediately arose a strident chorus warning Mr Erdogan not to go too far in going after the coup plotters and supporters. Yes, suspicions that he would use the putsch to remove innocent critics were not entirely without foundation.

But frankly, it was as though he was being publicly lectured and told off – when he had just survived an insurrection in which soldiers just missed seizing him with the intention, many believe, of shooting him.

The reaction struck nearly all Turks as astonishingly inappropriate, as were suggestions that Turkey would be made to suffer if it revived the death penalty to deal with the ringleaders.

In some cases this was also wildly hypocritical. Many countries, including the United States, have capital punishment on their statute books, and whatever one thinks of it, it is the right of countries and legislatures to decide on the punishments for the most serious crimes, right up to the taking of criminals’ lives for the most heinous. Why on Earth should that have had any bearing on Turkey’s eventual membership (or not) of the EU, which looks increasingly unlikely in any case?

At a moment when Mr Erdogan, his supporters, and even the opposition parties who have no love for the AKP, all came together to save Turkish democracy, much of the rest of the world decided not to applaud the president but to wag their fingers at him.

How can anyone be surprised if this leads Mr Erdogan to believe that the West is not to be trusted, and that working to elevate to even greater levels his friendship with Vladimir Putin – who won’t criticise him for increasing his powers by democratic means or otherwise – is likely to bear far greater fruit?

Western commentators and leaders may also be making a mistake by implying that Mr Erdogan’s calls for Fethullah Gulen’s extradition from the US are the result of an unbelievable conspiracy theory. Even critics of the president, such as the writer Mustafa Akyol, author of Islam Without Extremes: A Muslim Case for Liberty, believe that Mr Gulen, an elderly, reclusive cleric, is the “prime suspect” behind the coup. Akyol quotes a disillusioned Gulenist saying that “there is a darker side of the movement” beyond its involvement in schools, charities and religious teaching. He thinks the Gulenists really are out “to capture state power”, and that there are few lengths to which they will not go in order to do so.

There is unanimity, too, that the reach and infiltration of the Gulenists goes very deep indeed. Comparisons have been made with the need to de-communise East German institutions on reunification with the West. Similarly in Turkey, clearing out a network of supporters whose allegiance to a cult trumps their loyalty to the democratic state may well involve the removal of thousands.

The former Swedish prime minister, Carl Bildt, agrees both about the threat posed by the Gulenists and the need to remove them. And while he sees a “silver lining” to the putsch in that “after years of division, it has united Turkey’s democratic political parties around the shared goal of defending democracy against future internal threats”, he also concludes: “The West’s lack of empathy for Turkey during this traumatic period has been astonishing.”

I agree. Personal distaste for the prickly Mr Erdogan, and disagreement with his hoovering of power – no matter that the Turkish electorate continue to vote him in – clouded the reality. And that was that Turkey’s democracy was saved; and vengeance will be visited on those who tried to overthrow it.

It would have been more appropriate for the outside world to recognise that – and also to recognise that faced with such an entrenched internal threat, they would probably react in exactly the same way.

Sholto Byrnes is a senior fellow at the Institute of Strategic and International Studies, Malaysia