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(shynet, sfmc, serenytics, sendgrid, segment) #1657

Merged
merged 3 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 21 additions & 39 deletions src/v0/destinations/sendgrid/deleteUsers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const _ = require("lodash");
const { httpDELETE } = require("../../../adapters/network");
const ErrorBuilder = require("../../util/error");
const { MAX_BATCH_SIZE, DELETE_CONTACTS_ENDPOINT } = require("./config");
const {
MAX_BATCH_SIZE,
DESTINATION,
DELETE_CONTACTS_ENDPOINT
} = require("./config");
const {
processAxiosResponse
processAxiosResponse,
getDynamicErrorType
} = require("../../../adapters/utils/networkUtils");
const { isHttpStatusSuccess } = require("../../util");
const { TRANSFORMER_METRIC } = require("../../util/constant");
const {
NetworkError,
ConfigurationError,
InstrumentationError
} = require("../../util/errorTypes");
const tags = require("../../util/tags");

/**
* This function will help to delete the users one by one from the userAttributes array.
Expand All @@ -21,29 +22,11 @@ const { TRANSFORMER_METRIC } = require("../../util/constant");
const userDeletionHandler = async (userAttributes, config) => {
const { apiKey } = config;
if (!Array.isArray(userAttributes)) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: userAttributes is not an array")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError("userAttributes is not an array");
}

if (!apiKey) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: apiKey is required for deleting user")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new ConfigurationError("ApiKey is required for deleting user");
}

let endpoint = DELETE_CONTACTS_ENDPOINT;
Expand All @@ -68,17 +51,16 @@ const userDeletionHandler = async (userAttributes, config) => {
const deletionRespone = await httpDELETE(endpoint, requestOptions);
const processedDeletionRespone = processAxiosResponse(deletionRespone);
if (!isHttpStatusSuccess(processedDeletionRespone.status)) {
throw new ErrorBuilder()
.setMessage("[SendGrid]::Deletion Request is not successful")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta:
TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_EVENT
})
.build();
throw new NetworkError(
"Deletion Request is not successful",
processedDeletionRespone.status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(
processedDeletionRespone.status
)
},
processedDeletionRespone
);
}
});

Expand Down
73 changes: 15 additions & 58 deletions src/v0/destinations/sendgrid/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require("lodash");
const { EventType } = require("../../../constants");
const {
ErrorMessage,
isEmptyObject,
constructPayload,
getErrorRespEvents,
Expand Down Expand Up @@ -32,9 +33,11 @@ const {
generatePayloadFromConfig,
createOrUpdateContactPayloadBuilder
} = require("./util");
const ErrorBuilder = require("../../util/error");
const { TRANSFORMER_METRIC } = require("../../util/constant");
const { DESTINATION } = require("./config");
const {
ConfigurationError,
TransformationError,
InstrumentationError
} = require("../../util/errorTypes");

const responseBuilder = (payload, method, endpoint, apiKey) => {
if (payload) {
Expand All @@ -50,16 +53,7 @@ const responseBuilder = (payload, method, endpoint, apiKey) => {
}

// fail-safety for developer error
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Payload could not be constructed")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_EVENT
})
.build();
throw new TransformationError(ErrorMessage.FailedToConstructPayload);
};

const identifyResponseBuilder = async (message, destination) => {
Expand Down Expand Up @@ -90,19 +84,9 @@ const trackResponseBuilder = async (message, { Config }) => {
if (email) {
payload.personalizations = [{ to: [{ email }] }];
} else {
throw new ErrorBuilder()
.setMessage(
"[SendGrid] :: Either email not found in traits or personalizations field is missing/empty"
)
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta:
TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError(
"Either email not found in traits or personalizations field is missing/empty"
);
}
}
}
Expand Down Expand Up @@ -153,30 +137,11 @@ const trackResponseBuilder = async (message, { Config }) => {
const processEvent = async (message, destination) => {
// Validating if message type is even given or not
if (!message.type) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Message Type is not present. Aborting message")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_EVENT
})
.build();
throw new InstrumentationError("Event type is required");
}

if (!destination.Config.apiKey) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Invalid Api Key")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta:
TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.CONFIGURATION
})
.build();
throw new ConfigurationError("Invalid Api Key");
}

const messageType = message.type.toLowerCase();
Expand All @@ -189,17 +154,9 @@ const processEvent = async (message, destination) => {
response = await trackResponseBuilder(message, destination);
break;
default:
throw new ErrorBuilder()
.setMessage(`[SendGrid] :: Message type ${messageType} not supported`)
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta:
TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_EVENT
})
.build();
throw new InstrumentationError(
`Event type ${messageType} is not supported`
);
}
return response;
};
Expand Down
115 changes: 29 additions & 86 deletions src/v0/destinations/sendgrid/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ const {
removeUndefinedAndNullValues,
removeUndefinedAndNullAndEmptyValues
} = require("../../util");
const tags = require("../../util/tags");
const Cache = require("../../util/cache");
const ErrorBuilder = require("../../util/error");
const {
getDynamicErrorType,
processAxiosResponse
} = require("../../../adapters/utils/networkUtils");
const { httpGET } = require("../../../adapters/network");
const { AUTH_CACHE_TTL, TRANSFORMER_METRIC } = require("../../util/constant");
const { MAPPING_CONFIG, CONFIG_CATEGORIES, DESTINATION } = require("./config");
const {
NetworkError,
ConfigurationError,
InstrumentationError
} = require("../../util/errorTypes");
const { AUTH_CACHE_TTL } = require("../../util/constant");
const { MAPPING_CONFIG, CONFIG_CATEGORIES } = require("./config");

const customFieldsCache = new Cache(AUTH_CACHE_TTL);

Expand Down Expand Up @@ -53,85 +59,32 @@ const isValidEvent = (Config, event) => {
const validateTrackPayload = (message, Config) => {
let event = getValueFromMessage(message, "event");
if (!event) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Event is required for track call")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError("Event is required for track call");
}
event = event.trim().toLowerCase();
if (!isValidEvent(Config, event)) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Event not configured on dashboard")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new ConfigurationError("Event not configured on dashboard");
}
};

const requiredFieldValidator = payload => {
if (!payload.template_id) {
if (!payload.content || (payload.content && isEmpty(payload.content))) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Either template id or content is required")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta:
TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError(
"Either template id or content is required"
);
}
}
if (!payload.personalizations || isEmpty(payload.personalizations)) {
throw new ErrorBuilder()
.setMessage(
"[SendGrid] :: Personalizations field cannot be missing or empty"
)
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError(
"Personalizations field cannot be missing or empty"
);
}
if (!payload.from) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: From is required field")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError("From is required field");
}
if (payload.from && !payload.from.email) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Email inside from object is required")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError("Email inside from object is required");
}
};

Expand Down Expand Up @@ -456,16 +409,7 @@ const generatePayloadFromConfig = (payload, Config) => {
const validateIdentifyPayload = message => {
const email = getFieldValueFromMessage(message, "email");
if (!email) {
throw new ErrorBuilder()
.setMessage("[SendGrid] :: Parameter mail is required")
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_PARAM
})
.build();
throw new InstrumentationError("Parameter mail is required");
}
};

Expand Down Expand Up @@ -538,16 +482,15 @@ const fetchCustomFields = async destination => {
}

const { message } = processedResponse.response.errors[0];
throw new ErrorBuilder()
.setMessage(message)
.setStatus(400)
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META.BAD_EVENT
})
.build();
const status = processedResponse.status || 400;
throw new NetworkError(
message,
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status)
},
processedResponse.response
);
});
};

Expand Down
Loading