/* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
 *
 * schroot 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.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  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., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 *********************************************************************/

#ifndef SBUILD_ERROR_H
#define SBUILD_ERROR_H

#include <map>
#include <stdexcept>
#include <string>
#include <typeinfo>

#include <boost/format.hpp>

namespace sbuild
{

  /**
   * Error exception base class.
   */
  class error_base : public std::runtime_error
  {
  protected:
    /**
     * The constructor.
     *
     * @param error the error message.
     */
    error_base(std::string const& error):
      runtime_error(error),
      reason()
    {
    }

    /**
     * The constructor.
     *
     * @param error the error message.
     * @param reason further information about the error
     */
    error_base(std::string const& error,
	  std::string const& reason):
      runtime_error(error),
      reason(reason)
    {
    }

  public:
    /// The destructor.
    virtual ~error_base () throw ()
    {}

    /**
     * Get the reason for the error.
     *
     * @returns the reason.
     */
    virtual const char *
    why () const throw ()
    {
      return this->reason.c_str();
    }

    /**
     * Get the reason for the error.
     *
     * @returns the reason.
     */
    std::string const&
    get_reason () const
    {
      return this->reason;
    }

    /**
     * Set the reason for the error.
     *
     * @param reason further information about the error
     */
    void
    set_reason (std::string const& reason)
    {
      this->reason = reason;
    }

  protected:
    /**
     * Copy the reason from an exception.  This is only done if the
     * error is derived from error_base.
     *
     * @param error the error code.
     */
    void
    copy_reason (std::runtime_error const& error)
    {
      try
	{
	  error_base const& eb(dynamic_cast<error_base const&>(error));
	  set_reason(eb.get_reason());
	}
      catch (std::bad_cast const& discard)
	{
	}
    }

  private:
    /// The reason for the error.
    std::string reason;
  };

  /**
   * Error exception class.
   */
  template <typename T>
  class error : public error_base
  {
  public:
    typedef T error_type;
    typedef std::map<error_type,const char *> map_type;


    /**
     * The constructor.
     *
     * @param error the error message.
     */
    error(std::string const& error):
      error_base(error)
    {
    }

    /**
     * The constructor.
     *
     * @param error the error message.
     * @param reason further information about the error
     */
    error(std::string const& error,
	  std::string const& reason):
      error_base(error, reason)
    {
    }

    /// The destructor.
    virtual ~error () throw ()
    {}

  private:
    /// Mapping between error code and string.
    static map_type error_strings;

    /**
     * Get a translated error string.
     *
     * @param error the error code.
     * @returns a translated error string.
     */
    static const char *
    get_error (error_type error);

  protected:
    /**
     * Format an error message.
     *
     * @param context1 context of the error.
     * @param context2 additional context of the error.
     * @param context3 additional context of the error.
     * @param error the error code.
     * @param detail1 details of the error.
     * @param detail2 additional details of the error.
     * @returns a translated error message.
     */
    template <typename A, typename B, typename C, typename D, typename E>
    static std::string
    format_error (A const&   context1,
		  B const&   context2,
		  C const&   context3,
		  error_type error,
		  D const&   detail1,
		  E const&   detail2);

    /**
     * Format an error message.
     *
     * @param context1 context of the error.
     * @param context2 additional context of the error.
     * @param context3 additional context of the error.
     * @param error the error code.
     * @param detail1 details of the error.
     * @param detail2 additional details of the error.
     * @returns a translated error message.
     */
    template <typename A, typename B, typename C, typename D, typename E>
    static std::string
    format_error (A const&                  context1,
		  B const&                  context2,
		  C const&                  context3,
		  std::runtime_error const& error,
		  D const&                  detail1,
		  E const&                  detail2);

    /**
     * Format an reason string.
     *
     * @param context1 context of the error.
     * @param context2 additional context of the error.
     * @param context3 additional context of the error.
     * @param error the error or error code.
     * @param detail1 details of the error.
     * @param detail2 additional details of the error.
     * @returns a translated error message.
     */
    template <typename A, typename B, typename C, typename R, typename D, typename E>
    static std::string
    format_reason (A const&   context1,
		   B const&   context2,
		   C const&   context3,
		   R const&   error,
		   D const&   detail1,
		   E const&   detail2);

    template<typename A>
    static void
    add_detail(boost::format& fmt,
	       A const&       value);

    static void
    add_detail(boost::format&        fmt,
	       std::exception const& value);

    template<typename A>
    static void
    add_reason(std::string& reason,
	       A const&     value);

    static void
    add_reason(std::string&          reason,
	       std::exception const& value);

  };

}

#include "sbuild-error.tcc"

#endif /* SBUILD_ERROR_H */

/*
 * Local Variables:
 * mode:C++
 * End:
 */
