errorhandlermodule.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //-----------------------------------------------------------------------------
  2. // Definition
  3. //-----------------------------------------------------------------------------
  4. //! Definition and API of an ErrorHandlerModule - Do not insert any logic here! (as this class is not moddable)
  5. class ErrorHandlerModule
  6. {
  7. //! Returns the category the module handles.
  8. proto native ErrorCategory GetCategory();
  9. //! Set the category the module handles.
  10. proto native void SetCategory(ErrorCategory category);
  11. //! Event that gets triggered when an error of the owned category is thrown.
  12. protected void OnErrorThrown(int errorCode, owned string additionalInfo = "")
  13. {
  14. #ifdef ENABLE_LOGGING
  15. Print(ErrorModuleHandler.GetErrorHex(errorCode));
  16. #endif
  17. }
  18. //! Retrieve the message shown on Client
  19. string GetClientMessage(int errorCode, string additionalInfo = "")
  20. {
  21. return GetSimpleMessage(errorCode, additionalInfo);
  22. }
  23. //! Retrieve the message shown on Client
  24. string GetLastClientMessage(int errorCode)
  25. {
  26. return GetSimpleMessage(errorCode);
  27. }
  28. //! Retrieve the message shown on Server
  29. string GetServerMessage(int errorCode, string additionalInfo = "")
  30. {
  31. return GetSimpleMessage(errorCode, additionalInfo);
  32. }
  33. //! Retrieve the message shown on Server
  34. string GetLastServerMessage(int errorCode)
  35. {
  36. return GetSimpleMessage(errorCode);
  37. }
  38. //! Simple message of just code and info
  39. string GetSimpleMessage(int errorCode, string additionalInfo = "")
  40. {
  41. return string.Format("[%1]: %2", ErrorModuleHandler.GetErrorHex(errorCode), additionalInfo);
  42. }
  43. //! Event called by ErrorModuleHandler
  44. void OnEvent(EventType eventTypeId, Param params)
  45. {
  46. }
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Script override
  50. //-----------------------------------------------------------------------------
  51. /**
  52. \brief This is where to input logic and extend functionality of ErrorHandlerModule
  53. */
  54. class ErrorHandlerModuleScript : ErrorHandlerModule
  55. {
  56. protected string m_Header = ""; //!< \p Optional: Header (e.g. The header of a Dialogue box)
  57. protected string m_Prefix = ""; //!< \p Optional: Prefix (e.g. Fixed text at the start of the messages in the module)
  58. protected ref UIScriptedMenu m_UIHandler = null; //!< \p Optional: The UI the handler might generally use
  59. protected int m_LastErrorThrown = 0; //!< Holds the last thrown error in this module, defaults to 0
  60. protected string m_LastAdditionalInfo = ""; //!< Holds the last additional info passed in
  61. /**
  62. \brief \p Map containing the codes that exist for the ErrorHandlerModule
  63. * The code links to ErrorProperties
  64. * This contains at the very least the Message for the error
  65. * Additionally, it can contain the way to handle the error (e.g. Throw a Dialogue with the message)
  66. */
  67. protected ref map<int, ref ErrorProperties> m_ErrorDataMap = new map<int, ref ErrorProperties>();
  68. //! Constructor, by default calls the function that will fill the ErrorDataMap
  69. void ErrorHandlerModuleScript()
  70. {
  71. InitOptionalVariables();
  72. FillErrorDataMap();
  73. }
  74. void ~ErrorHandlerModuleScript()
  75. {
  76. if (m_UIHandler)
  77. {
  78. delete m_UIHandler;
  79. }
  80. }
  81. //! Function which gets called before FillErrorDataMap, designed to set Optional Variales before ErrorProperties are created
  82. void InitOptionalVariables()
  83. {
  84. }
  85. //! Function to fill up m_ErrorDataMap, gets called in the Constructor
  86. void FillErrorDataMap()
  87. {
  88. //! Already insert the default "UNKNOWN ERROR" message for code "-1"
  89. InsertDialogueErrorProperties(-1, "#server_browser_error_unknown");
  90. }
  91. /**
  92. \brief Fetches the ErrorProperties for the error code.
  93. \param errorCode \p int The full error code
  94. \return \p ErrorProperties The data and handling for the error
  95. */
  96. ErrorProperties GetProperties(int errorCode)
  97. {
  98. int error = ErrorModuleHandler.GetCodeFromError(errorCode);
  99. ErrorProperties properties = null;
  100. if (!m_ErrorDataMap.Find(error, properties))
  101. {
  102. Error(string.Format("[EHM] Could not find any properties for error %1(%2) in %3", errorCode, ErrorModuleHandler.GetErrorHex(errorCode), this));
  103. }
  104. return properties;
  105. }
  106. /**
  107. \brief Fetches the Client message for the error code.
  108. \param errorCode \p int The full error code
  109. \return \p string The Client message for the error
  110. */
  111. override string GetClientMessage(int errorCode, string additionalInfo = "")
  112. {
  113. ErrorProperties properties = GetProperties(errorCode);
  114. if ( properties )
  115. {
  116. return properties.GetClientMessage(additionalInfo);
  117. }
  118. else
  119. {
  120. return additionalInfo;
  121. }
  122. }
  123. /**
  124. \brief Fetches the Client message for the error code, attempting to retrieve the data from the latest.
  125. \param errorCode \p int The full error code to check against
  126. \return \p string The Client message for the error
  127. */
  128. override string GetLastClientMessage(int errorCode)
  129. {
  130. if (errorCode == m_LastErrorThrown)
  131. {
  132. return GetClientMessage(errorCode, m_LastAdditionalInfo);
  133. }
  134. else
  135. {
  136. ErrorEx(string.Format("Was unable to get the information on the last error as another has already occurred. (%1 != %2)", ErrorModuleHandler.GetErrorHex(errorCode), ErrorModuleHandler.GetErrorHex(m_LastErrorThrown)));
  137. return GetClientMessage(errorCode);
  138. }
  139. }
  140. /**
  141. \brief Fetches the Server message for the error code.
  142. \param errorCode \p int The full error code
  143. \return \p string The Server message for the error
  144. */
  145. override string GetServerMessage(int errorCode, string additionalInfo = "")
  146. {
  147. ErrorProperties properties = GetProperties(errorCode);
  148. if ( properties )
  149. {
  150. return properties.GetServerMessage(additionalInfo);
  151. }
  152. else
  153. {
  154. return additionalInfo;
  155. }
  156. }
  157. /**
  158. \brief Fetches the Server message for the error code, attempting to retrieve the data from the latest.
  159. \param errorCode \p int The full error code to check against
  160. \return \p string The Server message for the error
  161. */
  162. override string GetLastServerMessage(int errorCode)
  163. {
  164. if (errorCode == m_LastErrorThrown)
  165. {
  166. return GetServerMessage(errorCode, m_LastAdditionalInfo);
  167. }
  168. else
  169. {
  170. ErrorEx(string.Format("Was unable to get the information on the last error as another has already occurred. (%1 != %2)", ErrorModuleHandler.GetErrorHex(errorCode), ErrorModuleHandler.GetErrorHex(m_LastErrorThrown)), ErrorExSeverity.WARNING);
  171. return GetServerMessage(errorCode);
  172. }
  173. }
  174. /**
  175. \brief Event that gets triggered when an error of the owned category is thrown.
  176. * Do not call directly!
  177. * Call ErrorModuleHandler.ThrowError instead
  178. \param errorCode \p int The full error code
  179. \param additionalInfo \p string Any additional info regarding the error, usually data
  180. */
  181. protected override void OnErrorThrown(int errorCode, owned string additionalInfo = "")
  182. {
  183. super.OnErrorThrown(errorCode, additionalInfo);
  184. m_LastErrorThrown = errorCode;
  185. m_LastAdditionalInfo = additionalInfo;
  186. ErrorProperties properties = GetProperties(errorCode);
  187. if ( properties )
  188. {
  189. properties.HandleError(errorCode, additionalInfo);
  190. }
  191. else
  192. {
  193. ErrorEx(string.Format("Error code %1(%2) was thrown but no ErrorProperties was found for it in category %3.", errorCode, ErrorModuleHandler.GetErrorHex(errorCode), GetCategory().ToString()));
  194. if (m_ErrorDataMap.Find(-1, properties))
  195. {
  196. properties.HandleError(errorCode, additionalInfo);
  197. }
  198. }
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Insert helpers
  202. //-----------------------------------------------------------------------------
  203. //! Insert an error with Dialogue as handling, using the Optional Variables
  204. void InsertDialogueErrorProperties(int code, string message, int dialogButtonType = DBT_OK, int defaultButton = DBB_OK, int dialogMeaningType = DMT_EXCLAMATION, bool displayAdditionalInfo = true)
  205. {
  206. m_ErrorDataMap.Insert(code, DialogueErrorProperties(string.Format("%1%2", m_Prefix, message), message, m_Header, m_UIHandler, dialogButtonType, defaultButton, dialogMeaningType, displayAdditionalInfo));
  207. }
  208. //! Insert an error with Dialogue as handling with custom header
  209. void InsertHeaderDialogueErrorProperties(int code, string message, string header, int dialogButtonType = DBT_OK, int defaultButton = DBB_OK, int dialogMeaningType = DMT_EXCLAMATION, bool displayAdditionalInfo = true)
  210. {
  211. m_ErrorDataMap.Insert(code, DialogueErrorProperties(string.Format("%1%2", m_Prefix, message), message, header, m_UIHandler, dialogButtonType, defaultButton, dialogMeaningType, displayAdditionalInfo));
  212. }
  213. //! Insert an error with Dialogue as handling with custom prefix
  214. void InsertPrefixDialogueErrorProperties(int code, string message, string prefix, int dialogButtonType = DBT_OK, int defaultButton = DBB_OK, int dialogMeaningType = DMT_EXCLAMATION, bool displayAdditionalInfo = true)
  215. {
  216. m_ErrorDataMap.Insert(code, DialogueErrorProperties(string.Format("%1%2", prefix, message), message, m_Header, m_UIHandler, dialogButtonType, defaultButton, dialogMeaningType, displayAdditionalInfo));
  217. }
  218. //! Insert an error with Dialogue as handling with extended prefix
  219. void InsertExtendedPrefixDialogueErrorProperties(int code, string message, string prefix, int dialogButtonType = DBT_OK, int defaultButton = DBB_OK, int dialogMeaningType = DMT_EXCLAMATION, bool displayAdditionalInfo = true)
  220. {
  221. m_ErrorDataMap.Insert(code, DialogueErrorProperties(string.Format("%1%2%3", m_Prefix, prefix, message), message, m_Header, m_UIHandler, dialogButtonType, defaultButton, dialogMeaningType, displayAdditionalInfo));
  222. }
  223. //! Insert an error with Dialogue as handling with extended prefix and separate server message
  224. void InsertExtendedPrefixSplitDialogueErrorProperties(int code, string message, string prefix, string serverMessage, int dialogButtonType = DBT_OK, int defaultButton = DBB_OK, int dialogMeaningType = DMT_EXCLAMATION, bool displayAdditionalInfo = true)
  225. {
  226. m_ErrorDataMap.Insert(code, DialogueErrorProperties(string.Format("%1%2%3", m_Prefix, prefix, message), serverMessage, m_Header, m_UIHandler, dialogButtonType, defaultButton, dialogMeaningType, displayAdditionalInfo));
  227. }
  228. //! Insert an error with Dialogue as handling with separate server message
  229. void InsertSplitDialogueErrorProperties(int code, string message, string serverMessage, int dialogButtonType = DBT_OK, int defaultButton = DBB_OK, int dialogMeaningType = DMT_EXCLAMATION, bool displayAdditionalInfo = true)
  230. {
  231. m_ErrorDataMap.Insert(code, DialogueErrorProperties(string.Format("%1%2", m_Prefix, message), serverMessage, m_Header, m_UIHandler, dialogButtonType, defaultButton, dialogMeaningType, displayAdditionalInfo));
  232. }
  233. //! Insert an error with no handling
  234. void InsertErrorProperties(int code, string message = "")
  235. {
  236. m_ErrorDataMap.Insert(code, ErrorProperties(message, message));
  237. }
  238. }