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 destinations (snapchat_custom_audience, snapchat_conversion, slack, singular, signl4) #1655

Merged
merged 5 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
27 changes: 13 additions & 14 deletions src/v0/destinations/signl4/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ const { populatePayload } = require("./utils");
const {
defaultRequestConfig,
defaultPostRequestConfig,
CustomError,
getErrorRespEvents,
getSuccessRespEvents,
removeUndefinedAndNullAndEmptyValues
} = require("../../util");
const {
InstrumentationError,
ConfigurationError,
TransformationError
} = require("../../util/errorTypes");

const responseBuilder = (payload, endpoint) => {
if (payload) {
Expand All @@ -21,9 +25,8 @@ const responseBuilder = (payload, endpoint) => {
response.body.JSON = removeUndefinedAndNullAndEmptyValues(payload);
return response;
}
throw new CustomError(
"Payload could not be populated due to wrong input",
400
throw new TransformationError(
"Payload could not be populated due to wrong input"
);
};

Expand All @@ -38,10 +41,7 @@ const trackResponseBuilder = (message, { Config }) => {
const { event } = message;

if (!event) {
throw new CustomError(
"[SIGNL4]: event is not present in the input payload",
400
);
throw new InstrumentationError("Event is not present in the input payload");
}

const endpoint = `${BASE_URL}/${apiKey}`;
Expand All @@ -53,13 +53,10 @@ const trackResponseBuilder = (message, { Config }) => {

const processEvent = (message, destination) => {
if (!message.type) {
throw new CustomError(
"Message Type is not present. Aborting message.",
400
);
throw new InstrumentationError("Event type is required");
}
if (!destination.Config.apiKey) {
throw new CustomError("apiKey is a required field", 400);
throw new ConfigurationError("apiKey is a required field");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new ConfigurationError("apiKey is a required field");
throw new ConfigurationError("ApiKey is a required field");

}
const messageType = message.type.toLowerCase();
let response;
Expand All @@ -68,7 +65,9 @@ const processEvent = (message, destination) => {
response = trackResponseBuilder(message, destination);
break;
default:
throw new CustomError(`Message type ${messageType} not supported`, 400);
throw new InstrumentationError(
`Event type ${messageType} is not supported`
);
}
return response;
};
Expand Down
22 changes: 6 additions & 16 deletions src/v0/destinations/singular/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ const {
defaultRequestConfig,
defaultGetRequestConfig,
getSuccessRespEvents,
getErrorRespEvents,
CustomError
getErrorRespEvents
} = require("../../util");

const {
platformWisePayloadGenerator,
generateRevenuePayloadArray,
isSessionEvent
} = require("./util");
const { InstrumentationError } = require("../../util/errorTypes");

const responseBuilderSimple = (message, { Config }) => {
const eventName = message.event;

if (!eventName) {
throw new CustomError(
"[Singular]::event name is not present for the event",
400
);
throw new InstrumentationError("event name is not present for the event");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new InstrumentationError("event name is not present for the event");
throw new InstrumentationError("Event name is not present for the event");

}

const sessionEvent = isSessionEvent(Config, eventName);
Expand Down Expand Up @@ -58,18 +55,15 @@ const responseBuilderSimple = (message, { Config }) => {

const processEvent = (message, destination) => {
if (!message.type) {
throw new CustomError(
"Message Type is not present. Aborting message.",
400
);
throw new InstrumentationError("Event type is required");
}
const messageType = message.type.toLowerCase();

if (messageType === "track") {
return responseBuilderSimple(message, destination);
}

throw new CustomError("[Singular]: Message type not supported", 400);
throw new InstrumentationError(`Event type ${messageType} is not supported`);
};

const process = event => {
Expand Down Expand Up @@ -102,11 +96,7 @@ const processRouterDest = inputs => {
} catch (error) {
return getErrorRespEvents(
[input.metadata],
error.response
? error.response.status
: error.code
? error.code
: 400,
error.response ? error.response.status : error.code || 400,
error.message || "Error occurred while processing payload."
);
}
Expand Down
14 changes: 8 additions & 6 deletions src/v0/destinations/singular/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ const {
defaultRequestConfig,
defaultGetRequestConfig,
removeUndefinedAndNullValues,
CustomError,
extractCustomFields,
getValueFromMessage,
isDefinedAndNotNull,
isAppleFamily
} = require("../../util");
const {
TransformationError,
InstrumentationError
} = require("../../util/errorTypes");

/*
All the fields listed inside properties which are not directly mapped, will be sent to 'e' as custom event attributes
Expand Down Expand Up @@ -106,7 +109,7 @@ const platformWisePayloadGenerator = (message, isSessionEvent) => {
let platform = getValueFromMessage(message, "context.os.name");
const typeOfEvent = isSessionEvent ? "SESSION" : "EVENT";
if (!platform) {
throw new CustomError("[Singular] :: Platform name is missing", 400);
throw new InstrumentationError("Platform name is missing");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new InstrumentationError("Platform name is missing");
throw new InstrumentationError("Platform name is missing from context.os.name");

}
// checking if the os is one of ios, ipados, watchos, tvos
if (typeof platform === "string" && isAppleFamily(platform.toLowerCase())) {
Expand All @@ -115,7 +118,7 @@ const platformWisePayloadGenerator = (message, isSessionEvent) => {
}
platform = platform.toLowerCase();
if (!SUPPORTED_PLATFORM[platform]) {
throw new CustomError("[Singular] :: Platform is not supported");
throw new InstrumentationError("Platform is not supported");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new InstrumentationError("Platform is not supported");
throw new InstrumentationError(`Platform ${platform} is not supported`);

}

const payload = constructPayload(
Expand All @@ -126,9 +129,8 @@ const platformWisePayloadGenerator = (message, isSessionEvent) => {
);

if (!payload) {
throw new CustomError(
`Failed to Create ${platform} ${typeOfEvent} Payload`,
400
throw new TransformationError(
`Failed to Create ${platform} ${typeOfEvent} Payload`
);
}
if (isSessionEvent) {
Expand Down
21 changes: 8 additions & 13 deletions src/v0/destinations/slack/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const {
defaultRequestConfig,
getFieldValueFromMessage,
getSuccessRespEvents,
getErrorRespEvents,
CustomError
getErrorRespEvents
} = require("../../util");
const { InstrumentationError } = require("../../util/errorTypes");

// to string json traits, not using JSON.stringify()
// always first check for whitelisted traits
Expand Down Expand Up @@ -179,7 +179,7 @@ function processTrack(message, destination) {
const eventTemplateConfig = destination.Config.eventTemplateSettings;

if (!message.event) {
throw new CustomError("Event name is required", 400);
throw new InstrumentationError("Event name is required");
}
const eventName = message.event;
const channelListToSendThisEvent = new Set();
Expand Down Expand Up @@ -308,10 +308,7 @@ function process(event) {
let response;
const { message, destination } = event;
if (!message.type) {
throw new CustomError(
"Message Type is not present. Aborting message.",
400
);
throw new InstrumentationError("Event type is required");
}
const messageType = message.type.toLowerCase();
logger.debug("messageType: ", messageType);
Expand All @@ -329,7 +326,9 @@ function process(event) {
break;
default:
logger.debug("Message type not supported");
throw new CustomError("Message type not supported", 400);
throw new InstrumentationError(
`Event type ${messageType} is not supported`
);
}
logger.debug(JSON.stringify(respList));
logger.debug("=====end======");
Expand Down Expand Up @@ -362,11 +361,7 @@ const processRouterDest = async inputs => {
} catch (error) {
return getErrorRespEvents(
[input.metadata],
error.response
? error.response.status
: error.code
? error.code
: 400,
error.response ? error.response.status : error.code || 400,
error.message || "Error occurred while processing payload."
);
}
Expand Down
43 changes: 16 additions & 27 deletions src/v0/destinations/snapchat_conversion/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
removeUndefinedAndNullValues,
getFieldValueFromMessage,
getSuccessRespEvents,
CustomError,
isAppleFamily,
getValidDynamicFormConfig,
checkInvalidRtTfEvents,
Expand All @@ -33,6 +32,7 @@ const {
channelMapping,
generateBatchedPayloadForArray
} = require("./util");
const { InstrumentationError } = require("../../util/errorTypes");

// Returns the response for the track event after constructing the payload and setting necessary fields
function trackResponseBuilder(message, { Config }, mappedEvent) {
Expand All @@ -57,23 +57,16 @@ function trackResponseBuilder(message, { Config }, mappedEvent) {
(eventConversionType === "WEB" || eventConversionType === "OFFLINE") &&
!pixelId
) {
throw new CustomError(
"[Snapchat] :: Pixel Id is required for web and offline events",
400
throw new InstrumentationError(
Copy link
Member

Choose a reason for hiding this comment

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

This should be configuration error

"Pixel Id is required for web and offline events"
);
}

if (eventConversionType === "MOBILE_APP" && !(appId && snapAppId)) {
if (!appId) {
throw new CustomError(
"[Snapchat] :: App Id is required for app events",
400
);
throw new InstrumentationError("App Id is required for app events");
Copy link
Member

Choose a reason for hiding this comment

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

configuration error

} else {
throw new CustomError(
"[Snapchat] :: Snap App Id is required for app events",
400
);
throw new InstrumentationError("Snap App Id is required for app events");
Copy link
Member

Choose a reason for hiding this comment

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

Same here

}
}

Expand Down Expand Up @@ -178,9 +171,8 @@ function trackResponseBuilder(message, { Config }, mappedEvent) {
break;
}
} else {
throw new CustomError(
`Event ${event} doesn't match with Snapchat Events!`,
400
throw new InstrumentationError(
`Event ${event} doesn't match with Snapchat Events!`
);
}

Expand Down Expand Up @@ -239,9 +231,8 @@ function trackResponseBuilder(message, { Config }, mappedEvent) {
!payload.hashed_mobile_ad_id &&
!(payload.hashed_ip_address && payload.user_agent)
) {
throw new CustomError(
"At least one of email or phone or advertisingId or ip and userAgent is required",
400
throw new InstrumentationError(
"At least one of email or phone or advertisingId or ip and userAgent is required"
);
}
payload.timestamp = getFieldValueFromMessage(message, "timestamp");
Expand All @@ -252,9 +243,8 @@ function trackResponseBuilder(message, { Config }, mappedEvent) {
// calculates past event in days
const deltaDay = Math.ceil(moment.duration(current.diff(start)).asDays());
if (deltaDay > 28) {
throw new CustomError(
"[snapchat_conversion]: Events must be sent within 28 days of their occurrence.",
400
throw new InstrumentationError(
"Events must be sent within 28 days of their occurrence"
);
}
}
Expand Down Expand Up @@ -302,7 +292,7 @@ function eventMappingHandler(message, destination) {
let event = get(message, "event");

if (!event) {
throw new CustomError("[Snapchat] :: Event name is required", 400);
throw new InstrumentationError("Event name is required");
}
event = event.trim().replace(/\s+/g, "_");

Expand Down Expand Up @@ -336,10 +326,7 @@ function process(event) {
const { message, destination } = event;

if (!message.type) {
throw new CustomError(
"Message Type is not present. Aborting message.",
400
);
throw new InstrumentationError("Event type is required");
}

const messageType = message.type.toLowerCase();
Expand All @@ -363,7 +350,9 @@ function process(event) {
break;
}
default:
throw new CustomError(`Message type ${messageType} not supported`, 400);
throw new InstrumentationError(
`Event type ${messageType} is not supported`
);
}
return response;
}
Expand Down
15 changes: 7 additions & 8 deletions src/v0/destinations/snapchat_custom_audience/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const {
defaultRequestConfig,
removeUndefinedAndNullValues,
simpleProcessRouterDest,
CustomError,
isDefinedAndNotNullAndNotEmpty
} = require("../../util");
const ErrorBuilder = require("../../util/error");
const {
InstrumentationError,
InvalidAuthTokenError
} = require("../../util/errorTypes");
const { BASE_URL, schemaType } = require("./config");
const { validatePayload, validateFields } = require("./utils");

Expand All @@ -26,10 +28,7 @@ const getAccessToken = metadata => {
const { secret } = metadata;
// we would need to verify if secret is present and also if the access token field is present in secret
if (!secret || !secret.access_token) {
throw new ErrorBuilder()
.setStatus(500)
.setMessage("Empty/Invalid access token")
.build();
throw new InvalidAuthTokenError("Empty/Invalid access token");
}
return secret.access_token;
};
Expand Down Expand Up @@ -80,7 +79,7 @@ const getProperty = (schema, element) => {
element?.MOBILE_ID;
return property;
default:
throw new CustomError("Invalid schema", 400);
throw new InstrumentationError("Invalid schema");
Copy link
Member

Choose a reason for hiding this comment

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

Config error

}
};

Expand All @@ -100,7 +99,7 @@ const getHashedProperty = (schema, hashedProperty) => {
case "mobileAdId":
return sha256(hashedProperty.toLowerCase());
default:
throw new CustomError("Invalid schema", 400);
throw new InstrumentationError("Invalid schema");
Copy link
Member

Choose a reason for hiding this comment

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

Config error

}
};

Expand Down
Loading