Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- var _ = require("lodash");
- var debug = require("debug")("paystand:models:ps-error");
- module.exports = function (PsError) {
- /**
- * Status Codes
- * @type {{}}
- */
- PsError.errorStatuses = {
- "success": "200",
- "noContent": "204",
- "badRequest": "400",
- "unauthorized": "401",
- "requestFailed": "402",
- "notFound": "404",
- "tooManyRequests": "429",
- "internalServerError": "500",
- "serviceUnavailable": "503"
- };
- /**
- * Error types
- * @type {{}}
- */
- PsError.errorTypes = {
- // public
- "apiConnectionError": "Failure to connect to the API",
- "apiError": "General API error",
- "authenticationError": "There was a problem with the authentication",
- "accessError": "There was a problem with the customer access",
- "requestError": "The request was invalid",
- "rateLimitError": "Too many requests",
- // internal
- "statusError": "There was an error during status updates.",
- "dataError": "There was a problem with the data and better handling should be implemented.",
- "databaseError": "There was a problem handling the database record",
- "dangerousError": "An unexpected error that likely result from bad code.",
- "providerError": "There was an error thrown by our provider"
- };
- PsError.publicCodes = {
- "apiError": {
- "status": "requestFailed",
- "type": "apiError",
- "description": "The request failed due to an API error"
- },
- //
- "invalidAuthenticationCredentials": {
- "status": "unauthorized",
- "type": "authenticationError",
- "description": "Credentials were not correct."
- },
- "insufficientResourceAccess": {
- "status": "unauthorized",
- "type": "accessError",
- "description": "You do not have access to the resource."
- },
- "insufficientFeatureAccess": {
- "status": "unauthorized",
- "type": "accessError",
- "description": "You do not have access to the feature."
- },
- "validationError": {
- "status": "badRequest",
- "type": "requestError",
- "description": "There are requirements that have not been met."
- },
- "resourceNotFound": {
- "status": "notFound",
- "type": "requestError",
- "description": "The resource was not found."
- },
- "paymentFailure": {
- "status": "requestFailed",
- "type": "requestError",
- "description": "There was a problem with the payment."
- },
- "cardFailure": {
- "status": "requestFailed",
- "type": "requestError",
- "description": "There was a problem with the card."
- }
- };
- PsError.internalCodes = {
- "caughtError": {
- "status": "internalServerError",
- "type": "dangerousError",
- "description": "An error was caught unexpectedly."
- },
- "databaseError": {
- "status": "internalServerError",
- "type": "databaseError",
- "description": "An error occurred trying to save the record to the database"
- },
- "missingRequirements": {
- "status": "requestFailed",
- "type": "dataError",
- "description": "Requirements needed to complete the request are missing or were not found, possibly due to bad or unexpected data"
- },
- "invalidResourceData": {
- "status": "requestFailed",
- "type": "dataError",
- "description": "There was a problem with the data on the resource."
- },
- "invalidStatusTransition": {
- "status": "requestFailed",
- "type": "statusError",
- "description": "There was a problem during a status transition."
- },
- "providerValidationError": {
- "status": "badRequest",
- "type": "providerError",
- "description": "There are provider requirements that have not been met."
- },
- "providerConnectionError": {
- "status": "serviceUnavailable",
- "type": "providerError",
- "description": "There was a connection error with our provider"
- }
- };
- /**
- * Error codes
- */
- PsError.errorCodes = _.assign({},
- PsError.publicCodes,
- PsError.internalCodes
- );
- /**
- * Error object
- */
- PsError.paystandError = function () {
- };
- PsError.paystandError.prototype = Object.create(Error.prototype);
- /**
- * New Error
- * @param code
- * @param explanation
- * @param parameter
- * @param value
- * @param ref
- * @param log
- */
- PsError.createError = function (errorData) {
- var error = new PsError.paystandError();
- var spec = PsError.errorCodes[errorData.code];
- if (!spec) {
- console.log("errorCode not found:", errorData);
- spec = PsError.errorCodes.apiError;
- }
- _.assign(error, {
- "name": spec.status,
- "status": PsError.errorStatuses[spec.status],
- "type": spec.type,
- "description": PsError.errorTypes[spec.type],
- "details": {
- "code": errorData.code,
- "description": spec.description
- },
- "ref": errorData.ref
- });
- if (errorData.parameter) {
- error.details.parameter = errorData.parameter;
- }
- if (errorData.value) {
- error.details.value = errorData.value;
- }
- if (errorData.explanation) {
- error.details.explanation = errorData.explanation;
- }
- if (errorData.log !== false) {
- console.log(errorData.log || errorData.explanation);
- }
- return error;
- };
- /**
- * Pass or create
- * @param errorData
- * @returns {PsError.paystandError}
- */
- PsError.passOrCreate = function (errorData) {
- if (errorData.error instanceof PsError.paystandError) {
- return errorData.error;
- }
- return PsError.createError(errorData);
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement