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: refactor new error types ( amplitude, algolia, appcues, attentive_tag, attribution, autopilot) #1648

Merged
merged 9 commits into from
Dec 14, 2022
36 changes: 19 additions & 17 deletions src/v0/destinations/algolia/transform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const set = require("set-value");
const { EventType } = require("../../../constants");
const {
CustomError,
InstrumentationError,
ConfigurationError
} = require("../../util/errorTypes");
const {
getValueFromMessage,
constructPayload,
defaultRequestConfig,
Expand All @@ -24,7 +27,7 @@ const {
const trackResponseBuilder = (message, { Config }) => {
let event = getValueFromMessage(message, "event");
if (!event) {
throw new CustomError("event is required for track call", 400);
throw new InstrumentationError("event is required for track call");
}
event = event.trim().toLowerCase();
let payload = constructPayload(message, trackMapping);
Expand All @@ -34,7 +37,7 @@ const trackResponseBuilder = (message, { Config }) => {
getValueFromMessage(message, "properties.eventType") || eventMapping[event];

if (!payload.eventType) {
throw new CustomError("eventType is mandatory for track call", 400);
throw new InstrumentationError("eventType is mandatory for track call");
}
payload = genericpayloadValidator(payload);

Expand All @@ -57,21 +60,19 @@ const trackResponseBuilder = (message, { Config }) => {
}
// making size of object list and position list equal
if (posLen > 0 && objLen > 0 && posLen !== objLen) {
throw new CustomError(
"length of objectId and position should be equal",
400
throw new InstrumentationError(
"length of objectId and position should be equal"
);
}
}
}
// for all events either filter or objectID should be there
if (!payload.filters && !payload.objectIDs) {
throw new CustomError("Either filters or objectIds is required.", 400);
throw new InstrumentationError("Either filters or objectIds is required.");
}
if (payload.filters && payload.objectIDs) {
throw new CustomError(
"event can’t have both objectIds and filters at the same time.",
400
throw new InstrumentationError(
"event can’t have both objectIds and filters at the same time."
);
}
if (payload.eventType === "click") {
Expand All @@ -91,17 +92,16 @@ const trackResponseBuilder = (message, { Config }) => {
const process = event => {
const { message, destination } = event;
if (!message.type) {
throw new CustomError(
"message Type is not present. Aborting message.",
400
throw new InstrumentationError(
"message Type is not present. Aborting message."
);
}

if (!destination.Config.apiKey) {
throw new CustomError("Invalid Api Key", 400);
throw new ConfigurationError("Invalid Api Key");
}
if (!destination.Config.applicationId) {
throw new CustomError("Invalid Application Id", 400);
throw new ConfigurationError("Invalid Application Id");
}
const messageType = message.type.toLowerCase();

Expand All @@ -111,14 +111,16 @@ const process = event => {
response = trackResponseBuilder(message, destination);
break;
default:
throw new CustomError(`message type ${messageType} not supported`, 400);
throw new InstrumentationError(
`message type ${messageType} not supported`
);
}
return response;
};

const processRouterDest = async inputs => {
if (!Array.isArray(inputs) || inputs.length === 0) {
throw new CustomError("Invalid event array", 400);
throw new InstrumentationError("Invalid event array");
}

const inputChunks = returnArrayOfSubarrays(inputs, MAX_BATCH_SIZE);
Expand Down
17 changes: 7 additions & 10 deletions src/v0/destinations/algolia/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const logger = require("../../../logger");
const { CustomError } = require("../../util");
const { InstrumentationError } = require("../../util/errorTypes");
const { EVENT_TYPES } = require("./config");

/**
Expand Down Expand Up @@ -28,9 +28,8 @@ const genericpayloadValidator = payload => {
const updatedPayload = payload;
updatedPayload.eventType = payload.eventType.trim().toLowerCase();
if (!EVENT_TYPES.includes(payload.eventType)) {
throw new CustomError(
"eventType can be either click, view or conversion",
400
throw new InstrumentationError(
"eventType can be either click, view or conversion"
);
}
if (payload.filters && !Array.isArray(payload.filters)) {
Expand Down Expand Up @@ -116,9 +115,8 @@ const clickPayloadValidator = payload => {
}
if (payload.objectIDs && payload.positions) {
if (payload.objectIDs.length !== payload.positions.length) {
throw new CustomError(
"length of objectId and position should be equal",
400
throw new InstrumentationError(
"length of objectId and position should be equal"
);
}
}
Expand All @@ -127,9 +125,8 @@ const clickPayloadValidator = payload => {
(payload.positions && !payload.queryID) ||
(!payload.positions && payload.queryID)
) {
throw new CustomError(
"for click eventType either both positions and queryId should be present or none",
400
throw new InstrumentationError(
"for click eventType either both positions and queryId should be present or none"
);
}
}
Expand Down
40 changes: 4 additions & 36 deletions src/v0/destinations/am/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ const {
isAppleFamily,
isDefinedAndNotNullAndNotEmpty
} = require("../../util");
const ErrorBuilder = require("../../util/error");
const {
DESTINATION,
BASE_URL,
BASE_URL_EU,
ConfigCategory,
Expand All @@ -42,6 +40,7 @@ const {
const AMUtils = require("./utils");

const logger = require("../../../logger");
const { InstrumentationError } = require("../../util/errorTypes");

const AMBatchSizeLimit = 20 * 1024 * 1024; // 20 MB
const AMBatchEventLimit = 500; // event size limit from sdk is 32KB => 15MB
Expand Down Expand Up @@ -610,18 +609,7 @@ function processSingleMessage(message, destination) {
groupInfo.group_properties = groupTraits;
} else {
logger.debug("Group call parameters are not valid");
throw new ErrorBuilder()
.setStatus(400)
.setMessage("Group call parameters are not valid")
.setStatTags({
destType: DESTINATION,
stage: TRANSFORMER_METRIC.TRANSFORMER_STAGE.TRANSFORM,
scope: TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.SCOPE,
meta:
TRANSFORMER_METRIC.MEASUREMENT_TYPE.TRANSFORMATION.META
.INSTRUMENTATION
})
.build();
throw new InstrumentationError("Group call parameters are not valid");
}
}
break;
Expand All @@ -635,17 +623,7 @@ function processSingleMessage(message, destination) {
case EventType.TRACK:
evType = message.event;
if (!isDefinedAndNotNullAndNotEmpty(evType)) {
throw new ErrorBuilder()
.setStatus(400)
.setMessage("message type not defined")
.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("message type not defined");
}
if (
message.properties &&
Expand All @@ -661,17 +639,7 @@ function processSingleMessage(message, destination) {
break;
default:
logger.debug("could not determine type");
throw new ErrorBuilder()
.setStatus(400)
.setMessage("message type not supported")
.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("message type not supported");
}
return responseBuilderSimple(
groupInfo,
Expand Down
19 changes: 10 additions & 9 deletions src/v0/destinations/appcues/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const {
removeUndefinedAndNullAndEmptyValues,
getSuccessRespEvents,
getErrorRespEvents,
CustomError
generateErrorObject
} = require("../../util");
const { InstrumentationError } = require("../../util/errorTypes");

const { ConfigCategory, mappingConfig, getEndpoint } = require("./config");

Expand Down Expand Up @@ -70,16 +71,14 @@ function process(event) {
const { message, destination } = event;

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

if (!message.userId) {
throw new CustomError(
"User id is absent. Aborting event as userId is mandatory for Appcues",
400
throw new InstrumentationError(
"User id is absent. Aborting event as userId is mandatory for Appcues"
);
}

Expand Down Expand Up @@ -113,7 +112,7 @@ function process(event) {
getEndpoint(destination.Config.accountId, message.userId)
);
default:
throw new CustomError("Message type is not supported", 400);
throw new InstrumentationError("Message type is not supported");
}
}

Expand Down Expand Up @@ -141,14 +140,16 @@ const processRouterDest = async inputs => {
input.destination
);
} catch (error) {
const errRes = generateErrorObject(error);
return getErrorRespEvents(
[input.metadata],
error.response
? error.response.status
: error.code
? error.code
: 400,
error.message || "Error occurred while processing payload."
error.message || "Error occurred while processing payload.",
errRes.statTags
);
}
})
Expand Down
Loading