PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/code/boss-common/branches/wrinklyninja-2.0/Common/Error.h

http://better-oblivion-sorting-software.googlecode.com/
C Header | 215 lines | 158 code | 17 blank | 40 comment | 3 complexity | 4f218a9426f27c1626fc1841dc29194a MD5 | raw file
Possible License(s): GPL-3.0
  1. /* Better Oblivion Sorting Software
  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 Better Oblivion Sorting Software.
  7. Better Oblivion Sorting Software 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. Better Oblivion Sorting Software 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 Better Oblivion Sorting Software. 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
  32. enum : uint32_t {
  33. BOSS_ERROR_OK,
  34. BOSS_ERROR_OBLIVION_AND_NEHRIM,
  35. BOSS_ERROR_NO_MASTER_FILE,
  36. BOSS_ERROR_FILE_READ_FAIL,
  37. BOSS_ERROR_FILE_WRITE_FAIL,
  38. BOSS_ERROR_FILE_NOT_UTF8,
  39. BOSS_ERROR_FILE_NOT_FOUND,
  40. BOSS_ERROR_FILE_PARSE_FAIL,
  41. BOSS_ERROR_CONDITION_EVAL_FAIL,
  42. BOSS_ERROR_NO_GAME_DETECTED,
  43. BOSS_ERROR_FIND_ONLINE_MASTERLIST_REVISION_FAIL,
  44. BOSS_ERROR_FIND_ONLINE_MASTERLIST_DATE_FAIL,
  45. BOSS_ERROR_READ_UPDATE_FILE_LIST_FAIL,
  46. BOSS_ERROR_FILE_CRC_MISMATCH,
  47. BOSS_ERROR_FS_FILE_MOD_TIME_READ_FAIL,
  48. BOSS_ERROR_FS_FILE_MOD_TIME_WRITE_FAIL,
  49. BOSS_ERROR_FS_FILE_RENAME_FAIL,
  50. BOSS_ERROR_FS_FILE_DELETE_FAIL,
  51. BOSS_ERROR_CURL_INIT_FAIL,
  52. BOSS_ERROR_CURL_SET_ERRBUFF_FAIL,
  53. BOSS_ERROR_CURL_SET_OPTION_FAIL,
  54. BOSS_ERROR_CURL_SET_PROXY_FAIL,
  55. BOSS_ERROR_CURL_SET_PROXY_TYPE_FAIL,
  56. BOSS_ERROR_CURL_SET_PROXY_AUTH_FAIL,
  57. BOSS_ERROR_CURL_SET_PROXY_AUTH_TYPE_FAIL,
  58. BOSS_ERROR_CURL_PERFORM_FAIL,
  59. BOSS_ERROR_CURL_USER_CANCEL,
  60. BOSS_ERROR_GUI_WINDOW_INIT_FAIL,
  61. BOSS_ERROR_MAX = BOSS_ERROR_GUI_WINDOW_INIT_FAIL
  62. };
  63. class boss_error {
  64. public:
  65. //For general errors not referencing specific files.
  66. inline boss_error(const uint32_t internalErrCode)
  67. : errCode(internalErrCode), errString(""), errSubject("") {}
  68. //For general errors referencing specific files.
  69. inline boss_error(const uint32_t internalErrCode, const string internalErrSubject)
  70. : errCode(internalErrCode), errString(""), errSubject(internalErrSubject) {}
  71. //For errors from BOOST Filesystem functions.
  72. inline boss_error(const uint32_t internalErrCode, const string internalErrSubject, const string externalErrString)
  73. : errCode(internalErrCode), errString(externalErrString), errSubject(internalErrSubject) {}
  74. //For errors from cURL functions.
  75. inline boss_error(const string externalErrString, const uint32_t internalErrCode)
  76. : errCode(internalErrCode), errString(externalErrString), errSubject("") {}
  77. //Returns the error string for the object.
  78. inline string getString() {
  79. switch(errCode) {
  80. case BOSS_ERROR_OK:
  81. return "No error.";
  82. case BOSS_ERROR_OBLIVION_AND_NEHRIM:
  83. return "Oblivion.esm and Nehrim.esm both detected!";
  84. case BOSS_ERROR_NO_MASTER_FILE:
  85. return "No game master .esm file found!";
  86. case BOSS_ERROR_FILE_READ_FAIL:
  87. return "\"" + errSubject + "\" cannot be read!";
  88. case BOSS_ERROR_FILE_WRITE_FAIL:
  89. return "\"" + errSubject + "\" cannot be written to!";
  90. case BOSS_ERROR_FILE_NOT_UTF8:
  91. return "\"" + errSubject + "\" is not encoded in valid UTF-8!";
  92. case BOSS_ERROR_FILE_NOT_FOUND:
  93. return "\"" + errSubject + "\" cannot be found!";
  94. case BOSS_ERROR_CONDITION_EVAL_FAIL:
  95. return "Evaluation of conditional \"" + errSubject + "\" failed!";
  96. case BOSS_ERROR_NO_GAME_DETECTED:
  97. return "No game detected!";
  98. case BOSS_ERROR_FIND_ONLINE_MASTERLIST_REVISION_FAIL:
  99. return "Cannot find online masterlist revision number!";
  100. case BOSS_ERROR_FIND_ONLINE_MASTERLIST_DATE_FAIL:
  101. return "Cannot find online masterlist revision date!";
  102. case BOSS_ERROR_READ_UPDATE_FILE_LIST_FAIL:
  103. return "Cannot read list of files to be updated!";
  104. case BOSS_ERROR_FILE_CRC_MISMATCH:
  105. return "Downloaded file \"" + errSubject + "\" failed verification test!";
  106. case BOSS_ERROR_FS_FILE_MOD_TIME_READ_FAIL:
  107. return "The modification date of \"" + errSubject + "\" cannot be read! Filesystem response: \"" + errString + "\".";
  108. case BOSS_ERROR_FS_FILE_RENAME_FAIL:
  109. return "\"" + errSubject + "\" cannot be renamed! Filesystem response: \"" + errString + "\".";
  110. case BOSS_ERROR_FS_FILE_DELETE_FAIL:
  111. return "\"" + errSubject + "\" cannot be deleted! Filesystem response: \"" + errString + "\".";
  112. case BOSS_ERROR_CURL_INIT_FAIL:
  113. return "cURL cannot be initialised!";
  114. case BOSS_ERROR_CURL_SET_ERRBUFF_FAIL:
  115. return "cURL's error buffer could not be set! cURL response: \"" + errString + "\".";
  116. case BOSS_ERROR_CURL_SET_OPTION_FAIL:
  117. return "A cURL option could not be set! cURL response: \"" + errString + "\".";
  118. case BOSS_ERROR_CURL_SET_PROXY_FAIL:
  119. return "Proxy hostname or port invalid! cURL response: \"" + errString + "\".";
  120. case BOSS_ERROR_CURL_SET_PROXY_TYPE_FAIL:
  121. return "Failed to set proxy type! cURL response: \"" + errString + "\".";
  122. case BOSS_ERROR_CURL_SET_PROXY_AUTH_FAIL:
  123. return "Proxy authentication username or password invalid! cURL response: \"" + errString + "\".";
  124. case BOSS_ERROR_CURL_SET_PROXY_AUTH_TYPE_FAIL:
  125. return "Failed to set proxy authentication type! cURL response: \"" + errString + "\".";
  126. case BOSS_ERROR_CURL_PERFORM_FAIL:
  127. return "cURL could not perform task! cURL response: \"" + errString + "\".";
  128. case BOSS_ERROR_CURL_USER_CANCEL:
  129. return "Cancelled by user.";
  130. case BOSS_ERROR_FILE_PARSE_FAIL:
  131. return "Parsing of \"" + errSubject + "\" failed!";
  132. case BOSS_ERROR_FS_FILE_MOD_TIME_WRITE_FAIL:
  133. return "The modification date of \"" + errSubject + "\" cannot be written! Filesystem response: \"" + errString + "\".";
  134. case BOSS_ERROR_GUI_WINDOW_INIT_FAIL:
  135. return "The window \"" + errSubject + "\" failed to initialise. Details: \"" + errString + "\".";
  136. default:
  137. return "No error.";
  138. }
  139. }
  140. //Returns the error code for the object.
  141. inline uint32_t getCode() { return errCode; }
  142. private:
  143. uint32_t errCode;
  144. string errString;
  145. string errSubject;
  146. };
  147. //Parsing error formats.
  148. static format MasterlistParsingErrorHeader("Masterlist Parsing Error: Expected a %1% at:");
  149. static format IniParsingErrorHeader("Ini Parsing Error: Expected a %1% at:");
  150. static format RuleListParsingErrorHeader("Userlist Parsing Error: Expected a %1% at:");
  151. static format RuleListSyntaxErrorMessage("Userlist Syntax Error: The rule beginning \"%1%: %2%\" %3%");
  152. static const string MasterlistParsingErrorFooter("Masterlist parsing aborted. Utility will end now.");
  153. static const string IniParsingErrorFooter("Ini parsing aborted. Some or all of the options may not have been set correctly.");
  154. static const string RuleListParsingErrorFooter("Userlist parsing aborted. No rules will be applied.");
  155. //RuleList syntax error strings.
  156. static const string ESortLineInForRule("includes a sort line in a rule with a FOR rule keyword.");
  157. static const string EAddingModGroup("tries to add a group.");
  158. static const string ESortingGroupEsms("tries to sort the group \"ESMs\".");
  159. static const string ESortingMasterEsm("tries to sort the master .ESM file.");
  160. static const string EReferencingModAndGroup("references a mod and a group.");
  161. static const string ESortingGroupBeforeEsms("tries to sort a group before the group \"ESMs\".");
  162. static const string ESortingModBeforeGameMaster("tries to sort a mod before the master .ESM file.");
  163. static const string EInsertingToTopOfEsms("tries to insert a mod into the top of the group \"ESMs\", before the master .ESM file.");
  164. static const string EInsertingGroupOrIntoMod("tries to insert a group or insert something into a mod.");
  165. static const string EAttachingMessageToGroup("tries to attach a message to a group.");
  166. static const string EMultipleSortLines("has more than one sort line.");
  167. static const string EMultipleReplaceLines("has more than one REPLACE-using message line.");
  168. static const string EReplaceNotFirst("has a REPLACE-using message line that is not the first message line.");
  169. static const string ESortNotSecond("has a sort line that is not the second line of the rule.");
  170. static const string ESortingToItself("tries to sort a mod or group relative to itself.");
  171. //Parsing error class.
  172. class ParsingError {
  173. public:
  174. inline ParsingError() : header(""), footer(""), detail(""), wholeMessage("") {}
  175. //For parsing errors.
  176. inline ParsingError(const string inHeader, const string inDetail, const string inFooter)
  177. : header(inHeader), detail(inDetail), footer(inFooter) {}
  178. //For userlist syntax errors.
  179. inline ParsingError(const string inWholeMessage)
  180. : wholeMessage(inWholeMessage) {}
  181. //Outputs correctly-formatted error message.
  182. string FormatFor(const uint32_t format);
  183. inline bool Empty() { return (header.empty() && footer.empty() && detail.empty() && wholeMessage.empty()); }
  184. private:
  185. string header;
  186. string footer;
  187. string detail;
  188. string wholeMessage;
  189. };
  190. }
  191. #endif