1 /*!
2  * \brief A global mapping for errors
3  *
4  * \copyright Copyright (c) 2016-2021 Governikus GmbH & Co. KG, Germany
5  */
6 
7 #pragma once
8 
9 #include "EnumHelper.h"
10 
11 #include <QCoreApplication>
12 #include <QMap>
13 #include <QPair>
14 #include <QSharedData>
15 
16 namespace governikus
17 {
18 
19 class GlobalStatus
20 {
21 	Q_GADGET
22 	Q_DECLARE_TR_FUNCTIONS(governikus::GlobalStatus)
23 
24 	public:
25 		enum class Code
26 		{
27 			Unknown_Error,
28 			No_Error,
29 
30 			Network_ServiceUnavailable,
31 			Network_Ssl_Establishment_Error,
32 			Network_TimeOut,
33 			Network_Proxy_Error,
34 			Network_Other_Error,
35 
36 			Downloader_File_Not_Found,
37 			Downloader_Cannot_Save_File,
38 			Downloader_Data_Corrupted,
39 			Downloader_Missing_Platform,
40 			Downloader_Aborted,
41 
42 			Update_Execution_Failed,
43 
44 			Workflow_AlreadyInProgress_Error,
45 			Workflow_Communication_Missing_Redirect_Url,
46 			Workflow_Cancellation_By_User,
47 			Workflow_Card_Removed,
48 			Workflow_Cannot_Confirm_IdCard_Authenticity,
49 			Workflow_Unknown_Paos_From_EidServer,
50 			Workflow_Unexpected_Message_From_EidServer,
51 			Workflow_Preverification_Developermode_Error,
52 			Workflow_Preverification_Error,
53 			Workflow_No_Unique_AtCvc,
54 			Workflow_No_Unique_DvCvc,
55 			Workflow_No_Permission_Error,
56 			Workflow_Certificate_No_Description,
57 			Workflow_Certificate_No_Url_In_Description,
58 			Workflow_Certificate_Hash_Error,
59 			Workflow_Certificate_Sop_Error,
60 			Workflow_Error_Page_Transmission_Error,
61 			Workflow_Redirect_Transmission_Error,
62 			Workflow_Processing_Error,
63 			Workflow_TrustedChannel_Establishment_Error,
64 			Workflow_TrustedChannel_Error_From_Server,
65 			Workflow_TrustedChannel_Hash_Not_In_Description,
66 			Workflow_TrustedChannel_No_Data_Received,
67 			Workflow_TrustedChannel_Ssl_Certificate_Unsupported_Algorithm_Or_Length,
68 			Workflow_TrustedChannel_ServiceUnavailable,
69 			Workflow_TrustedChannel_TimeOut,
70 			Workflow_TrustedChannel_Proxy_Error,
71 			Workflow_TrustedChannel_Server_Format_Error,
72 			Workflow_TrustedChannel_Other_Network_Error,
73 			Workflow_Reader_Became_Inaccessible,
74 			Workflow_Server_Incomplete_Information_Provided,
75 			Workflow_Network_Ssl_Connection_Unsupported_Algorithm_Or_Length,
76 			Workflow_Network_Ssl_Certificate_Unsupported_Algorithm_Or_Length,
77 			Workflow_Network_Ssl_Hash_Not_In_Certificate_Description,
78 			Workflow_Network_Empty_Redirect_Url,
79 			Workflow_Network_Expected_Redirect,
80 			Workflow_Network_Invalid_Scheme,
81 			Workflow_Network_Malformed_Redirect_Url,
82 			Workflow_Wrong_Parameter_Invocation,
83 
84 			Paos_Unexpected_Warning,
85 
86 			Paos_Generic_Server_Error,
87 
88 			Paos_Error_AL_Unknown_Error,
89 			Paos_Error_AL_Internal_Error,
90 			Paos_Error_AL_Communication_Error,
91 			Paos_Error_DP_Trusted_Channel_Establishment_Failed,
92 			Paos_Error_SAL_Cancellation_by_User,
93 			Paos_Error_SAL_Invalid_Key,
94 
95 			Card_Not_Found,
96 			Card_Communication_Error,
97 			Card_Protocol_Error,
98 			Card_Unexpected_Transmit_Status,
99 			Card_Cancellation_By_User,
100 			Card_Input_TimeOut,
101 			Card_Invalid_Pin,
102 			Card_Invalid_Can,
103 			Card_Invalid_Puk,
104 			Card_Pin_Blocked,
105 			Card_Pin_Not_Blocked,
106 			Card_Puk_Blocked,
107 			Card_NewPin_Mismatch,
108 			Card_NewPin_Invalid_Length,
109 			Card_ValidityVerificationFailed,
110 
111 			RemoteReader_CloseCode_AbnormalClose,
112 
113 			RemoteConnector_InvalidRequest,
114 			RemoteConnector_NoSupportedApiLevel,
115 			RemoteConnector_ConnectionTimeout,
116 			RemoteConnector_ConnectionError,
117 			RemoteConnector_RemoteHostRefusedConnection
118 		};
119 
120 		enum class Origin
121 		{
122 			Server, Client
123 		};
124 
125 		enum class ExternalInformation
126 		{
127 			ECARDAPI_ERROR,
128 			LAST_URL,
129 			HTTP_STATUS_CODE,
130 			REDIRECT_URL,
131 			CERTIFICATE_ISSUER_NAME,
132 			URL_SCHEME,
133 			ACTIVATION_ERROR
134 		};
135 
136 		using ExternalInfoMap = QMap<ExternalInformation, QString>;
137 
138 		Q_ENUM(Code)
Q_ENUM(Origin)139 		Q_ENUM(Origin)
140 		Q_ENUM(ExternalInformation)
141 
142 	private:
143 		class InternalStatus
144 			: public QSharedData
145 		{
146 			public:
147 				const Code mStatusCode;
148 				const ExternalInfoMap mExternalInformation;
149 				const Origin mOrigin;
150 
151 				InternalStatus(Code pStatusCode, const ExternalInfoMap& pExternalInformation, const Origin pOrigin)
152 					: mStatusCode(pStatusCode)
153 					, mExternalInformation(pExternalInformation)
154 					, mOrigin(pOrigin)
155 				{
156 				}
157 
158 
159 				InternalStatus(Code pStatusCode, const QPair<ExternalInformation, QString>& pExternalInformation, const Origin pOrigin)
160 					: mStatusCode(pStatusCode)
161 					, mExternalInformation({
162 								{pExternalInformation.first, pExternalInformation.second}
163 							})
164 					, mOrigin(pOrigin)
165 				{
166 				}
167 
168 
169 				bool operator ==(const InternalStatus& pOther) const
170 				{
171 					return mStatusCode == pOther.mStatusCode &&
172 						   mExternalInformation == pOther.mExternalInformation &&
173 						   mOrigin == pOther.mOrigin;
174 				}
175 
176 
177 		};
178 
179 		QSharedDataPointer<InternalStatus> d;
180 		[[nodiscard]] QString getExternalInfo(ExternalInformation pType) const;
181 
182 		[[nodiscard]] QString toErrorDescriptionInternal() const;
183 
184 	public:
185 		GlobalStatus(Code pStatusCode, const ExternalInfoMap& pExternalInformation, const Origin pOrigin = Origin::Client)
d(new InternalStatus (pStatusCode,pExternalInformation,pOrigin))186 			: d(new InternalStatus(pStatusCode, pExternalInformation, pOrigin))
187 		{
188 		}
189 
190 
191 		GlobalStatus(Code pStatusCode, const QPair<ExternalInformation, QString>& pExternalInformation, const Origin pOrigin = Origin::Client)
d(new InternalStatus (pStatusCode,pExternalInformation,pOrigin))192 			: d(new InternalStatus(pStatusCode, pExternalInformation, pOrigin))
193 		{
194 		}
195 
196 
197 		GlobalStatus(Code pStatusCode = Code::Unknown_Error, const Origin pOrigin = Origin::Client)
d(new InternalStatus (pStatusCode,ExternalInfoMap (),pOrigin))198 			: d(new InternalStatus(pStatusCode, ExternalInfoMap(), pOrigin))
199 		{
200 		}
201 
202 
203 		bool operator ==(const GlobalStatus& pOther) const;
204 		[[nodiscard]] bool is(const Code pStatusCode) const;
205 
206 		[[nodiscard]] Code getStatusCode() const;
207 
208 		[[nodiscard]] QString toErrorDescription(const bool pSimplifiedVersion = false) const;
209 		[[nodiscard]] QString getExternalInfo(const QString& pToken = QStringLiteral("; ")) const;
210 
211 		[[nodiscard]] Origin getOrigin() const;
212 		[[nodiscard]] bool isOriginServer() const;
213 
214 		[[nodiscard]] bool isNoError() const;
215 		[[nodiscard]] bool isError() const;
216 		[[nodiscard]] bool isCancellationByUser() const;
217 		[[nodiscard]] bool isMessageMasked() const;
218 };
219 
220 using Origin = GlobalStatus::Origin;
221 
222 defineEnumOperators(GlobalStatus::Code)
223 
224 } // namespace governikus
225 
226 
227 QDebug operator <<(QDebug pDbg, const governikus::GlobalStatus& pStatus);
228