pink-bears
Version:
Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications
59 lines (55 loc) • 1.74 kB
JavaScript
const crypto = require("crypto");
const { getContext } = require('./context');
const { messageGroupId } = require('./constants');
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");
const pushError = async (type, error) => {
const context = getContext();
const attributes = {
type: {
DataType: "String",
StringValue: type,
},
message: {
DataType: "String",
StringValue: error.message,
},
name: {
DataType: "String",
StringValue: error?.name
},
stack: {
DataType: "String",
StringValue: error?.stack
},
functionName: {
DataType: "String",
StringValue: context.functionName
},
logGroup: {
DataType: "String",
StringValue: context.logGroupName
},
logStream: {
DataType: "String",
StringValue: context.logStreamName
}
};
const hash = crypto.createHash("sha256");
hash.update(JSON.stringify(attributes));
const deduplicationId = hash.digest("hex");
const params = {
MessageBody: error.message,
QueueUrl: process.env.sqsUrl,
MessageAttributes: attributes,
MessageDeduplicationId: deduplicationId,
MessageGroupId: messageGroupId
}
try {
const sqsClient = new SQSClient({ region: process.env.region });
const sendMessageCommand = new SendMessageCommand(params);
await sqsClient.send(sendMessageCommand);
} catch (error) {
console.log("An error occurred while alerting error: ", error);
}
}
module.exports = { pushError };