Our error reporting is historically grown into some kind of mess.
A while ago I implemented stacking for the global error which is used in
this commit now to wrap calls to functions which do not report (all)
errors via return, so that only failures in those calls cause a failure
to propergate down the chain rather than failing if anything
(potentially totally unrelated) has failed at some point in the past.
This way we can avoid stopping the entire acquire process just because a
single source produced an error for example. It also means that after
the acquire process the cache is generated – even if the acquire
process had failures – as we still have the old good data around we can and
should generate a cache for (again).
There are probably more instances of this hiding, but all these looked
like the easiest to work with and fix with reasonable (aka net-positive)
effects.
 
    FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
    pkgTagFile TF(&Fd);
-   if (_error->PendingError() == true)
+   if (Fd.IsOpen() == false || Fd.Failed())
       return false;
 
    pkgTagSection Tags;
         _error->PushToStack();
         _error->Errno("pkgAcqFile::Done", "Symlinking file %s failed", DestFile.c_str());
         std::stringstream msg;
-        _error->DumpErrors(msg);
+        _error->DumpErrors(msg, GlobalError::DEBUG, false);
         _error->RevertToStack();
         ErrorText = msg.str();
         Status = StatError;
 
 }
 pkgAcquire::RunResult pkgAcquire::Run(int PulseIntervall)
 {
+   _error->PushToStack();
    CheckDropPrivsMustBeDisabled(*this);
 
    Running = true;
         _error->Errno("select","Select has failed");
         break;
       }
-            
+
       RunFds(&RFds,&WFds);
-      if (_error->PendingError() == true)
-        break;
-      
+
       // Timeout, notify the log class
       if (Res == 0 || (Log != 0 && Log->Update == true))
       {
 
    // Shut down the items
    for (ItemIterator I = Items.begin(); I != Items.end(); ++I)
-      (*I)->Finished(); 
-   
-   if (_error->PendingError())
+      (*I)->Finished();
+
+   bool const newError = _error->PendingError();
+   _error->MergeWithStack();
+   if (newError)
       return Failed;
    if (WasCancelled)
       return Cancelled;
 
 }
                                                                        /*}}}*/
 // CacheFile::BuildCaches - Open and build the cache files             /*{{{*/
-// ---------------------------------------------------------------------
-/* */
+class APT_HIDDEN ScopedErrorMerge {
+public:
+   ScopedErrorMerge() { _error->PushToStack(); }
+   ~ScopedErrorMerge() { _error->MergeWithStack(); }
+};
 bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
 {
    if (Cache != NULL)
       return true;
 
+   ScopedErrorMerge sem;
    if (_config->FindB("pkgCacheFile::Generate", true) == false)
    {
       FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
+      if (file.IsOpen() == false || file.Failed())
+        return false;
       Map = new MMap(file, MMap::Public|MMap::ReadOnly);
       Cache = new pkgCache(Map);
-      if (_error->PendingError() == true)
-         return false;
-      return true;
+      return _error->PendingError() == false;
    }
 
-   const bool ErrorWasEmpty = _error->empty();
    if (WithLock == true)
       if (_system->Lock() == false)
         return false;
       return _error->Error(_("The package lists or status file could not be parsed or opened."));
 
    /* This sux, remove it someday */
-   if (ErrorWasEmpty == true && _error->empty() == false)
+   if (_error->PendingError() == true)
       _error->Warning(_("You may want to run apt-get update to correct these problems"));
 
    Cache = new pkgCache(Map);
 
 #include <unistd.h>
 #include <string>
 #include <cstring>
+#include <algorithm>
 
                                                                        /*}}}*/
 
        if (mergeStack == true)
                for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
                     s != Stacks.rend(); ++s)
-                       Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
+                       std::copy(s->Messages.begin(), s->Messages.end(), std::front_inserter(Messages));
+
+       std::for_each(Messages.begin(), Messages.end(), [&threshold, &out](Item const &m) {
+               if (m.Type >= threshold)
+                       out << m << std::endl;
+       });
 
-       for (std::list<Item>::const_iterator m = Messages.begin();
-            m != Messages.end(); ++m)
-               if (m->Type >= threshold)
-                       out << (*m) << std::endl;
        Discard();
 }
                                                                        /*}}}*/
 }
                                                                        /*}}}*/
 // GlobalError::empty - does our error list include anything?          /*{{{*/
-bool GlobalError::empty(MsgType const &trashhold) const {
+bool GlobalError::empty(MsgType const &threshold) const {
        if (PendingFlag == true)
                return false;
 
        if (Messages.empty() == true)
                return true;
 
-       for (std::list<Item>::const_iterator m = Messages.begin();
-            m != Messages.end(); ++m)
-               if (m->Type >= trashhold)
-                       return false;
-
-       return true;
+       return std::find_if(Messages.begin(), Messages.end(), [&threshold](Item const &m) {
+               return m.Type >= threshold;
+       }) == Messages.end();
 }
                                                                        /*}}}*/
 // GlobalError::PushToStack                                            /*{{{*/
 void GlobalError::PushToStack() {
-       MsgStack pack(Messages, PendingFlag);
-       Stacks.push_back(pack);
+       Stacks.emplace_back(Messages, PendingFlag);
        Discard();
 }
                                                                        /*}}}*/
 // GlobalError::MergeWithStack                                         /*{{{*/
 void GlobalError::MergeWithStack() {
        MsgStack pack = Stacks.back();
-       Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
+       Messages.splice(Messages.begin(), pack.Messages);
        PendingFlag = PendingFlag || pack.PendingFlag;
        Stacks.pop_back();
 }
 
        bool PendingFlag;
 
        struct MsgStack {
-               std::list<Item> const Messages;
+               std::list<Item> Messages;
                bool const PendingFlag;
 
                MsgStack(std::list<Item> const &Messages, bool const &Pending) :
 
    char *p = realpath(File.c_str(), NULL);
    if (p == NULL)
    {
-      _error->Errno("realpath", "flAbsPath failed");
+      _error->Errno("realpath", "flAbsPath on %s failed", File.c_str());
       return "";
    }
    std::string AbsPath(p);
 
                MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(Workspace),
                GrowFactor(Grow), Limit(Limit)
 {
-   if (_error->PendingError() == true)
-      return;
-
    // disable Moveable if we don't grow
    if (Grow == 0)
       this->Flags &= ~Moveable;
                MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace),
                GrowFactor(Grow), Limit(Limit)
 {
-       if (_error->PendingError() == true)
-               return;
-
        // disable Moveable if we don't grow
        if (Grow == 0)
                this->Flags &= ~Moveable;
       const unsigned long size = 20*1024;
       I->Count = size/ItemSize;
       Pool* oldPools = Pools;
+      _error->PushToStack();
       Result = RawAllocate(size,ItemSize);
+      bool const newError = _error->PendingError();
+      _error->MergeWithStack();
       if (Pools != oldPools)
         I += Pools - oldPools;
 
       // Does the allocation failed ?
-      if (Result == 0 && _error->PendingError())
+      if (Result == 0 && newError)
         return 0;
       I->Start = Result;
    }
    if (Len == (unsigned long)-1)
       Len = strlen(String);
 
+   _error->PushToStack();
    unsigned long const Result = RawAllocate(Len+1,0);
+   bool const newError = _error->PendingError();
+   _error->MergeWithStack();
 
-   if (Base == NULL || (Result == 0 && _error->PendingError()))
+   if (Base == NULL || (Result == 0 && newError))
       return 0;
 
    memcpy((char *)Base + Result,String,Len);
 
       return false;
 
    pkgTagFile TagFile(&Fd, Fd.Size());
-   if (_error->PendingError() == true)
+   if (Fd.IsOpen() == false || Fd.Failed())
    {
       if (ErrorText != NULL)
         strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
    // signature for an 'InRelease' file couldn't be checked
    if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
       return false;
-   if (_error->PendingError() == true)
-      return false;
 
    // Store the IMS information
    pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
 
    pkgTagFile TagFile(&Rel, Rel.Size());
    pkgTagSection Section;
-   if (_error->PendingError() == true || TagFile.Step(Section) == false)
+   if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false)
       return false;
 
    std::string data;
    if (data.empty() == false) \
    { \
       map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
+      if (storage == 0) return false; \
       STORE = storage; \
    }
    APT_INRELEASE(MIXED, "Suite", File->Archive)
    Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
    Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
 
-   return !_error->PendingError();
+   return true;
 }
                                                                        /*}}}*/
 // ReleaseIndex::FindInCache - Find this index                         /*{{{*/
 
         free(d->slave);
         d->slave = NULL;
       }
-      _error->DumpErrors(std::cerr);
+      _error->DumpErrors(std::cerr, GlobalError::DEBUG, false);
    }
    _error->RevertToStack();
 }
 
       off_t const FileSize = Pkg.Size();
 
       pkgTagFile Parser(&Pkg);
-      if (_error->PendingError() == true)
+      if (Pkg.IsOpen() == false || Pkg.Failed())
         return false;
       
       // Open the output file
       } else {
          Target.Open(TargetF,FileFd::WriteAtomic);
       }
-      if (_error->PendingError() == true)
+      if (Target.IsOpen() == false || Target.Failed())
         return false;
 
       // Setup the progress meter
       off_t const FileSize = Pkg.Size();
 
       pkgTagFile Parser(&Pkg);
-      if (_error->PendingError() == true)
+      if (Pkg.IsOpen() == false || Pkg.Failed())
         return false;
 
       // Open the output file
       } else {
         Target.Open(TargetF,FileFd::WriteAtomic);
       }
-      if (_error->PendingError() == true)
+      if (Pkg.IsOpen() == false || Pkg.Failed())
         return false;
 
       // Setup the progress meter
 
    _error->PushToStack();
    std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
    bool const newError = _error->PendingError();
+   _error->MergeWithStack();
    if (newError == false && Parser == nullptr)
       return true;
    if (Parser == NULL)
 
                     if (Debug)
                     {
                        clog << OutputInDepth(Depth) << "Avoidance unpack of " << ConflictPkg.FullName() << " failed for " << End << " ignoring:" << std::endl;
-                       _error->DumpErrors(std::clog);
+                       _error->DumpErrors(std::clog, GlobalError::DEBUG, false);
                     }
                     _error->RevertToStack();
                     // ignorance can only happen if a) one of the offenders is already gone
 
                    Map(*pMap), Cache(pMap,false), Progress(Prog),
                     CurrentRlsFile(NULL), CurrentFile(NULL), d(NULL)
 {
-   if (_error->PendingError() == true)
-      return;
-
    if (Map.Size() == 0)
    {
       // Setup the map interface..
       Cache.HeaderP = (pkgCache::Header *)Map.Data();
-      if (Map.RawAllocate(sizeof(pkgCache::Header)) == 0 && _error->PendingError() == true)
+      _error->PushToStack();
+      Map.RawAllocate(sizeof(pkgCache::Header));
+      bool const newError = _error->PendingError();
+      _error->MergeWithStack();
+      if (newError)
         return;
 
       Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
 
    // Add a new version
    map_pointer_t const verindex = NewVersion(Ver, Version, Pkg.Index(), Hash, *LastVer);
-   if (verindex == 0 && _error->PendingError())
+   if (unlikely(verindex == 0))
       return _error->Error(_("Error occurred while processing %s (%s%d)"),
                           Pkg.Name(), "NewVersion", 1);
 
    Dynamic<pkgCache::DescIterator> DynDesc(Desc);
 
    map_pointer_t const descindex = NewDescription(Desc, lang, CurMd5, md5idx);
-   if (unlikely(descindex == 0 && _error->PendingError()))
+   if (unlikely(descindex == 0))
       return _error->Error(_("Error occurred while processing %s (%s%d)"),
            Ver.ParentPkg().Name(), "NewDescription", 1);
 
 /* This just verifies that each file in the list of index files exists,
    has matching attributes with the cache and the cache does not have
    any extra files. */
+class APT_HIDDEN ScopedErrorRevert {
+public:
+   ScopedErrorRevert() { _error->PushToStack(); }
+   ~ScopedErrorRevert() { _error->RevertToStack(); }
+};
 static bool CheckValidity(const string &CacheFile,
                           pkgSourceList &List,
                           FileIterator const Start,
                           FileIterator const End,
                           MMap **OutMap = 0)
 {
+   ScopedErrorRevert ser;
    bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
    // No file, certainly invalid
    if (CacheFile.empty() == true || FileExists(CacheFile) == false)
    FileFd CacheF(CacheFile,FileFd::ReadOnly);
    std::unique_ptr<MMap> Map(new MMap(CacheF,0));
    pkgCache Cache(Map.get());
-   if (_error->PendingError() == true || Map->Size() == 0)
+   if (_error->PendingError() || Map->Size() == 0)
    {
       if (Debug == true)
         std::clog << "Errors are pending or Map is empty() for " << CacheFile << std::endl;
-      _error->Discard();
       return false;
    }
 
       if (Debug == true)
       {
         std::clog << "Validity failed because of pending errors:" << std::endl;
-        _error->DumpErrors();
+        _error->DumpErrors(std::clog, GlobalError::DEBUG, false);
       }
-      _error->Discard();
       return false;
    }
-   
+
    if (OutMap != 0)
       *OutMap = Map.release();
    return true;
       std::string const &FileName)
 {
    FileFd SCacheF(FileName, FileFd::WriteAtomic);
-   if (_error->PendingError() == true)
+   if (SCacheF.IsOpen() == false || SCacheF.Failed())
       return false;
 
    fchmod(SCacheF.Fd(),0644);
 {
    Map.reset(CreateDynamicMMap(NULL, 0));
    FileFd CacheF(FileName, FileFd::ReadOnly);
+   if (CacheF.IsOpen() == false || CacheF.Failed())
+      return false;
+   _error->PushToStack();
    map_pointer_t const alloc = Map->RawAllocate(CacheF.Size());
-   if ((alloc == 0 && _error->PendingError())
-        || CacheF.Read((unsigned char *)Map->Data() + alloc,
-           CacheF.Size()) == false)
+   bool const newError = _error->PendingError();
+   _error->MergeWithStack();
+   if (alloc == 0 && newError)
+      return false;
+   if (CacheF.Read((unsigned char *)Map->Data() + alloc, CacheF.Size()) == false)
       return false;
    Gen.reset(new pkgCacheGenerator(Map.get(),Progress));
    return true;
 }
                                                                        /*}}}*/
 // CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
-// ---------------------------------------------------------------------
-/* */
+class APT_HIDDEN ScopedErrorMerge {
+public:
+   ScopedErrorMerge() { _error->PushToStack(); }
+   ~ScopedErrorMerge() { _error->MergeWithStack(); }
+};
 APT_DEPRECATED bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
    { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
 bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
    if (_system->AddStatusFiles(Files) == false)
       return false;
 
+   ScopedErrorMerge sem;
    std::unique_ptr<DynamicMMap> Map(CreateDynamicMMap(NULL, 0));
    map_filesize_t CurrentSize = 0;
    map_filesize_t TotalSize = 0;
-   
    TotalSize = ComputeSize(NULL, Files.begin(), Files.end());
-   
+
    // Build the status cache
    if (Progress != NULL)
       Progress->OverallProgress(0,1,1,_("Reading package lists"));
 
    
    FileFd Fd(File,FileFd::ReadOnly);
    pkgTagFile TF(&Fd);
-   if (_error->PendingError() == true)
+   if (Fd.IsOpen() == false || Fd.Failed())
       return false;
 
    pkgUserTagSection Tags;
       }
       for (; Word != End && isspace(*Word) != 0; Word++);
 
-      int priority = Tags.FindI("Pin-Priority", 0);
+      _error->PushToStack();
+      int const priority = Tags.FindI("Pin-Priority", 0);
+      bool const newError = _error->PendingError();
+      _error->MergeWithStack();
       if (priority < std::numeric_limits<short>::min() ||
           priority > std::numeric_limits<short>::max() ||
-         _error->PendingError()) {
+         newError) {
         return _error->Error(_("%s: Value %s is outside the range of valid pin priorities (%d to %d)"),
                              File.c_str(), Tags.FindS("Pin-Priority").c_str(),
                              std::numeric_limits<short>::min(),
 
    // see if we can read the file
    FileFd Fd(File, FileFd::ReadOnly);
    pkgTagFile Sources(&Fd);
-   if (_error->PendingError() == true)
+   if (Fd.IsOpen() == false || Fd.Failed())
       return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str());
 
    // read step by step
 
       for (std::vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
           J != Indexes->end(); ++J)
       {
-         Parser* P = (*J)->CreateSrcParser();
-        if (_error->PendingError() == true)
-            return;
-         if (P != 0)
-            Files.push_back(P);
+        _error->PushToStack();
+        Parser* P = (*J)->CreateSrcParser();
+        bool const newError = _error->PendingError();
+        _error->MergeWithStack();
+        if (newError)
+           return;
+        if (P != 0)
+           Files.push_back(P);
       }
    }
    
    // Step to the next record, possibly switching files
    while ((*Current)->Step() == false)
    {
-      if (_error->PendingError() == true)
-         return 0;
       ++Current;
       if (Current == Files.end())
          return 0;
       if(Step() == 0)
          return 0;
 
-      // IO error somehow
-      if (_error->PendingError() == true)
-        return 0;
-
       // Source name hit
       if ((*Current)->Package() == Package)
         return *Current;
 
       _error->PushToStack();
       res = sl.Read(sourceslist);
       if (!res) {
-         _error->DumpErrors();
+         _error->DumpErrors(std::cerr, GlobalError::DEBUG, false);
          strprintf(outs, _("Failed to parse %s. Edit again? "),
                    sourceslist.c_str());
          std::cout << outs;
 
            if (p.Open(patch_name, FileFd::ReadOnly, FileFd::Gzip) == false ||
                  patch.read_diff(p, &patch_hash) == false)
            {
-              _error->DumpErrors(std::cerr);
+              _error->DumpErrors(std::cerr, GlobalError::DEBUG, false);
               return false;
            }
            p.Close();
 
 Get:2 file:$APTARCHIVE unstable Release
 Err:2 file:$APTARCHIVE unstable Release
   File not found
+Reading package lists...
 W: The repository 'file:$APTARCHIVE unstable Release' does not have a Release file. This is deprecated, please contact the owner of the repository.
 E: Use --allow-insecure-repositories to force the update" aptget update --no-allow-insecure-repositories
 
 
 
 msgmsg 'expired InRelease'
 EXPECT='Hit:1 http://localhost:8080 unstable InRelease
+Reading package lists...
 E: Release file for http://localhost:8080/dists/unstable/InRelease is expired (invalid since). Updates for this repository will not be applied.'
 echo 'Acquire::GzipIndexes "0";' > rootdir/etc/apt/apt.conf.d/02compressindex
 runtest 'failure'
 EXPECT='Ign:1 http://localhost:8080 unstable InRelease
   404  Not Found
 Hit:2 http://localhost:8080 unstable Release
+Reading package lists...
 E: Release file for http://localhost:8080/dists/unstable/Release is expired (invalid since). Updates for this repository will not be applied.'
 find aptarchive -name 'InRelease' -delete
 echo 'Acquire::GzipIndexes "0";' > rootdir/etc/apt/apt.conf.d/02compressindex
 Hit:2 http://localhost:8080 unstable Release
 Ign:3 http://localhost:8080 unstable Release.gpg
   404  Not Found
+Reading package lists...
 W: The data from 'http://localhost:8080 unstable Release' is not signed. Packages from that repository can not be authenticated.
 E: Release file for http://localhost:8080/dists/unstable/Release is expired (invalid since). Updates for this repository will not be applied."
 find aptarchive -name 'Release.gpg' -delete
 
 Get:2 $1 unstable/main amd64 Packages [$(stat -c '%s' 'aptarchive/dists/unstable/main/binary-amd64/Packages.gz') B]
 Err:2 $1 unstable/main amd64 Packages
   Hash Sum mismatch
+Reading package lists...
 W: Failed to fetch $1/dists/unstable/main/binary-amd64/Packages.gz  Hash Sum mismatch
 E: Some index files failed to download. They have been ignored, or old ones used instead." aptget update
        testfileequal 'listsdir-without-amd64.lst' "$(listcurrentlistsdirectory)"
 Get:4 $1 unstable/main amd64 Packages [$(stat -c '%s' 'aptarchive/dists/unstable/main/binary-amd64/Packages.gz') B]
 Err:4 $1 unstable/main amd64 Packages
   Hash Sum mismatch
+Reading package lists...
 W: Failed to fetch $1/dists/unstable/main/binary-amd64/Packages.gz  Hash Sum mismatch
 E: Some index files failed to download. They have been ignored, or old ones used instead." aptget update
        testfileequal 'listsdir-without-amd64.lst' "$(listcurrentlistsdirectory)"
 
        testaptgetupdate "Get:2 file:$APTARCHIVE  Packages
 Err:2 file:$APTARCHIVE  Packages
   Empty files can't be valid archives
+Reading package lists...
 W: Failed to fetch ${COMPRESSOR}:${APTARCHIVE}/Packages.$COMPRESS  Empty files can't be valid archives
 E: Some index files failed to download. They have been ignored, or old ones used instead." "empty file Packages.$COMPRESS over file"
 }
        testaptgetupdate "Get:2 http://localhost:8080  Packages
 Err:2 http://localhost:8080  Packages
   Empty files can't be valid archives
+Reading package lists...
 W: Failed to fetch ${COMPRESSOR}:$(readlink -f rootdir/var/lib/apt/lists/partial/localhost:8080_Packages.${COMPRESS})  Empty files can't be valid archives
 E: Some index files failed to download. They have been ignored, or old ones used instead." "empty file Packages.$COMPRESS over http"
 }