diff --git a/src/v0/destinations/user/transform.js b/src/v0/destinations/user/transform.js index 766746a97b5..8d186305ba0 100644 --- a/src/v0/destinations/user/transform.js +++ b/src/v0/destinations/user/transform.js @@ -1,5 +1,4 @@ const { - CustomError, getErrorRespEvents, getSuccessRespEvents, defaultRequestConfig, @@ -19,6 +18,11 @@ const { createOrUpdateUserPayloadBuilder, createEventOccurrencePayloadBuilder } = require("./utils"); +const { + TransformationError, + InstrumentationError, + NetworkInstrumentationError +} = require("../../util/errorTypes"); const { EventType } = require("../../../constants"); @@ -36,7 +40,9 @@ const responseBuilder = async (payload, endpoint, method, apiKey) => { return response; } // fail-safety for developer error - throw new CustomError("[ User.com ]:: Payload could not be constructed", 400); + throw new TransformationError( + "Something went wrong while constructing the payload" + ); }; const identifyResponseBuilder = async (message, destination) => { @@ -57,7 +63,7 @@ const identifyResponseBuilder = async (message, destination) => { const trackResponseBuilder = async (message, destination) => { if (!message.event) { - throw new CustomError("[ User.com ]:: parameter event is required", 400); + throw new InstrumentationError("Parameter event is required"); } let payload; @@ -76,9 +82,8 @@ const trackResponseBuilder = async (message, destination) => { return responseBuilder(payload, endpoint, method, apiKey); } - throw new CustomError( - "[ User.com ]:: No user found with given lookup field, Track event cannot be completed if there is no valid user", - 400 + throw new NetworkInstrumentationError( + "No user found with given lookup field, Track event cannot be completed if there is no valid user" ); }; @@ -98,9 +103,8 @@ const pageResponseBuilder = async (message, destination) => { endpoint = prepareUrl(endpoint, appSubdomain); return responseBuilder(payload, endpoint, method, apiKey); } - throw new CustomError( - "[ User.com ]:: No user found with given lookup field. Page event cannot be completed if there is no valid user", - 400 + throw new NetworkInstrumentationError( + "No user found with given lookup field. Page event cannot be completed if there is no valid user" ); }; @@ -135,16 +139,13 @@ const groupResponseBuilder = async (message, destination) => { ); return responseBuilder(payload, endpoint, method, apiKey); } - throw new CustomError("[ User.com ] :: No user found with given userId", 400); + throw new NetworkInstrumentationError("No user found with given userId"); }; const processEvent = async (message, destination) => { // Validating if message type is even given or not if (!message.type) { - throw new CustomError( - "[ User.com ]:: Message Type is not present. Aborting message.", - 400 - ); + throw new InstrumentationError("Event type is required"); } const messageType = message.type.toLowerCase(); let response; @@ -162,9 +163,8 @@ const processEvent = async (message, destination) => { response = await pageResponseBuilder(message, destination); break; default: - throw new CustomError( - `[ User.com ]:: Message type ${messageType} not supported.`, - 400 + throw new InstrumentationError( + `Event type ${messageType} is not supported` ); } return response; diff --git a/src/v0/destinations/user/utils.js b/src/v0/destinations/user/utils.js index 7ae5688f292..7e15fe36242 100644 --- a/src/v0/destinations/user/utils.js +++ b/src/v0/destinations/user/utils.js @@ -1,6 +1,5 @@ const get = require("get-value"); const { - CustomError, getHashFromArray, constructPayload, getIntegrationsObj, @@ -22,6 +21,10 @@ const { groupSourceKeys, identifySourceKeys } = require("./config"); +const { + InstrumentationError, + NetworkInstrumentationError +} = require("../../util/errorTypes"); /** * Returns updated User.com url @@ -205,16 +208,13 @@ const validatePagePayload = message => { const timestamp = getFieldValueFromMessage(message, "timestamp"); if (!pageUrl) { - throw new CustomError("[ User.com ]:: Parameter url is required", 400); + throw new InstrumentationError("Parameter url is required"); } if (!pagePath) { - throw new CustomError("[ User.com ]:: Parameter path is required", 400); + throw new InstrumentationError("Parameter path is required"); } if (!timestamp) { - throw new CustomError( - "[ User.com ]:: Parameter timestamp is required", - 400 - ); + throw new InstrumentationError("Parameter timestamp is required"); } }; @@ -228,7 +228,7 @@ const validateGroupPayload = message => { const traits = getFieldValueFromMessage(message, "traits"); const { name } = traits; if (!name) { - throw new CustomError("[ User.com ]:: Parameter name is required", 400); + throw new InstrumentationError("Parameter name is required"); } }; @@ -346,10 +346,7 @@ const getUserByUserKey = async (apiKey, userKey, appSubdomain) => { */ const getUserByEmail = async (apiKey, email, appSubdomain) => { if (!email) { - throw new CustomError( - "[ User.com ]:: lookup field : email value is not present", - 400 - ); + throw new InstrumentationError("Lookup field : email value is not present"); } const endpoint = prepareUrl( @@ -384,10 +381,7 @@ const getUserByEmail = async (apiKey, email, appSubdomain) => { */ const getUserByPhoneNumber = async (apiKey, phoneNumber, appSubdomain) => { if (!phoneNumber) { - throw new CustomError( - "[ User.com ] :: lookup field : phone value is not present", - 400 - ); + throw new InstrumentationError("Lookup field : phone value is not present"); } const endpoint = prepareUrl( @@ -409,14 +403,12 @@ const getUserByPhoneNumber = async (apiKey, phoneNumber, appSubdomain) => { const { response } = processedUserResponse; const { results } = response; if (results.length === 0) { - throw new CustomError( - "[ User.com ] :: no user found for a given lookup field", - 400 + throw new NetworkInstrumentationError( + "No user found for a given lookup field" ); } else if (results.length > 1) { - throw new CustomError( - "[ User.com ] :: multiple users obtained for a given lookup field", - 400 + throw new NetworkInstrumentationError( + "Multiple users obtained for a given lookup field" ); } else { const [first] = results; @@ -523,9 +515,9 @@ const retrieveUserFromLookup = async (message, destination) => { if (lookupField === "phone") { return getUserByPhoneNumber(apiKey, lookupFieldValue, appSubdomain); } - throw new CustomError( - `[ User.com ] :: lookup field : ${lookupField} is not supported for this destination`, - 400 + + throw new InstrumentationError( + `Lookup field : ${lookupField} is not supported for this destination` ); } else { const userId = getValueFromMessage(message, "userId"); @@ -536,9 +528,9 @@ const retrieveUserFromLookup = async (message, destination) => { if (isDefinedAndNotNullAndNotEmpty(email)) { return getUserByEmail(apiKey, email, appSubdomain); } - throw new CustomError( - "[ User.com ] :: default lookup field : email value is empty", - 400 + + throw new InstrumentationError( + "Default lookup field : email value is empty" ); } }; diff --git a/test/__tests__/data/user.json b/test/__tests__/data/user.json index 3fa08d600a8..5f3ed2a04af 100644 --- a/test/__tests__/data/user.json +++ b/test/__tests__/data/user.json @@ -36,7 +36,7 @@ } }, "output": { - "error": "[ User.com ]:: Message Type is not present. Aborting message." + "error": "Event type is required" } }, { @@ -77,7 +77,7 @@ } }, "output": { - "error": "[ User.com ]:: Message type trackuser not supported." + "error": "Event type trackuser is not supported" } }, { @@ -108,7 +108,7 @@ } }, "output": { - "error": "[ User.com ]:: parameter event is required" + "error": "Parameter event is required" } }, { @@ -150,7 +150,7 @@ } }, "output": { - "error": "[ User.com ]:: Parameter name is required" + "error": "Parameter name is required" } }, {