Do we have to obey the magisterium if it is teaching error?
Without a particular example to examine, I cannot determine for you whether or not you are correct that the magisterium is teaching error in a particular case. I can only assure you that the Holy Spirit protects the Church from teaching error in matters of doctrine and morals.
As a rule of thumb, unless a person is exceptionally well-formed in his faith (which is, unfortunately, an anomaly in modern Catholic circles), he should refrain from assuming that the Church is teaching error. He should, instead, presume that he himself is in error and seek to understand why the Church teaches as it does. Failing that, he should seek to understand how authoritatively the Church is teaching on a particular issue. If a Church official is giving a private opinion on a non-religious matter, a Catholic is free to disagree. If the Church’s teaching authority is actually teaching the faithful, there is a spectrum of authority that ranges from theological opinion to de fide dogma. The more authoritatively the Church teaches on a particular issue, the graver the obligation of the Catholic to accept it.