The criminal offense happened as Jamie Harron was weaving through a Dubai bar in July, balancing a drink — his transgression so small that the British national didn't know he'd committed a crime until he was almost in handcuffs.

Harron had lost his balance and grabbed a man's shoulder to keep himself and his drink upright, according to the Associated Press. Apologetically, he claims, he patted the man on his behind.

But it was not over. Police were called. Harron was arrested and was well on his way to being featured in the latest anecdote about just how different things are in Dubai.

Harron, an electrician from Stirling, Scotland, was traveling to Afghanistan for a new job. But what was supposed to be a relaxing two-day layover in Dubai, on the western edge of the Persian Gulf — he'd snapped photos of himself swimming in the Gulf and standing in front of a skyscraper — morphed into a months-long legal battle as Harron was tried, then convicted of sexual assault. He was also accused of raising his middle finger to his accuser as police were en route to the bar.

Harron's ordeal ended Monday, when the ruler of Dubai, Sheikh Mohammed bin Rashid Maktum, nullified Harron's conviction and the three-month jail sentence, the Associated Press reported. Harron was called to the police station (a summons he briefly feared was a trap) and handed his passport, free to leave the emirate whenever he wanted.

While navigating the legal maze, he'd lost his job and accumulated more than $40,000 in legal and other expenses.

“I've lost my job, I'm in debt now, I may be going to prison and all this for a two-day stopover,” he told the Independent newspaper. “The whole thing is like a horrible dream and I just don’t know when it is going to end. I thought it would be over by now but it feels like it will never be.

Harron is detained in Dubai, and an aid group that has been working with him applauded the ruler's move but said the incident illustrates systemic problems in the United Arab Emirates.

“Of course, a fully functional legal system would not require outside intervention, and a case like Jamie’s would never proceed in the first place,” the group said in a statement. “But we are enormously grateful to Sheikh Mohammed for stepping in and vindicating Jamie after months of hardship.”

Legal codes in the United Arab Emirates often ensnare expatriate residents and visitors lulled into assuming that the Western-style amenities — sprawling malls, five-star hotel bars and beach resorts — also mean relaxed rules on public behavior.

Dubai, one of seven emirates that comprise the United Arab Emirates and the hub of the UAE tourist industry, does allow far more latitude on dress codes and other aspects compared with neighboring Persian Gulf nations. But UAE laws are still widely influenced by tribal codes and traditions that leave little flexibility.

Bouncing checks can be punishable by jail time, and women who report sexual assault can end up in legal trouble themselves on charges of having sex outside marriage.

Travel groups and websites have made a cottage industry of listing how to avoid potential trouble in the UAE.

Among the cautionary tales is that of an unmarried British couple found guilty in 2008 of having sex on a beach in Dubai after leaving an all-you-can-drink champagne brunch at a luxury hotel. In another case, a British couple was ordered jailed for a month in 2010 after an Emirati woman complained that they had kissed each other on the mouth in a Dubai restaurant. A couple was once arrested for sending each other “sexy texts.

Britain’s Foreign Office has put out notices warning travelers to be mindful of local rules while in Dubai.

Last week, Philip Parham, the British ambassador to the United Arab Emirates, issued advice to British travelers, politely and diplomatically reminding them that Dubai's legal system can be a field of land mines.

“Our travel advice for the UAE explains that local laws and customs are very different to those in the UK and that there may be serious penalties for doing something which may not be illegal in the UK,” he said. “Both our travel advice and our Embassy’s many public awareness-raising campaigns cover some of the specific issues which may arise from differences in laws and customs.”

That travel advice includes such statements as “terrorists are likely to try to carry out attacks in the UAE” and “showing sympathy for Qatar on social media  . . . is an offense. Offenders could be imprisoned.”

It is a tricky balancing act for Dubai, the affluent municipality that seeks to boost its economy with tourism dollars from people like Harron. But as Detained in Dubai wrote on its blog, Dubai is also an extremely religiously conservative emirate “with an archaic corrupt legal system used as a corporate jail by the unscrupulous and vindictive.”

For many, Harron's arrest has served as an unofficial travel advisory.

Harron told the Sun that the justice system in Dubai was in an “absolute shambles.”

But right now, he said, he has more pressing matters than pursuing legal reform in the Middle Eastern country. His flight back to Scotland is on Tuesday. He wants to see his mother.

According to the Sun, he told her: “Just stop crying, Mum, don't be upset anymore. Don't cry anymore.”

Read more:

Authorities: Gold miners at a bar bragged about slaughtering members of a reclusive Brazilian tribe

Russian ‘cannibal couple’ may have drugged, killed and eaten as many as 30 people, police say

This video showed a Nazi-saluting dog. Was posting it on YouTube a hate crime?