More than 760 people were injured in confrontations between police and voters during the Catalan independence referendum vote on Oct. 1, officials said. (Anonymous via Storyful)

Frida Ghitis is a columnist for World Politics Review and a regular CNN.com opinion contributor.

The images from Spain this weekend have shocked the world — and many Spaniards, too. Yes, Prime Minister Mariano Rajoy had warned unequivocally that he would not allow Catalonia’s independence referendum to go ahead. Yet despite the crescendo of tensions ahead of Sunday’s vote, few expected to see bloodshed, much less on such a large scale.

By the time the chaotic day was done, at least 840 people had been injured in dramatic clashes with riot-gear-clad national security forces. The National Police and Civil Guard, armed with truncheons and shields, managed to confiscate millions of ballots and forcibly drag people from the voting stations. The vote went on nonetheless. Bloodied Catalonians joined forces against the strong-arm tactics of the central government, demanding the right to vote and to live free of state brutality.

If Rajoy wanted to prevent Catalonia from leaving Spain, he could hardly have used tactics that were more counterproductive. It’s a safe bet that after the mayhem, more than a few Catalans who were ambivalent about remaining in Spain now believe Catalonia should go its separate way from a country that won’t allow it to take its own pulse about independence.

The rest of Europe, the rest of the world, should thank Spain for offering this startling lesson on how not to deal with separatist movements. At a moment when nationalism is on the upsurge, when identity politics and a slew of social, economic and political developments — not least the rise of demagogues — are creating centrifugal forces, the self-defeating actions of the Spanish government offer a vivid cautionary tale.

In the past few years, a perfect storm of forces has converged to give new vitality to nationalist movements, some of them seeking to break regional bonds with national governments. The global financial crisis, the arrival of large refugee populations, the threat of terrorism, and the rise of ethnic tensions spur the impulse to circle the family wagons. In some cases, the arguments of populist rabble-rousers have become more resonant. Legitimate grievances have become inflamed. As Ximo Puig, the president of Spain’s Valencia region, noted, a certain amount of “low-intensity xenophobia” and “abundant excesses of demagoguery” have been spreading.

Regions like Catalonia, which is a major engine of the Spanish economy, feel shortchanged by the national government. Catalans have their own language, their own history. They also have a shared history with Spain, and a great deal of autonomy.

True, the referendum had been declared illegal, and the Spanish constitution of 1978 says the unity of Spain is “indissoluble.” Spain is right to worry about what would happen if Catalonia were to secede. It is vital to the national economy and, yes, to Spanish identity. And a departure by Catalonia could embolden other separatist movements and risk unraveling the nation.

Rajoy didn’t have great choices, but he clearly picked the worst. Spain’s neighbors are watching closely — as they should.

In Western Europe alone there are independence movements eager to bring about their own break with central governments. They, too, are watching closely.

In Spain’s Basque Country, the fate of Catalonia is of great interest. But there are countless others keeping tabs. From Denmark’s Faroe Islands to Belgium’s independence-minded Flanders, from France’s Corsica to Italy’s wealthy Padania, groups and regions across Europe are weighing what joins them and what separates them from the countries to which they belong. After all, the nation-state is a relatively recent invention. Every place has its own history.

And that’s just Western Europe. The centrifugal forces threatening to atomize, to Balkanize, existing countries are even more pronounced in other parts of the world. Iraq’s Kurds just held their own referendum. (Incredibly, it went more peacefully than Catalonia’s. It, too, had been declared illegal.) Baluchistan yearns for freedom from Pakistan, and throughout the former Soviet Union countless regions dream of statehood.

(Low-intensity nationhood movements also exist in the United States. Spain’s lessons should not be lost on America, either.)

The ultimate test of political maturity is resolving conflicts without violence. Spain failed that test.

Ironically, polls ahead of the referendum and previous votes showed most Catalans did not favor independence. A July poll showed 41 percent in favor, 49 percent opposed.  Most did support their right to vote on the issue.

Spain should have highlighted the ways Catalonia benefits from being part of Spain — and it still should. It should also discuss ways to address the region’s grievances.

The referendum, incidentally, produced the expected landslide “Yes” for independence. But the turnout was well below 50 percent, so most Catalans have not given their support.

For his part, Catalan regional president Carles Puigdemont says he’s not looking for a traumatic exit and is now calling for international mediation.

He seems to be offering Madrid a chance to correct its mistake. The world should keep watching and learning from Spain’s grievous error.