A seven-year court battle in the District over an HIV mis­diagnosis ended this week when the Whitman-Walker Clinic quietly settled a $20 million lawsuit by a former patient who was mistakenly told he had the virus.

In 2005, Terry Hedgepeth, 52, sued Whitman-Walker because it had mistakenly told him five years earlier that he was HIV-positive. Since then, Hedgepeth, his attorney, Whitman-Walker, and judges in D.C. Superior Court and the D.C. Court of Appeals had been embroiled in legal wrangling.

On Tuesday, just a week before the case was to go to trial in D.C. Superior Court, an agreement was reached. Details of the settlement weren’t disclosed.

“We are happy to settle the case amicably,” said Don Blanchon, Whitman-Walker’s chief executive. He would not comment further on the case.

Hedgepeth’s attorney, Jonathan C. Dailey, said the “case was resolved amicably” and also declined to comment further on the agreement. But Dailey added that the agreement came a year after the D.C. Court of Appeals unanimously ruled in the case that medical patients who are given incorrect information from their doctors about a life-threatening illness can seek recourse through the courts for emotional distress.

That decision, Dailey said, paved the way for the agreement and could open the door for other cases in which doctors mistakenly misdiagnose a patient with a life-threatening illness.

“We changed 25 years of law. Now if a doctor misreads information, a patient can sue for negligent emotional distress,” Dailey said.

Dailey said his client, who is married and living in the Maryland suburbs, suffers from post-traumatic stress disorder because of the misdiagnosis.

“The effects of those five years have not worn off completely,” Dailey said. Repeated phone calls to Hedgepeth were not returned.

According to court records and interviews with Dailey, Hedgepeth went to Whitman-Walker after his then-girlfriend, with whom he had been sexually active, told him that she had AIDS and feared that she had infected him.

The test at the clinic, he would later discover, was negative. But a clinic employee mistakenly wrote in Hedgepeth’s files that he had taken two tests at the clinic and that one of them was positive. Then, a doctor at the clinic failed to carefully review Hedgepeth’s chart and instead began counseling him about the virus.

During the next four years, no further blood tests were done, and Hedgepeth continued to believe that he was HIV-positive. He became depressed, according to the court records, quit his job as a caterer, began using drugs and alcohol, and twice was committed to psychiatric wards because of suicidal thoughts.

Hedgepeth continued to be monitored at Whitman-Walker but was never medically treated for the virus. The clinic also arranged for Hedgepeth to live in a facility with HIV-positive people.

In June 2005, Hedgepeth decided to seek alternative treatment from the Abundant Life Clinic in Southeast Washington. The clinic conducted a routine blood test and discovered that he was not HIV-positive. A month later, Hedgepeth was referred to Johns Hopkins Bayview Medical Center to take a follow-up test, which confirmed that he had not contracted the virus.

Two months later, Hedgepeth sued Whitman-Walker for medical negligence. But in 2006, D.C. Superior Court Judge Robert E. Morin dismissed the case.

Three years later, three judges on the D.C. Court of Appeals agreed with Morin’s decision, saying that Hedgepeth was not physically harmed by the misdiagnosis and noting that he had not been prescribed HIV medication that caused any side effects.

In 2009, Hedgepeth and his attorney petitioned for all 10 of the D.C. appellate judges to review the case. Last year, the judges reversed the lower court’s decision, finding that the case should move forward because serious emotional distress could result from a doctor’s negligence.

The ruling finally gave Hedgepeth a chance to be heard by a jury. On Tuesday, as both sides were preparing for trial, they resolved the case.

And although Dailey believes that the appellate judges’ decision gives people misdiagnosed with deadly diseases grounds for such lawsuits, some experts disagree.

Catherine Hanssens, executive director of the Center for HIV Law and Policy in New York, said that courts and juries realize doctors make mistakes.

“Most people who find out they are not HIV-positive view it as good news — they don’t run out and get a lawyer,” Hanssens said.

“Doctors are not infallible, and patients have to realize [the doctors] don’t, and should not, have the last say in their health.”