Skip to content

Commit

Permalink
MessageController.ts: better error messages and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 5, 2024
1 parent a1c0c44 commit 802234e
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions backend/src/controllers/MessageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import EditWhatsAppMessage from "../services/WbotServices/EditWhatsAppMessage";

import { sendFacebookMessageMedia } from "../services/FacebookServices/sendFacebookMessageMedia";
import sendFaceMessage from "../services/FacebookServices/sendFacebookMessage";
import { logger } from "../utils/logger";

type IndexQuery = {
pageNumber: string;
Expand Down Expand Up @@ -143,21 +144,20 @@ export const remove = async (
};

export const send = async (req: Request, res: Response): Promise<Response> => {
const { whatsappId } = req.params as unknown as { whatsappId: number };
const { whatsappId } = req.params;
const messageData: MessageData = req.body;
const medias = req.files as Express.Multer.File[];

try {
const whatsapp = await Whatsapp.findByPk(whatsappId);

if (!whatsapp) {
throw new Error("Não foi possível realizar a operação");
}
if (messageData.number === undefined) {
throw new AppError("ERR_SYNTAX", 400);
}
const whatsapp = await Whatsapp.findByPk(whatsappId);

if (messageData.number === undefined) {
throw new Error("O número é obrigatório");
}
if (!whatsapp) {
throw new AppError("ERR_WHATSAPP_NOT_FOUND", 404);
}

try {
const numberToTest = messageData.number;
const { body } = messageData;

Expand Down Expand Up @@ -198,14 +198,17 @@ export const send = async (req: Request, res: Response): Promise<Response> => {
);
}

return res.send({ mensagem: "Mensagem enviada" });
return res.send({ mensagem: "Message added to queue" });
} catch (err) {
if (Object.keys(err).length === 0) {
throw new AppError(
"Não foi possível enviar a mensagem, tente novamente em alguns instantes"
);
const error = { errType: typeof err, serialized: JSON.stringify(err), err };
if (err?.message) {
console.error(error, `MessageController.send: ${err.message}`);
} else {
throw new AppError(err.message);
logger.error(
error,
"MessageController.send: Failed to put message on queue"
);
}
throw new AppError("ERR_INTERNAL_ERROR", 500);
}
};

0 comments on commit 802234e

Please sign in to comment.