Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. "use strict";
  2.  
  3. var _ = require("lodash");
  4. var debug = require("debug")("paystand:models:ps-error");
  5.  
  6. module.exports = function (PsError) {
  7.  
  8. /**
  9. * Status Codes
  10. * @type {{}}
  11. */
  12. PsError.errorStatuses = {
  13. "success": "200",
  14. "noContent": "204",
  15. "badRequest": "400",
  16. "unauthorized": "401",
  17. "requestFailed": "402",
  18. "notFound": "404",
  19. "tooManyRequests": "429",
  20. "internalServerError": "500",
  21. "serviceUnavailable": "503"
  22. };
  23.  
  24. /**
  25. * Error types
  26. * @type {{}}
  27. */
  28. PsError.errorTypes = {
  29.  
  30. // public
  31. "apiConnectionError": "Failure to connect to the API",
  32. "apiError": "General API error",
  33. "authenticationError": "There was a problem with the authentication",
  34. "accessError": "There was a problem with the customer access",
  35. "requestError": "The request was invalid",
  36. "rateLimitError": "Too many requests",
  37.  
  38. // internal
  39. "statusError": "There was an error during status updates.",
  40. "dataError": "There was a problem with the data and better handling should be implemented.",
  41. "databaseError": "There was a problem handling the database record.",
  42. "dangerousError": "An unexpected error that likely result from bad code.",
  43. "providerError": "There was an error thrown by our provider."
  44.  
  45. };
  46.  
  47. PsError.publicCodes = {
  48. "apiError": {
  49. "status": "requestFailed",
  50. "type": "apiError",
  51. "description": "The request failed due to an API error"
  52. },
  53. //
  54. "invalidAuthenticationCredentials": {
  55. "status": "unauthorized",
  56. "type": "authenticationError",
  57. "description": "Credentials were not correct."
  58. },
  59. "insufficientResourceAccess": {
  60. "status": "unauthorized",
  61. "type": "accessError",
  62. "description": "You do not have access to the resource."
  63. },
  64. "insufficientFeatureAccess": {
  65. "status": "unauthorized",
  66. "type": "accessError",
  67. "description": "You do not have access to the feature."
  68. },
  69. "validationError": {
  70. "status": "badRequest",
  71. "type": "requestError",
  72. "description": "There are requirements that have not been met."
  73. },
  74. "resourceNotFound": {
  75. "status": "notFound",
  76. "type": "requestError",
  77. "description": "The resource was not found."
  78. },
  79. "paymentFailure": {
  80. "status": "requestFailed",
  81. "type": "requestError",
  82. "description": "There was a problem with the payment."
  83. },
  84. "cardFailure": {
  85. "status": "requestFailed",
  86. "type": "requestError",
  87. "description": "There was a problem with the card."
  88. }
  89. };
  90.  
  91. PsError.internalCodes = {
  92. "caughtError": {
  93. "status": "internalServerError",
  94. "type": "dangerousError",
  95. "description": "An error was caught unexpectedly."
  96. },
  97. "databaseError": {
  98. "status": "internalServerError",
  99. "type": "databaseError",
  100. "description": "An error occurred trying to save the record to the database."
  101. },
  102. "missingRequirements": {
  103. "status": "requestFailed",
  104. "type": "dataError",
  105. "description": "Requirements needed to complete the request are missing or were not found, possibly due to bad or unexpected data."
  106. },
  107. "invalidResourceData": {
  108. "status": "requestFailed",
  109. "type": "dataError",
  110. "description": "There was a problem with the data on the resource."
  111. },
  112. "invalidStatusTransition": {
  113. "status": "requestFailed",
  114. "type": "statusError",
  115. "description": "There was a problem during a status transition."
  116. },
  117. "providerValidationError": {
  118. "status": "badRequest",
  119. "type": "providerError",
  120. "description": "There are provider requirements that have not been met."
  121. },
  122. "providerConnectionError": {
  123. "status": "serviceUnavailable",
  124. "type": "providerError",
  125. "description": "There was a connection error with our provider."
  126. }
  127. };
  128.  
  129. /**
  130. * Error codes
  131. */
  132. PsError.errorCodes = _.assign({},
  133. PsError.publicCodes,
  134. PsError.internalCodes
  135. );
  136.  
  137. /**
  138. * Error object
  139. */
  140. PsError.paystandError = function () {
  141. };
  142. PsError.paystandError.prototype = Object.create(Error.prototype);
  143.  
  144. /**
  145. * New Error
  146. * @param code
  147. * @param explanation
  148. * @param parameter
  149. * @param value
  150. * @param ref
  151. * @param log
  152. */
  153. PsError.createError = function (errorData) {
  154. var error = new PsError.paystandError();
  155. var spec = PsError.errorCodes[errorData.code];
  156. if (!spec) {
  157. console.log("errorCode not found:", errorData);
  158. spec = PsError.errorCodes.apiError;
  159. }
  160. _.assign(error, {
  161. "name": spec.status,
  162. "status": PsError.errorStatuses[spec.status],
  163. "type": spec.type,
  164. "description": PsError.errorTypes[spec.type],
  165. "details": {
  166. "code": errorData.code,
  167. "description": spec.description
  168. },
  169. "ref": errorData.ref
  170. });
  171. if (errorData.parameter) {
  172. error.details.parameter = errorData.parameter;
  173. }
  174. if (errorData.value) {
  175. error.details.value = errorData.value;
  176. }
  177. if (errorData.explanation) {
  178. error.details.explanation = errorData.explanation;
  179. }
  180. if (errorData.log !== false) {
  181. console.log(errorData.log || errorData.explanation);
  182. }
  183. return error;
  184. };
  185.  
  186. /**
  187. * Pass or create
  188. * @param errorData
  189. * @returns {PsError.paystandError}
  190. */
  191. PsError.passOrCreate = function (errorData) {
  192. if (errorData.error instanceof PsError.paystandError) {
  193. return errorData.error;
  194. }
  195. return PsError.createError(errorData);
  196. };
  197.  
  198. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement