MAPIErrors.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. /**
  18. * MAPIErrors.cpp
  19. * Definition of GetMAPIErrorMessage()
  20. */
  21. #include <kopano/platform.h>
  22. #include <kopano/MAPIErrors.h>
  23. #include <mapidefs.h>
  24. #include <kopano/stringutil.h>
  25. #include <mapicode.h>
  26. #include <kopano/mapiext.h>
  27. #include <string>
  28. namespace KC {
  29. struct MAPIErrorTranslateRecord {
  30. HRESULT errorCode;
  31. const char* errorMessage;
  32. };
  33. static const MAPIErrorTranslateRecord MAPIErrorCodes[] = {
  34. { hrSuccess, "success" },
  35. { MAPI_E_CALL_FAILED, "call failed" },
  36. { MAPI_E_NOT_ENOUGH_MEMORY, "not enough memory" },
  37. { MAPI_E_INVALID_PARAMETER, "missing or invalid argument" },
  38. { MAPI_E_INTERFACE_NOT_SUPPORTED, "interface not supported" },
  39. { MAPI_E_NO_ACCESS, "no access" },
  40. { MAPI_E_NO_SUPPORT, "action not supported by server" },
  41. { MAPI_E_BAD_CHARWIDTH, "bad character width" },
  42. { MAPI_E_STRING_TOO_LONG, "string too long" },
  43. { MAPI_E_UNKNOWN_FLAGS, "unknown flags" },
  44. { MAPI_E_INVALID_ENTRYID, "invalid entry" },
  45. { MAPI_E_INVALID_OBJECT, "invalid object" },
  46. { MAPI_E_OBJECT_CHANGED, "object changed" },
  47. { MAPI_E_OBJECT_DELETED, "object deleted" },
  48. { MAPI_E_BUSY, "busy" },
  49. { MAPI_E_NOT_ENOUGH_DISK, "not enough disk" },
  50. { MAPI_E_NOT_ENOUGH_RESOURCES, "not enough resources" },
  51. { MAPI_E_NOT_FOUND, "not found" },
  52. { MAPI_E_VERSION, "version mismatch" },
  53. { MAPI_E_LOGON_FAILED, "logon failed" },
  54. { MAPI_E_SESSION_LIMIT, "session limit" },
  55. { MAPI_E_USER_CANCEL, "use cancel" },
  56. { MAPI_E_UNABLE_TO_ABORT, "unable to abort" },
  57. { MAPI_E_NETWORK_ERROR, "network error" },
  58. { MAPI_E_DISK_ERROR, "disk error" },
  59. { MAPI_E_TOO_COMPLEX, "too complex" },
  60. { MAPI_E_BAD_COLUMN, "bad column" },
  61. { MAPI_E_EXTENDED_ERROR, "extended error" },
  62. { MAPI_E_COMPUTED, "computed" },
  63. { MAPI_E_CORRUPT_DATA, "corrupt data" },
  64. { MAPI_E_UNCONFIGURED, "unconfigured" },
  65. { MAPI_E_FAILONEPROVIDER, "failoneprovider" },
  66. { MAPI_E_UNKNOWN_CPID, "unknown CPID" },
  67. { MAPI_E_UNKNOWN_LCID, "unknown LCID" },
  68. { MAPI_E_PASSWORD_CHANGE_REQUIRED, "password change required" },
  69. { MAPI_E_PASSWORD_EXPIRED, "password expired" },
  70. { MAPI_E_INVALID_WORKSTATION_ACCOUNT, "invalid workstation account" },
  71. { MAPI_E_INVALID_ACCESS_TIME, "invalid access time" },
  72. { MAPI_E_ACCOUNT_DISABLED, "account disabled" },
  73. { MAPI_E_END_OF_SESSION, "end of session" },
  74. { MAPI_E_UNKNOWN_ENTRYID, "unknown entryid" },
  75. { MAPI_E_MISSING_REQUIRED_COLUMN, "missing required column" },
  76. { MAPI_W_NO_SERVICE, "no service" },
  77. { MAPI_E_BAD_VALUE, "bad value" },
  78. { MAPI_E_INVALID_TYPE, "invalid type" },
  79. { MAPI_E_TYPE_NO_SUPPORT, "type no support" },
  80. { MAPI_E_UNEXPECTED_TYPE, "unexpected_type" },
  81. { MAPI_E_TOO_BIG, "too big" },
  82. { MAPI_E_DECLINE_COPY, "decline copy" },
  83. { MAPI_E_UNEXPECTED_ID, "unexpected id" },
  84. { MAPI_W_ERRORS_RETURNED, "errors returned" },
  85. { MAPI_E_UNABLE_TO_COMPLETE, "unable to complete" },
  86. { MAPI_E_TIMEOUT, "timeout" },
  87. { MAPI_E_TABLE_EMPTY, "table empty" },
  88. { MAPI_E_TABLE_TOO_BIG, "table too big" },
  89. { MAPI_E_INVALID_BOOKMARK, "invalid bookmark" },
  90. { MAPI_W_POSITION_CHANGED, "position changed" },
  91. { MAPI_W_APPROX_COUNT, "approx count" },
  92. { MAPI_E_WAIT, "wait" },
  93. { MAPI_E_CANCEL, "cancel" },
  94. { MAPI_E_NOT_ME, "not me" },
  95. { MAPI_W_CANCEL_MESSAGE, "cancel message" },
  96. { MAPI_E_CORRUPT_STORE, "corrupt store" },
  97. { MAPI_E_NOT_IN_QUEUE, "not in queue" },
  98. { MAPI_E_NO_SUPPRESS, "no suppress" },
  99. { MAPI_E_COLLISION, "collision" },
  100. { MAPI_E_NOT_INITIALIZED, "not initialized" },
  101. { MAPI_E_NON_STANDARD, "non standard" },
  102. { MAPI_E_NO_RECIPIENTS, "no recipients" },
  103. { MAPI_E_SUBMITTED, "submitted" },
  104. { MAPI_E_HAS_FOLDERS, "has folders" },
  105. { MAPI_E_HAS_MESSAGES, "has messages" },
  106. { MAPI_E_FOLDER_CYCLE, "folder cycle" },
  107. { MAPI_W_PARTIAL_COMPLETION, "partial completion" },
  108. { MAPI_E_AMBIGUOUS_RECIP, "ambiguous recipient" },
  109. { MAPI_E_STORE_FULL, "store full" },
  110. };
  111. /**
  112. * Get a string describing a MAPI error
  113. *
  114. * @param[in] errorCode MAPI error code
  115. * @retval C string describing error code, or NULL if not found
  116. */
  117. const char* GetMAPIErrorMessage(HRESULT errorCode)
  118. {
  119. const char* retval = "Unknown error code";
  120. for (size_t i = 0; i < ARRAY_SIZE(MAPIErrorCodes); ++i) {
  121. if (MAPIErrorCodes[i].errorCode == errorCode)
  122. {
  123. retval = MAPIErrorCodes[i].errorMessage;
  124. break;
  125. }
  126. }
  127. return retval;
  128. }
  129. /**
  130. * Prints a user friendly string for a given HRESULT value.
  131. *
  132. * We should try to be as informative as possible to the user, try to
  133. * get a nice descriptive message for the error and in the last case
  134. * just print the hex code.
  135. *
  136. * @param[in] hr A Mapierror code
  137. * @param[in] object Optional user object type name, default "object"
  138. * @return string A description for the given error
  139. */
  140. std::string getMapiCodeString(HRESULT hr, const char* object /* = "object" */)
  141. {
  142. std::string retval = GetMAPIErrorMessage(hr);
  143. std::string space(" ");
  144. std::string objectstring(object != nullptr ? object : "");
  145. switch (hr) {
  146. case MAPI_E_NOT_FOUND:
  147. return objectstring + space + retval;
  148. case MAPI_E_COLLISION:
  149. return objectstring + " already exists";
  150. case MAPI_E_NO_ACCESS:
  151. return retval + space + objectstring;
  152. case MAPI_E_UNABLE_TO_COMPLETE:
  153. return "please check your license";
  154. case MAPI_E_INVALID_TYPE:
  155. return "invalid type combination";
  156. default:
  157. return retval + std::string(" (") + stringify(hr, true) + std::string(")");
  158. };
  159. }
  160. } /* namespace */