LCOV - code coverage report
Current view: top level - net - serialise-error.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 22 26 84.6 %
Date: 2019-06-30 05:20:33 Functions: 2 2 100.0 %
Branches: 26 67 38.8 %

           Branch data     Line data    Source code
       1                 :            : /** @file serialise-error.cc
       2                 :            :  * @brief functions to convert Xapian::Error objects to strings and back
       3                 :            :  */
       4                 :            : /* Copyright (C) 2006,2007,2008,2009,2010,2011,2014,2015,2016 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include <xapian/error.h>
      24                 :            : 
      25                 :            : #include "pack.h"
      26                 :            : #include "serialise-error.h"
      27                 :            : 
      28                 :            : #include <string>
      29                 :            : 
      30                 :            : using namespace std;
      31                 :            : 
      32                 :            : string
      33                 :      24199 : serialise_error(const Xapian::Error &e)
      34                 :            : {
      35                 :            :     // The byte before the type name is the type code.
      36         [ +  - ]:      24199 :     string result(1, (e.get_type())[-1]);
      37         [ +  - ]:      24199 :     pack_string(result, e.get_context());
      38         [ +  - ]:      24199 :     pack_string(result, e.get_msg());
      39                 :            :     // The "error string" goes last so we don't need to store its length.
      40         [ +  - ]:      24199 :     const char * err = e.get_error_string();
      41 [ +  + ][ +  - ]:      24199 :     if (err) result += err;
      42                 :      24199 :     return result;
      43                 :            : }
      44                 :            : 
      45                 :            : void
      46                 :      24197 : unserialise_error(const string &serialised_error, const string &prefix,
      47                 :            :                   const string &new_context)
      48                 :            : {
      49                 :            :     // Use c_str() so last string is nul-terminated.
      50                 :      24197 :     const char * p = serialised_error.c_str();
      51                 :      24197 :     const char * end = p + serialised_error.size();
      52         [ +  - ]:      24197 :     if (p != end) {
      53                 :      24197 :         char type = *p++;
      54                 :            : 
      55         [ +  - ]:      24197 :         string context;
      56         [ +  - ]:      48394 :         string msg(prefix);
      57 [ +  - ][ +  - ]:      48394 :         if (!unpack_string(&p, end, context) ||
         [ -  + ][ -  + ]
      58         [ +  - ]:      24197 :             !unpack_string_append(&p, end, msg)) {
      59                 :          0 :             unpack_throw_serialisation_error(p);
      60                 :            :         }
      61                 :            : 
      62         [ +  + ]:      24197 :         const char * error_string = (p == end) ? NULL : p;
      63                 :            : 
      64         [ +  + ]:      24197 :         if (!new_context.empty()) {
      65         [ -  + ]:      24196 :             if (!context.empty()) {
      66         [ #  # ]:          0 :                 msg += "; context was: ";
      67         [ #  # ]:          0 :                 msg += context;
      68                 :            :             }
      69         [ +  - ]:      24196 :             context = new_context;
      70                 :            :         }
      71                 :            : 
      72   [ -  +  -  -  :      24197 :         switch (type) {
          +  -  -  -  -  
          +  -  +  -  -  
          -  -  -  -  -  
             +  -  -  - ]
      73                 :            : #include "xapian/errordispatch.h"
      74                 :      24197 :         }
      75                 :            :     }
      76                 :            : 
      77 [ #  # ][ #  # ]:          0 :     throw Xapian::InternalError("Unknown remote exception type", new_context);
      78                 :            : }

Generated by: LCOV version 1.11