Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / zypp / zyppng / media / network / networkrequesterror.h
1 #ifndef ZYPP_NG_MEDIA_CURL_NETWORK_REQUEST_ERROR_H_INCLUDED
2 #define ZYPP_NG_MEDIA_CURL_NETWORK_REQUEST_ERROR_H_INCLUDED
3
4 #include <zypp/zyppng/base/zyppglobal.h>
5 #include <zypp/base/PtrTypes.h>
6 #include <boost/any.hpp>
7 #include <string>
8 #include <map>
9
10 namespace zyppng {
11
12 class NetworkRequest;
13 class NetworkRequestErrorPrivate;
14
15   /**
16    * @brief The NetworkRequestError class
17    * Represents a error that occured in \see NetworkDownloadRequest
18    * or \see NetworkRequestDispatcher
19    */
20   class LIBZYPP_NG_EXPORT NetworkRequestError
21   {
22   public:
23     enum Type {
24       NoError = 0,
25       InternalError,     //< A error in the dispatcher that is not caused by the backend, check the error string
26       Cancelled,         //< The request was cancelled
27       PeerCertificateInvalid, //< the peer certificate validation failed
28       ConnectionFailed,       //< connecting to the server failed
29       ExceededMaxLen,         //< the downloaded data exceeded the requested maximum lenght
30       InvalidChecksum,        //< The downloaded data has a different checksum than expected
31       UnsupportedProtocol,    //< The protocol given in the URL scheme is not supported by the backend
32       MalformedURL,           //< The given URL is malformed
33       TemporaryProblem,       //< There was a temporary problem with the server side
34       Timeout,                //< The request timed out
35       Forbidden,              //< Accessing the requested ressource on the server was forbidden
36       NotFound,               //< The requested path in the URL does not exist on the server
37       Unauthorized,       //<< No auth data given but authorization required
38       AuthFailed,         //<< Auth data was given, but authorization failed
39       ServerReturnedError //<< A error was returned by the server that is not explicitely handled
40     };
41
42     NetworkRequestError ();
43
44     /**
45      * @brief type
46      * Returns the type of the error
47      */
48     Type type () const;
49
50     /**
51      * @brief toString
52      * Returns a string representation of the error
53      */
54     std::string toString () const;
55
56     /**
57      * @brief isError
58      * Will return true if this is a actual error
59      */
60     bool isError () const;
61
62     /*!
63      * Tries to find \a key in the extraInfo map, if the key is not found
64      * or the value can not be converted to the requested type \a defaultVal is returned.
65      */
66     template<typename T>
67     T extraInfoValue ( const std::string &key, T &&defaultVal = T()  ) const {
68       auto &t = extraInfo();
69       auto it = t.find(key);
70       if ( it != t.end() ) {
71         try {
72           return boost::any_cast<T>( it->second );
73         } catch ( const boost::bad_any_cast &) { }
74       }
75       return defaultVal;
76     }
77
78     /*!
79      * Returns the error extraInfo map.
80      */
81     const std::map<std::string, boost::any> &extraInfo () const;
82
83     /*!
84      * Returns the string returned by the backend if available.
85      */
86     std::string nativeErrorString() const;
87
88   protected:
89     NetworkRequestError( NetworkRequestErrorPrivate &d );
90
91   private:
92     ZYPP_FWD_DECLARE_PRIVATE(NetworkRequestError)
93     zypp::RWCOW_pointer<NetworkRequestErrorPrivate> d_ptr;
94   };
95 }
96
97 #endif