U.S. Army Sgt. Bowe Bergdahl. (Handout/Reuters)

IF THERE’S any lesson to be learned from the case of Sgt. Bowe Bergdahl, it is the age-old one about how appearances can be deceiving. When President Obama swapped five Taliban chiefs for the captive U.S. soldier, he clearly expected the homecoming to play as a good news story, about a person national security adviser Susan Rice said had served “with honor and distinction.” It quickly developed, however, that the Taliban captured Mr. Bergdahl after he had abandoned his post, and that many of his former comrades in arms deeply resented the dangers this reckless action imposed on those who risked their lives to search for him.

Soon, White House celebration of Mr. Bergdahl gave way to a campaign of denunciation in conservative circles, including Sen. John McCain’s (R-Ariz.) ill-advised threat to hold a Senate hearing if the returnee were not punished for desertion. The vilification reached its peak with Donald Trump’s labeling of Mr. Bergdahl as a “traitor” who should have been “executed.” Yet the demonization doesn’t square with the facts, as a newly disclosed report from the Army general assigned to investigate Mr. Bergdahl’s conduct shows. Mr. Bergdahl left his unit with the bizarre goal of sparking a high-level military investigation of his grunt-level grievances. That irrational decision, in turn, seemed reflective of long-standing mental-health issues that had caused Mr. Bergdahl to fail as a Coast Guard recruit and might well have caused the Army to turn him down, too, but for a manpower shortage at the time he volunteered.

Once in Taliban hands, Mr. Bergdahl was regularly and savagely beaten, starved and mistreated; far from collaborating, he repeatedly tried to escape, earning more torture. Meanwhile, the general’s report confirmed that no U.S. personnel lost their lives as a direct result of Mr. Bergdahl’s abandonment of his post, contrary to claims of many critics.

The general, Kenneth Dahl, recommended Mr. Bergdahl receive no prison time, as did a military hearing officer. But a higher-ranking officer referred the case to a general court-martial, where the erstwhile prisoner stands accused of not only desertion, which carries a maximum penalty of five years, but also the even more serious charge of misbehavior before the enemy — based on the dangers to which his actions exposed his fellow troops, for which a conviction could earn him life in prison.

What began as a test of Mr. Obama’s ability to deliver an American from captivity has thus mutated into a test of American military justice’s ability to remain impartial amid pressure from political overlords and a deeply polarized political debate. We agree with those who say that Mr. Bergdahl’s conduct in leaving his unit was wrong, that it put lives at risk and that, despite his psychological issues, he should be accountable. At the same time, the Army may have contributed to this debacle by enlisting a soldier it shouldn’t have. And even without formal accountability, he has already suffered horribly for his actions.

In our view, the military justice system will pass this test to the extent it tempers judgment with due consideration of everything the case reveals about human frailty — and with mercy.