http_errors.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. This Source Code Form is subject to the terms of the Mozilla Public
  3. License, v. 2.0. If a copy of the MPL was not distributed with this
  4. file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package logicmonclient
  7. import (
  8. "errors"
  9. "fmt"
  10. "net/http"
  11. )
  12. var ErrContinue = errors.New(http.StatusText(http.StatusContinue))
  13. var ErrSwitchingProtocols = errors.New(http.StatusText(http.StatusSwitchingProtocols))
  14. var ErrProcessing = errors.New(http.StatusText(http.StatusProcessing))
  15. var ErrEarlyHints = errors.New(http.StatusText(http.StatusEarlyHints))
  16. var ErrMultipleChoices = errors.New(http.StatusText(http.StatusMultipleChoices))
  17. var ErrMovedPermanently = errors.New(http.StatusText(http.StatusMovedPermanently))
  18. var ErrFound = errors.New(http.StatusText(http.StatusFound))
  19. var ErrSeeOther = errors.New(http.StatusText(http.StatusSeeOther))
  20. var ErrNotModified = errors.New(http.StatusText(http.StatusNotModified))
  21. var ErrUseProxy = errors.New(http.StatusText(http.StatusUseProxy))
  22. var ErrTemporaryRedirect = errors.New(http.StatusText(http.StatusTemporaryRedirect))
  23. var ErrPermanentRedirect = errors.New(http.StatusText(http.StatusPermanentRedirect))
  24. var ErrBadRequest = errors.New(http.StatusText(http.StatusBadRequest))
  25. var ErrUnauthorized = errors.New(http.StatusText(http.StatusUnauthorized))
  26. var ErrPaymentRequired = errors.New(http.StatusText(http.StatusPaymentRequired))
  27. var ErrForbidden = errors.New(http.StatusText(http.StatusForbidden))
  28. var ErrNotFound = errors.New(http.StatusText(http.StatusNotFound))
  29. var ErrMethodNotAllowed = errors.New(http.StatusText(http.StatusMethodNotAllowed))
  30. var ErrNotAcceptable = errors.New(http.StatusText(http.StatusNotAcceptable))
  31. var ErrProxyAuthRequired = errors.New(http.StatusText(http.StatusProxyAuthRequired))
  32. var ErrRequestTimeout = errors.New(http.StatusText(http.StatusRequestTimeout))
  33. var ErrConflict = errors.New(http.StatusText(http.StatusConflict))
  34. var ErrGone = errors.New(http.StatusText(http.StatusGone))
  35. var ErrLengthRequired = errors.New(http.StatusText(http.StatusLengthRequired))
  36. var ErrPreconditionFailed = errors.New(http.StatusText(http.StatusPreconditionFailed))
  37. var ErrRequestEntityTooLarge = errors.New(http.StatusText(http.StatusRequestEntityTooLarge))
  38. var ErrRequestURITooLong = errors.New(http.StatusText(http.StatusRequestURITooLong))
  39. var ErrUnsupportedMediaType = errors.New(http.StatusText(http.StatusUnsupportedMediaType))
  40. var ErrRequestedRangeNotSatisfiable = errors.New(http.StatusText(http.StatusRequestedRangeNotSatisfiable))
  41. var ErrExpectationFailed = errors.New(http.StatusText(http.StatusExpectationFailed))
  42. var ErrTeapot = errors.New(http.StatusText(http.StatusTeapot))
  43. var ErrMisdirectedRequest = errors.New(http.StatusText(http.StatusMisdirectedRequest))
  44. var ErrUnprocessableEntity = errors.New(http.StatusText(http.StatusUnprocessableEntity))
  45. var ErrLocked = errors.New(http.StatusText(http.StatusLocked))
  46. var ErrFailedDependency = errors.New(http.StatusText(http.StatusFailedDependency))
  47. var ErrTooEarly = errors.New(http.StatusText(http.StatusTooEarly))
  48. var ErrUpgradeRequired = errors.New(http.StatusText(http.StatusUpgradeRequired))
  49. var ErrPreconditionRequired = errors.New(http.StatusText(http.StatusPreconditionRequired))
  50. var ErrTooManyRequests = errors.New(http.StatusText(http.StatusTooManyRequests))
  51. var ErrRequestHeaderFieldsTooLarge = errors.New(http.StatusText(http.StatusRequestHeaderFieldsTooLarge))
  52. var ErrUnavailableForLegalReasons = errors.New(http.StatusText(http.StatusUnavailableForLegalReasons))
  53. var ErrInternalServerError = errors.New(http.StatusText(http.StatusInternalServerError))
  54. var ErrNotImplemented = errors.New(http.StatusText(http.StatusNotImplemented))
  55. var ErrBadGateway = errors.New(http.StatusText(http.StatusBadGateway))
  56. var ErrServiceUnavailable = errors.New(http.StatusText(http.StatusServiceUnavailable))
  57. var ErrGatewayTimeout = errors.New(http.StatusText(http.StatusGatewayTimeout))
  58. var ErrHTTPVersionNotSupported = errors.New(http.StatusText(http.StatusHTTPVersionNotSupported))
  59. var ErrVariantAlsoNegotiates = errors.New(http.StatusText(http.StatusVariantAlsoNegotiates))
  60. var ErrInsufficientStorage = errors.New(http.StatusText(http.StatusInsufficientStorage))
  61. var ErrLoopDetected = errors.New(http.StatusText(http.StatusLoopDetected))
  62. var ErrNotExtended = errors.New(http.StatusText(http.StatusNotExtended))
  63. var ErrNetworkAuthenticationRequired = errors.New(http.StatusText(http.StatusNetworkAuthenticationRequired))
  64. var httpStatusToError = map[int]error{
  65. 100: ErrContinue,
  66. 101: ErrSwitchingProtocols,
  67. 102: ErrProcessing,
  68. 103: ErrEarlyHints,
  69. 300: ErrMultipleChoices,
  70. 301: ErrMovedPermanently,
  71. 302: ErrFound,
  72. 303: ErrSeeOther,
  73. 304: ErrNotModified,
  74. 305: ErrUseProxy,
  75. 307: ErrTemporaryRedirect,
  76. 308: ErrPermanentRedirect,
  77. 400: ErrBadRequest,
  78. 401: ErrUnauthorized,
  79. 402: ErrPaymentRequired,
  80. 403: ErrForbidden,
  81. 404: ErrNotFound,
  82. 405: ErrMethodNotAllowed,
  83. 406: ErrNotAcceptable,
  84. 407: ErrProxyAuthRequired,
  85. 408: ErrRequestTimeout,
  86. 409: ErrConflict,
  87. 410: ErrGone,
  88. 411: ErrLengthRequired,
  89. 412: ErrPreconditionFailed,
  90. 413: ErrRequestEntityTooLarge,
  91. 414: ErrRequestURITooLong,
  92. 415: ErrUnsupportedMediaType,
  93. 416: ErrRequestedRangeNotSatisfiable,
  94. 417: ErrExpectationFailed,
  95. 418: ErrTeapot,
  96. 421: ErrMisdirectedRequest,
  97. 422: ErrUnprocessableEntity,
  98. 423: ErrLocked,
  99. 424: ErrFailedDependency,
  100. 425: ErrTooEarly,
  101. 426: ErrUpgradeRequired,
  102. 428: ErrPreconditionRequired,
  103. 429: ErrTooManyRequests,
  104. 431: ErrRequestHeaderFieldsTooLarge,
  105. 451: ErrUnavailableForLegalReasons,
  106. 500: ErrInternalServerError,
  107. 501: ErrNotImplemented,
  108. 502: ErrBadGateway,
  109. 503: ErrServiceUnavailable,
  110. 504: ErrGatewayTimeout,
  111. 505: ErrHTTPVersionNotSupported,
  112. 506: ErrVariantAlsoNegotiates,
  113. 507: ErrInsufficientStorage,
  114. 508: ErrLoopDetected,
  115. 510: ErrNotExtended,
  116. 511: ErrNetworkAuthenticationRequired,
  117. }
  118. type LMError struct {
  119. ErrorCode int32 `json:"errorCode"`
  120. ErrorMessage string `json:"errorMessage"`
  121. ErrorDetail string `json:"errorDetail"`
  122. }
  123. func (e *LMError) Error() string {
  124. return fmt.Sprintf(
  125. "logicmonitor error %d: %s (%s)",
  126. e.ErrorCode,
  127. e.ErrorMessage,
  128. e.ErrorDetail,
  129. )
  130. }
  131. // Taken from:
  132. // * https://www.logicmonitor.com/support/api-error-codes
  133. // * https://www.logicmonitor.com/support/rest-api-status-codes
  134. var ErrLMRecordExists = errors.New("The record already exists (600)")
  135. var ErrLMServerBusy = errors.New("Server is busy")
  136. var ErrLMInternal = errors.New("Internal error (1001)")
  137. var ErrLMNoSuchDeviceGroup = errors.New("No such DeviceGroup")
  138. var ErrLMBadRequest = errors.New("Bad request")
  139. var ErrLMDeviceGroupNotDeleted = errors.New("Device Group was not deleted")
  140. var ErrLMNoSuchDeviceDataSource = errors.New("No such Device DataSource")
  141. var ErrLMInstanceGroupNotCreated = errors.New("Instance group cannot be created")
  142. var ErrLMNoSuchInstanceGroup = errors.New("No such InstanceGroup")
  143. var ErrLMNoSuchDataSource = errors.New("No such DataSource")
  144. var ErrLMNoSuchInstance = errors.New("No such Instance")
  145. var ErrLMAppliesTo = errors.New("There is a syntax error in the appliesTo field.")
  146. var ErrLMNoSuchCollector = errors.New("No such Collector")
  147. var ErrLMNoSuchDatapoint = errors.New("No such Datapoint")
  148. var ErrLMCannotImportLogicModule = errors.New("Cannot import LogicModule")
  149. var ErrLMPermissionDenied = errors.New("Permission denied")
  150. var ErrLMNoSuchSDT = errors.New("No such SDT")
  151. var ErrLMNoSuchWidget = errors.New("No such widget")
  152. var ErrLMNoSuchCompany = errors.New("No such company (1065)")
  153. var ErrLMNoSuchRecord = errors.New("No such record (1069)")
  154. var ErrLMCollectorInactive = errors.New("The collector isn’t active")
  155. var ErrLMReportTooLarge = errors.New("Report too large")
  156. var ErrLMRequestEntityTooLarge = errors.New("The request entity/report is too large (1075)")
  157. var ErrLMTemplateTooLarge = errors.New("Template too large")
  158. var ErrLMInvalidMacros = errors.New("Invalid Macros / Bad request")
  159. var ErrLMUploadDocxFile = errors.New("Please upload a .docx file")
  160. var ErrLMReportTemplateNotUploaded = errors.New("The report template could not be uploaded")
  161. var ErrLMRateExceed = errors.New("Rate exceed")
  162. var ErrLMTooManyRequests = errors.New("Too many requests")
  163. var ErrLMQueryTimeout = errors.New("Query timed out")
  164. var ErrLMResourceDependency = errors.New("Resource Dependency (1104)")
  165. var ErrLMPartialSuccess = errors.New("Partial success / The update was partially successful (1201)")
  166. var ErrLMTaskRunning = errors.New("The task is running")
  167. var ErrLMNoContent = errors.New("No Content")
  168. var ErrLMSaveFailed = errors.New("Save failed")
  169. var ErrLMNoValidLocations = errors.New("All Locations are disable / No valid locations selected")
  170. var ErrLMReportInProgress = errors.New("Report in progress")
  171. var ErrLMBadRequest2 = errors.New("Bad request")
  172. var ErrLMAuthenticationFailed = errors.New("Authentication failed")
  173. var ErrLM2FARequired = errors.New("Two-factor authentication is required.")
  174. var ErrLMNoSuchRecord2 = errors.New("No such record (1404)")
  175. var ErrLMRecordExists2 = errors.New("The record already exists (1409")
  176. var ErrLMFalsePrecondition = errors.New("The supplied precondition evaluated to false")
  177. var ErrLMRequestEntityTooLarge2 = errors.New("The request entity is too large (1413)")
  178. var ErrLMInternal2 = errors.New("Internal error (1500)")
  179. var ErrLM2FANotEnabled = errors.New("User must enable two-factor authentication")
  180. var ErrLMIncorrectPassword = errors.New("Incorrect password")
  181. var ErrLMResourceDependency2 = errors.New("Resource Dependency (14001)")
  182. var ErrLMPartialSuccess2 = errors.New("Partial success (14002)")
  183. var ErrLMInvalidProperty = errors.New("Properties must start with a letter to be valid.")
  184. var ErrLMQueryStringParams = errors.New("Error in query string parameters")
  185. var ErrLMNoSuchCompany2 = errors.New("No Such Company (14042)")
  186. var lmStatusToError = map[int]error{
  187. 600: ErrLMRecordExists,
  188. 1000: ErrLMServerBusy,
  189. 1001: ErrLMInternal,
  190. 1004: ErrLMNoSuchDeviceGroup,
  191. 1007: ErrLMBadRequest,
  192. 1010: ErrLMDeviceGroupNotDeleted,
  193. 1013: ErrLMNoSuchDeviceDataSource,
  194. 1014: ErrLMInstanceGroupNotCreated,
  195. 1015: ErrLMNoSuchInstanceGroup,
  196. 1022: ErrLMNoSuchDataSource,
  197. 1027: ErrLMNoSuchInstance,
  198. 1031: ErrLMAppliesTo,
  199. 1033: ErrLMNoSuchCollector,
  200. 1037: ErrLMNoSuchDatapoint,
  201. 1040: ErrLMCannotImportLogicModule,
  202. 1041: ErrLMPermissionDenied,
  203. 1048: ErrLMNoSuchSDT,
  204. 1058: ErrLMNoSuchWidget,
  205. 1065: ErrLMNoSuchCompany,
  206. 1069: ErrLMNoSuchRecord,
  207. 1073: ErrLMCollectorInactive,
  208. 1074: ErrLMReportTooLarge,
  209. 1075: ErrLMRequestEntityTooLarge,
  210. 1076: ErrLMTemplateTooLarge,
  211. 1077: ErrLMInvalidMacros,
  212. 1078: ErrLMUploadDocxFile,
  213. 1079: ErrLMReportTemplateNotUploaded,
  214. 1091: ErrLMRateExceed,
  215. 1100: ErrLMTooManyRequests,
  216. 1101: ErrLMQueryTimeout,
  217. 1104: ErrLMResourceDependency,
  218. 1201: ErrLMPartialSuccess,
  219. 1202: ErrLMTaskRunning,
  220. 1204: ErrLMNoContent,
  221. 1301: ErrLMSaveFailed,
  222. 1303: ErrLMNoValidLocations,
  223. 1313: ErrLMReportInProgress,
  224. 1400: ErrLMBadRequest2,
  225. 1401: ErrLMAuthenticationFailed,
  226. 1403: ErrLM2FARequired,
  227. 1404: ErrLMNoSuchRecord2,
  228. 1409: ErrLMRecordExists2,
  229. 1412: ErrLMFalsePrecondition,
  230. 1413: ErrLMRequestEntityTooLarge2,
  231. 1500: ErrLMInternal2,
  232. 2403: ErrLM2FANotEnabled,
  233. 3403: ErrLMIncorrectPassword,
  234. 14001: ErrLMResourceDependency2,
  235. 14002: ErrLMPartialSuccess2,
  236. 14003: ErrLMInvalidProperty,
  237. 14004: ErrLMQueryStringParams,
  238. 14042: ErrLMNoSuchCompany2,
  239. }