PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/code/tags/2.0/boss-common/Common/Error.h

http://better-oblivion-sorting-software.googlecode.com/
C++ Header | 244 lines | 179 code | 24 blank | 41 comment | 3 complexity | 33f27e909298817202bfe1bbfaa3c25e MD5 | raw file
Possible License(s): GPL-3.0
  1. /* BOSS
  2. A "one-click" program for users that quickly optimises and avoids
  3. detrimental conflicts in their TES IV: Oblivion, Nehrim - At Fate's Edge,
  4. TES V: Skyrim, Fallout 3 and Fallout: New Vegas mod load orders.
  5. Copyright (C) 2009-2012 BOSS Development Team.
  6. This file is part of BOSS.
  7. BOSS is free software: you can redistribute
  8. it and/or modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation, either version 3 of
  10. the License, or (at your option) any later version.
  11. BOSS is distributed in the hope that it will
  12. be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with BOSS. If not, see
  17. <http://www.gnu.org/licenses/>.
  18. $Revision: 2188 $, $Date: 2011-01-20 10:05:16 +0000 (Thu, 20 Jan 2011) $
  19. */
  20. //Contains the BOSS exception class.
  21. #ifndef __BOSS_ERROR_H__
  22. #define __BOSS_ERROR_H__
  23. #include <string>
  24. #include <boost/cstdint.hpp>
  25. #include <boost/format.hpp>
  26. #include <boost/algorithm/string.hpp>
  27. #include "Common/DllDef.h"
  28. namespace boss {
  29. using namespace std;
  30. using boost::format;
  31. //Error Codes. Also return codes more generally.
  32. //DO NOT CHANGE THEIR VALUES. THEY MUST BE INVARIANT ACROSS RELEASES FOR API USERS.
  33. BOSS_COMMON const uint32_t BOSS_OK = 0;
  34. BOSS_COMMON const uint32_t BOSS_ERROR_NO_MASTER_FILE = 1;
  35. BOSS_COMMON const uint32_t BOSS_ERROR_FILE_READ_FAIL = 2;
  36. BOSS_COMMON const uint32_t BOSS_ERROR_FILE_WRITE_FAIL = 3;
  37. BOSS_COMMON const uint32_t BOSS_ERROR_FILE_NOT_UTF8 = 4;
  38. BOSS_COMMON const uint32_t BOSS_ERROR_FILE_NOT_FOUND = 5;
  39. BOSS_COMMON const uint32_t BOSS_ERROR_FILE_PARSE_FAIL = 6;
  40. BOSS_COMMON const uint32_t BOSS_ERROR_CONDITION_EVAL_FAIL = 7;
  41. BOSS_COMMON const uint32_t BOSS_ERROR_REGEX_EVAL_FAIL = 8;
  42. BOSS_COMMON const uint32_t BOSS_ERROR_NO_GAME_DETECTED = 9;
  43. BOSS_COMMON const uint32_t BOSS_ERROR_ENCODING_CONVERSION_FAIL = 10;
  44. BOSS_COMMON const uint32_t BOSS_ERROR_PLUGIN_BEFORE_MASTER = 39;
  45. BOSS_COMMON const uint32_t BOSS_ERROR_FIND_ONLINE_MASTERLIST_REVISION_FAIL = 11;
  46. BOSS_COMMON const uint32_t BOSS_ERROR_FIND_ONLINE_MASTERLIST_DATE_FAIL = 12;
  47. BOSS_COMMON const uint32_t BOSS_ERROR_READ_UPDATE_FILE_LIST_FAIL = 13;
  48. BOSS_COMMON const uint32_t BOSS_ERROR_FILE_CRC_MISMATCH = 14;
  49. BOSS_COMMON const uint32_t BOSS_ERROR_FS_FILE_MOD_TIME_READ_FAIL = 15;
  50. BOSS_COMMON const uint32_t BOSS_ERROR_FS_FILE_MOD_TIME_WRITE_FAIL = 16;
  51. BOSS_COMMON const uint32_t BOSS_ERROR_FS_FILE_RENAME_FAIL = 17;
  52. BOSS_COMMON const uint32_t BOSS_ERROR_FS_FILE_DELETE_FAIL = 18;
  53. BOSS_COMMON const uint32_t BOSS_ERROR_FS_CREATE_DIRECTORY_FAIL = 19;
  54. BOSS_COMMON const uint32_t BOSS_ERROR_FS_ITER_DIRECTORY_FAIL = 20;
  55. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_INIT_FAIL = 21;
  56. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_SET_ERRBUFF_FAIL = 22;
  57. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_SET_OPTION_FAIL = 23;
  58. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_SET_PROXY_FAIL = 24;
  59. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_SET_PROXY_TYPE_FAIL = 25;
  60. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_SET_PROXY_AUTH_FAIL = 26;
  61. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_SET_PROXY_AUTH_TYPE_FAIL = 27;
  62. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_PERFORM_FAIL = 28;
  63. BOSS_COMMON const uint32_t BOSS_ERROR_CURL_USER_CANCEL = 29;
  64. BOSS_COMMON const uint32_t BOSS_ERROR_GUI_WINDOW_INIT_FAIL = 30;
  65. BOSS_COMMON const uint32_t BOSS_OK_NO_UPDATE_NECESSARY = 31;
  66. BOSS_COMMON const uint32_t BOSS_ERROR_LO_MISMATCH = 32;
  67. BOSS_COMMON const uint32_t BOSS_ERROR_NO_MEM = 33;
  68. BOSS_COMMON const uint32_t BOSS_ERROR_INVALID_ARGS = 34;
  69. BOSS_COMMON const uint32_t BOSS_ERROR_NETWORK_FAIL = 35;
  70. BOSS_COMMON const uint32_t BOSS_ERROR_NO_INTERNET_CONNECTION = 36;
  71. BOSS_COMMON const uint32_t BOSS_ERROR_NO_TAG_MAP = 37;
  72. BOSS_COMMON const uint32_t BOSS_ERROR_PLUGINS_FULL = 38;
  73. BOSS_COMMON const uint32_t BOSS_ERROR_MAX = BOSS_ERROR_PLUGINS_FULL;
  74. class BOSS_COMMON boss_error {
  75. public:
  76. //For general errors not referencing specific files.
  77. inline boss_error(const uint32_t internalErrCode)
  78. : errCode(internalErrCode), errString(""), errSubject("") {}
  79. //For general errors referencing specific files.
  80. inline boss_error(const uint32_t internalErrCode, const string internalErrSubject)
  81. : errCode(internalErrCode), errString(""), errSubject(internalErrSubject) {}
  82. //For errors from BOOST Filesystem functions.
  83. inline boss_error(const uint32_t internalErrCode, const string internalErrSubject, const string externalErrString)
  84. : errCode(internalErrCode), errString(externalErrString), errSubject(internalErrSubject) {}
  85. //For errors from cURL functions.
  86. inline boss_error(const string externalErrString, const uint32_t internalErrCode)
  87. : errCode(internalErrCode), errString(externalErrString), errSubject("") {}
  88. //Returns the error string for the object.
  89. inline string getString() {
  90. switch(errCode) {
  91. case BOSS_OK:
  92. return "No error.";
  93. case BOSS_ERROR_NO_MASTER_FILE:
  94. return "No game master .esm file found!";
  95. case BOSS_ERROR_FILE_READ_FAIL:
  96. return "\"" + errSubject + "\" cannot be read!";
  97. case BOSS_ERROR_FILE_WRITE_FAIL:
  98. return "\"" + errSubject + "\" cannot be written to!";
  99. case BOSS_ERROR_FILE_NOT_UTF8:
  100. return "\"" + errSubject + "\" is not encoded in valid UTF-8!";
  101. case BOSS_ERROR_FILE_NOT_FOUND:
  102. return "\"" + errSubject + "\" cannot be found!";
  103. case BOSS_ERROR_CONDITION_EVAL_FAIL:
  104. return "Evaluation of conditional \"" + errSubject + "\" failed!";
  105. case BOSS_ERROR_REGEX_EVAL_FAIL:
  106. return "\"" + errSubject + "\" is not a valid regular expression. Item skipped.";
  107. case BOSS_ERROR_NO_GAME_DETECTED:
  108. return "No game detected!";
  109. case BOSS_ERROR_ENCODING_CONVERSION_FAIL:
  110. return "\"" + errSubject + "\" cannot be converted from UTF-8 to \"" + errString + "\".";
  111. case BOSS_ERROR_PLUGIN_BEFORE_MASTER:
  112. return "Master file \"" + errSubject + "\" loading after non-master plugins!";
  113. case BOSS_ERROR_FIND_ONLINE_MASTERLIST_REVISION_FAIL:
  114. return "Cannot find online masterlist revision number!";
  115. case BOSS_ERROR_FIND_ONLINE_MASTERLIST_DATE_FAIL:
  116. return "Cannot find online masterlist revision date!";
  117. case BOSS_ERROR_READ_UPDATE_FILE_LIST_FAIL:
  118. return "Cannot read list of files to be updated!";
  119. case BOSS_ERROR_FILE_CRC_MISMATCH:
  120. return "Downloaded file \"" + errSubject + "\" failed verification test!";
  121. case BOSS_ERROR_FS_FILE_MOD_TIME_READ_FAIL:
  122. return "The modification date of \"" + errSubject + "\" cannot be read! Filesystem response: \"" + errString + "\".";
  123. case BOSS_ERROR_FS_FILE_RENAME_FAIL:
  124. return "\"" + errSubject + "\" cannot be renamed! Filesystem response: \"" + errString + "\".";
  125. case BOSS_ERROR_FS_FILE_DELETE_FAIL:
  126. return "\"" + errSubject + "\" cannot be deleted! Filesystem response: \"" + errString + "\".";
  127. case BOSS_ERROR_FS_CREATE_DIRECTORY_FAIL:
  128. return "\"" + errSubject + "\" cannot be created! Filesystem response: \"" + errString + "\".";
  129. case BOSS_ERROR_FS_ITER_DIRECTORY_FAIL:
  130. return "\"" + errSubject + "\" cannot be scanned! Filesystem response: \"" + errString + "\".";
  131. case BOSS_ERROR_CURL_INIT_FAIL:
  132. return "cURL cannot be initialised!";
  133. case BOSS_ERROR_CURL_SET_ERRBUFF_FAIL:
  134. return "cURL's error buffer could not be set! cURL response: \"" + errString + "\".";
  135. case BOSS_ERROR_CURL_SET_OPTION_FAIL:
  136. return "A cURL option could not be set! cURL response: \"" + errString + "\".";
  137. case BOSS_ERROR_CURL_SET_PROXY_FAIL:
  138. return "Proxy hostname or port invalid! cURL response: \"" + errString + "\".";
  139. case BOSS_ERROR_CURL_SET_PROXY_TYPE_FAIL:
  140. return "Failed to set proxy type! cURL response: \"" + errString + "\".";
  141. case BOSS_ERROR_CURL_SET_PROXY_AUTH_FAIL:
  142. return "Proxy authentication username or password invalid! cURL response: \"" + errString + "\".";
  143. case BOSS_ERROR_CURL_SET_PROXY_AUTH_TYPE_FAIL:
  144. return "Failed to set proxy authentication type! cURL response: \"" + errString + "\".";
  145. case BOSS_ERROR_CURL_PERFORM_FAIL:
  146. return "cURL could not perform task! cURL response: \"" + errString + "\".";
  147. case BOSS_ERROR_CURL_USER_CANCEL:
  148. return "Cancelled by user.";
  149. case BOSS_ERROR_FILE_PARSE_FAIL:
  150. return "Parsing of \"" + errSubject + "\" failed!";
  151. case BOSS_ERROR_FS_FILE_MOD_TIME_WRITE_FAIL:
  152. return "The modification date of \"" + errSubject + "\" cannot be written! Filesystem response: \"" + errString + "\".";
  153. case BOSS_ERROR_GUI_WINDOW_INIT_FAIL:
  154. return "The window \"" + errSubject + "\" failed to initialise. Details: \"" + errString + "\".";
  155. default:
  156. return "No error.";
  157. }
  158. }
  159. //Returns the error code for the object.
  160. inline uint32_t getCode() { return errCode; }
  161. private:
  162. uint32_t errCode;
  163. string errString;
  164. string errSubject;
  165. };
  166. //Parsing error formats.
  167. static format MasterlistParsingErrorHeader("Masterlist Parsing Error: Expected a %1% at:");
  168. static format IniParsingErrorHeader("Ini Parsing Error: Expected a %1% at:");
  169. static format RuleListParsingErrorHeader("Userlist Parsing Error: Expected a %1% at:");
  170. static format RuleListSyntaxErrorMessage("Userlist Syntax Error: The rule beginning \"%1%: %2%\" %3%");
  171. static const string MasterlistParsingErrorFooter("Masterlist parsing aborted. Utility will end now.");
  172. static const string IniParsingErrorFooter("Ini parsing aborted. Some or all of the options may not have been set correctly.");
  173. static const string RuleListParsingErrorFooter("Userlist parsing aborted. No rules will be applied.");
  174. //RuleList syntax error strings.
  175. static const string ESortLineInForRule("includes a sort line in a rule with a FOR rule keyword.");
  176. static const string EAddingModGroup("tries to add a group.");
  177. static const string ESortingGroupEsms("tries to sort the group \"ESMs\".");
  178. static const string ESortingMasterEsm("tries to sort the master .ESM file.");
  179. static const string EReferencingModAndGroup("references a mod and a group.");
  180. static const string ESortingGroupBeforeEsms("tries to sort a group before the group \"ESMs\".");
  181. static const string ESortingModBeforeGameMaster("tries to sort a mod before the master .ESM file.");
  182. static const string EInsertingToTopOfEsms("tries to insert a mod into the top of the group \"ESMs\", before the master .ESM file.");
  183. static const string EInsertingGroupOrIntoMod("tries to insert a group or insert something into a mod.");
  184. static const string EAttachingMessageToGroup("tries to attach a message to a group.");
  185. static const string EMultipleSortLines("has more than one sort line.");
  186. static const string EMultipleReplaceLines("has more than one REPLACE-using message line.");
  187. static const string EReplaceNotFirst("has a REPLACE-using message line that is not the first message line.");
  188. static const string ESortNotSecond("has a sort line that is not the second line of the rule.");
  189. static const string ESortingToItself("tries to sort a mod or group relative to itself.");
  190. static const string EAttachingNonMessage("tries to attach an malformatted message.");
  191. static const string ESortingMasterAfterPlugin("tries to sort a plugin bfore a master file.");
  192. static const string ESortingPluginBeforeMaster("tries to sort a master file before a plugin.");
  193. //Parsing error class.
  194. class BOSS_COMMON ParsingError {
  195. public:
  196. inline ParsingError() : header(""), footer(""), detail(""), wholeMessage("") {}
  197. //For parsing errors.
  198. inline ParsingError(const string inHeader, const string inDetail, const string inFooter)
  199. : header(inHeader), detail(inDetail), footer(inFooter) {}
  200. //For userlist syntax errors.
  201. inline ParsingError(const string inWholeMessage)
  202. : wholeMessage(inWholeMessage) {}
  203. //Outputs correctly-formatted error message.
  204. string FormatFor(const uint32_t format);
  205. inline bool Empty() { return (header.empty() && footer.empty() && detail.empty() && wholeMessage.empty()); }
  206. private:
  207. string header;
  208. string footer;
  209. string detail;
  210. string wholeMessage;
  211. };
  212. }
  213. #endif