errormodulehandler.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. \brief ErrorCategory - To decide what ErrorHandlerModule needs to be called and easily identify where it came from
  3. */
  4. enum ErrorCategory
  5. {
  6. Unknown/* = -1*/,
  7. //! Generic error group
  8. Generic,
  9. //! Error group for when something went wrong while trying to connect to the server
  10. ConnectErrorClient,
  11. //! Error group for when the Client did connect to the Server, but the server rejected the connection
  12. ConnectErrorServer,
  13. //! Error group for connect errors thrown from Script
  14. ConnectErrorScript,
  15. //! Error group for when Client is kicked from server
  16. ClientKicked,
  17. //! Error group for BIOS errors
  18. BIOSError,
  19. };
  20. /**
  21. \brief The error handler itself, for managing and distributing errors to modules
  22. * Manages the ErrorHandlerModule instances inserted in Init.
  23. * API comes with several functions to Create, Throw and extract data from error codes.
  24. * The format used is an int which is made up of two shorts, one that holds the category and one that holds the code.
  25. * Therefore when looking at an error code, it is much easier to identify when looking at the hex value.
  26. */
  27. class ErrorModuleHandler
  28. {
  29. /**
  30. \brief Creates and throws the error code, sending it to the handler of the category.
  31. \param category \p ErrorCategory Category the error is thrown from
  32. \param code \p int The code that the error belongs to inside the category between [-32768, 32767]
  33. \param additionalInfo \p string Any additional info regarding the error, usually data
  34. \return \p int The full error code
  35. @code
  36. int errorCode = ErrorModuleHandler.ThrowError( ErrorCategory.ConnectErrorClient, -1 );
  37. Print( errorCode );
  38. >> errorCode = 196607
  39. @endcode
  40. */
  41. static proto int ThrowError(ErrorCategory category, int code, string additionalInfo = "");
  42. /**
  43. \brief Throws the error code and sends it to the handler of the category.
  44. \param errorCode \p int The full error code
  45. \param additionalInfo \p string Any additional info regarding the error, usually data
  46. \return \p int The full error code
  47. @code
  48. int errorCode = ErrorModuleHandler.ThrowErrorCode( 0x0002FFFF );
  49. Print( errorCode );
  50. >> errorCode = 196607
  51. @endcode
  52. */
  53. static proto int ThrowErrorCode(int errorCode, string additionalInfo = "");
  54. /**
  55. \brief Creates full error code.
  56. \param category \p ErrorCategory Category the error is thrown from
  57. \param code \p int The code that the error belongs to inside the category between [-32768, 32767]
  58. \return \p int The full error code
  59. @code
  60. int errorCode = ErrorModuleHandler.CreateError( ErrorCategory.ConnectErrorClient, -1 );
  61. Print( errorCode );
  62. >> errorCode = 196607
  63. @endcode
  64. */
  65. static proto int CreateError(ErrorCategory category, int code);
  66. /**
  67. \brief Returns the category the error was thrown from.
  68. \param errorCode \p int The full error code
  69. \return \p ErrorCategory The ErrorCategory the error was thrown from
  70. @code
  71. ErrorCategory category = ErrorModuleHandler.GetCategoryFromError( 0x0002FFFF );
  72. Print( category );
  73. >> category = 2
  74. @endcode
  75. */
  76. static proto ErrorCategory GetCategoryFromError(int errorCode);
  77. /**
  78. \brief Returns the code of the error.
  79. \param errorCode \p int The full error code
  80. \return \p int The code of the error
  81. @code
  82. int code = ErrorModuleHandler.GetCodeFromError( 0x0002FFFF );
  83. Print( code );
  84. >> code = -1
  85. @endcode
  86. */
  87. static proto int GetCodeFromError(int errorCode);
  88. /**
  89. \brief Returns a formatted string of the error code.
  90. \param errorCode \p int The full error code
  91. \return \p string A formatted string of the error code
  92. @code
  93. string formattedCode = ErrorModuleHandler.GetErrorHex( 196607 );
  94. Print( formattedCode );
  95. >> formattedCode = '0x0002FFFF'
  96. @endcode
  97. */
  98. static proto owned string GetErrorHex(int errorCode);
  99. /**
  100. \brief Adds a module handler to the ErrorModuleHandler.
  101. \param category \p ErrorCategory Category the module is for
  102. \param errorModule \p ErrorHandlerModule The class containing the information and codes for the category.
  103. \return \p bool Whether the adding of the module was successful or not
  104. */
  105. static proto bool AddModule(ErrorCategory category, notnull ErrorHandlerModule errorModule);
  106. /**
  107. \brief Removes a module handler from the ErrorModuleHandler.
  108. \param category \p ErrorCategory Category the module is for
  109. \return \p bool Whether the removing of the module was successful or not
  110. */
  111. static proto bool RemoveModule(ErrorCategory category);
  112. /**
  113. \brief Gets the Client Message for specified error
  114. \param category \p ErrorCategory Category the error is thrown from
  115. \param code \p int The code that the error belongs to inside the category between [-32768, 32767]
  116. \param additionalInfo \p string Any additional info regarding the error, usually data
  117. \return \p string The message which would appear on Client
  118. */
  119. static proto string GetClientMessage(ErrorCategory category, int code, string additionalInfo = "");
  120. /**
  121. \brief Gets the Client Message for specified error
  122. \param errorCode \p int The full error code
  123. \param additionalInfo \p string Any additional info regarding the error, usually data
  124. \return \p string The message which would appear on Client
  125. */
  126. static proto string GetClientMessageByCode(int errorCode, string additionalInfo = "");
  127. /**
  128. \brief Gets the Client Message for specified error, while attempting to restore information on the most recent error
  129. \param category \p ErrorCategory Category the error is thrown from
  130. \param code \p int The code that the error belongs to inside the category between [-32768, 32767]
  131. \return \p string The message which would appear on Client
  132. */
  133. static proto string GetLastClientMessage(ErrorCategory category, int code);
  134. /**
  135. \brief Gets the Client Message for specified error, while attempting to restore information on the most recent error
  136. \param errorCode \p int The full error code
  137. \return \p string The message which would appear on Client
  138. */
  139. static proto string GetLastClientMessageByCode(int errorCode);
  140. /**
  141. \brief Gets the Server Message for specified error
  142. \param category \p ErrorCategory Category the error is thrown from
  143. \param code \p int The code that the error belongs to inside the category between [-32768, 32767]
  144. \param additionalInfo \p string Any additional info regarding the error, usually data
  145. \return \p string The message which would appear on Server
  146. */
  147. static proto string GetServerMessage(ErrorCategory category, int code, string additionalInfo = "");
  148. /**
  149. \brief Gets the Server Message for specified error
  150. \param errorCode \p int The full error code
  151. \param additionalInfo \p string Any additional info regarding the error, usually data
  152. \return \p string The message which would appear on Server
  153. */
  154. static proto string GetServerMessageByCode(int errorCode, string additionalInfo = "");
  155. /**
  156. \brief Gets the Server Message for specified error, while attempting to restore information on the most recent error
  157. \param category \p ErrorCategory Category the error is thrown from
  158. \param code \p int The code that the error belongs to inside the category between [-32768, 32767]
  159. \return \p string The message which would appear on Server
  160. */
  161. static proto string GetLastServerMessage(ErrorCategory category, int code);
  162. /**
  163. \brief Gets the Server Message for specified error, while attempting to restore information on the most recent error
  164. \param errorCode \p int The full error code
  165. \return \p string The message which would appear on Server
  166. */
  167. static proto string GetLastServerMessageByCode(int errorCode);
  168. /**
  169. \brief Gets the EMH Instance
  170. \return \p ErrorModuleHandler The ErrorModuleHandler Instance
  171. */
  172. static proto native ErrorModuleHandler GetInstance();
  173. static proto void GetErrorModules(notnull out array<ErrorHandlerModule> errorModules);
  174. /**
  175. \brief Wrapper for AddModule to give feedback whether it succeeded or not.
  176. \param errorModule \p ErrorHandlerModule The ErrorHandlerModule to add
  177. */
  178. void SafeAddModule(notnull ErrorHandlerModule errorModule)
  179. {
  180. if ( !AddModule(errorModule.GetCategory(), errorModule) )
  181. Error(string.Format("[EMH] Adding %1 failed. (Category: %2)", errorModule, errorModule.GetCategory()));
  182. }
  183. /**
  184. \brief Wrapper for RemoveModule to give feedback whether it succeeded or not.
  185. \param errorModule \p ErrorHandlerModule The ErrorHandlerModule to add
  186. */
  187. void SafeRemoveModule(notnull ErrorHandlerModule errorModule)
  188. {
  189. if ( !RemoveModule(errorModule.GetCategory()) )
  190. Error(string.Format("[EMH] Removing %1 failed. (Category: %2)", errorModule, errorModule.GetCategory()));
  191. }
  192. /**
  193. \brief Wrapper for RemoveModule to give feedback whether it succeeded or not.
  194. \param category \p ErrorCategory Category to remove
  195. */
  196. void SafeRemoveModule(ErrorCategory category)
  197. {
  198. if ( !RemoveModule(category) )
  199. Error(string.Format("[EMH] Removing %1 failed.", category));
  200. }
  201. /**
  202. \brief Gets called shortly after creation of ErrorModuleHandler.
  203. */
  204. private void Init()
  205. {
  206. if (!g_Game.IsDedicatedServer())
  207. {
  208. SafeAddModule(new ConnectErrorClientModule);
  209. SafeAddModule(new ConnectErrorServerModule);
  210. SafeAddModule(new ConnectErrorScriptModule);
  211. }
  212. SafeAddModule(new ClientKickedModule);
  213. SafeAddModule(new BIOSErrorModule);
  214. }
  215. /**
  216. \brief is called by DayZGame to pass Events.
  217. */
  218. void OnEvent(EventType eventTypeId, Param params)
  219. {
  220. array<ErrorHandlerModule> errorModules = new array<ErrorHandlerModule>;
  221. GetErrorModules(errorModules);
  222. foreach (ErrorHandlerModule module : errorModules)
  223. {
  224. module.OnEvent(eventTypeId, params);
  225. }
  226. }
  227. }