Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new error types for user.com destination #1638

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/v0/destinations/user/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {
CustomError,
getErrorRespEvents,
getSuccessRespEvents,
defaultRequestConfig,
Expand All @@ -19,6 +18,11 @@ const {
createOrUpdateUserPayloadBuilder,
createEventOccurrencePayloadBuilder
} = require("./utils");
const {
TransformationError,
InstrumentationError,
NetworkInstrumentationError
} = require("../../util/errorTypes");

const { EventType } = require("../../../constants");

Expand All @@ -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) => {
Expand All @@ -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("[User.com]:: parameter event is required");
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
}

let payload;
Expand All @@ -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(
"[User.com] :: No user found with given lookup field, Track event cannot be completed if there is no valid user"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try not put [User.com] :: these in error messages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

);
};

Expand All @@ -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(
"[User.com] :: No user found with given lookup field. Page event cannot be completed if there is no valid user"
);
};

Expand Down Expand Up @@ -135,16 +139,15 @@ 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 InstrumentationError(
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
"[User.com] :: 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;
Expand All @@ -162,9 +165,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;
Expand Down
48 changes: 23 additions & 25 deletions src/v0/destinations/user/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const get = require("get-value");
const {
CustomError,
getHashFromArray,
constructPayload,
getIntegrationsObj,
Expand All @@ -22,6 +21,10 @@ const {
groupSourceKeys,
identifySourceKeys
} = require("./config");
const {
InstrumentationError,
NetworkInstrumentationError
} = require("../../util/errorTypes");

/**
* Returns updated User.com url
Expand Down Expand Up @@ -205,15 +208,14 @@ const validatePagePayload = message => {
const timestamp = getFieldValueFromMessage(message, "timestamp");

if (!pageUrl) {
throw new CustomError("[ User.com ]:: Parameter url is required", 400);
throw new InstrumentationError("[User.com]:: Parameter url is required");
}
if (!pagePath) {
throw new CustomError("[ User.com ]:: Parameter path is required", 400);
throw new InstrumentationError("[User.com]:: Parameter path is required");
}
if (!timestamp) {
throw new CustomError(
"[ User.com ]:: Parameter timestamp is required",
400
throw new InstrumentationError(
"[User.com] :: Parameter timestamp is required"
);
}
};
Expand All @@ -228,7 +230,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("[User.com] :: Parameter name is required");
}
};

Expand Down Expand Up @@ -346,9 +348,8 @@ 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(
"[User.com]:: lookup field : email value is not present"
);
}

Expand Down Expand Up @@ -384,9 +385,8 @@ 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(
"[User.com] :: lookup field : phone value is not present"
);
}

Expand All @@ -409,14 +409,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(
"[User.com] :: 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(
"[User.com] :: multiple users obtained for a given lookup field"
);
} else {
const [first] = results;
Expand Down Expand Up @@ -523,9 +521,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(
`[User.com] :: lookup field : ${lookupField} is not supported for this destination`
);
} else {
const userId = getValueFromMessage(message, "userId");
Expand All @@ -536,9 +534,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(
"[User.com] :: default lookup field : email value is empty"
);
}
};
Expand Down
8 changes: 4 additions & 4 deletions test/__tests__/data/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"output": {
"error": "[ User.com ]:: Message Type is not present. Aborting message."
"error": "Event type is required"
}
},
{
Expand Down Expand Up @@ -77,7 +77,7 @@
}
},
"output": {
"error": "[ User.com ]:: Message type trackuser not supported."
"error": "Event type trackuser is not supported"
}
},
{
Expand Down Expand Up @@ -108,7 +108,7 @@
}
},
"output": {
"error": "[ User.com ]:: parameter event is required"
"error": "[User.com]:: parameter event is required"
}
},
{
Expand Down Expand Up @@ -150,7 +150,7 @@
}
},
"output": {
"error": "[ User.com ]:: Parameter name is required"
"error": "[User.com] :: Parameter name is required"
}
},
{
Expand Down