Program Listing for File error.h

Return to documentation for file (include/zim/error.h)

/*
 * Copyright (C) 2020 Matthieu Gautier <mgautier@kymeria.fr>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#ifndef ZIM_ERROR_H
#define ZIM_ERROR_H

#include "zim.h"
#include "tools.h"

#include <stdexcept>
#include <typeinfo>

namespace zim
{
  class LIBZIM_API ZimFileFormatError : public std::runtime_error
  {
    public:
      explicit ZimFileFormatError(const std::string& msg)
        : std::runtime_error(msg)
        { }
  };

  class LIBZIM_API InvalidType: public std::logic_error
  {
    public:
      explicit InvalidType(const std::string& msg)
        : std::logic_error(msg)
      {}
  };

  class LIBZIM_API EntryNotFound : public std::runtime_error
  {
    public:
      explicit EntryNotFound(const std::string& msg)
       : std::runtime_error(msg)
      {}
  };

  class LIBZIM_API CreatorError : public std::runtime_error
  {
    public:
      explicit CreatorError(const std::string& message)
       : std::runtime_error(message)
      {}
  };

  class LIBZIM_API InvalidEntry : public CreatorError
  {
    public:
      explicit InvalidEntry(const std::string& message)
       : CreatorError(message)
      {}
  };

  class LIBZIM_API IncoherentImplementationError : public CreatorError
  {
    public:
      explicit IncoherentImplementationError(const std::string& message)
       : CreatorError(message)
      {}
  };

  class LIBZIM_API TitleIndexingError : public CreatorError
  {
    public:
      explicit TitleIndexingError(const std::string& message)
       : CreatorError(message)
      {}
  };

  class LIBZIM_API AsyncError : public CreatorError
  {
    public:
      explicit AsyncError(const std::exception_ptr exception)
       : CreatorError(buildErrorMessage(exception)),
         m_exception(exception)
      {}

      [[noreturn]] void rethrow() const {
        std::rethrow_exception(m_exception);
      }

    private: // data
      std::exception_ptr m_exception;

    private: // function
      static std::string buildErrorMessage(const std::exception_ptr exception) {
        try {
          std::rethrow_exception(exception);
        } catch (const std::exception& e) {
          return Formatter()
                 << "Asynchronous error: " << typeid(e).name() << std::endl
                 << e.what();
        } catch (...) {
          return "Unknown asynchronous exception";
        }
      }
  };

  class LIBZIM_API CreatorStateError : public CreatorError
  {
    public:
      explicit CreatorStateError()
       : CreatorError("Creator is in error state.")
      {}
  };
}

#endif // ZIM_ERROR_H