improvement: translated all backend errors messages

This commit is contained in:
canove
2020-10-13 22:53:05 -03:00
parent 558475f121
commit a567614c63
45 changed files with 308 additions and 120 deletions

View File

@@ -30,7 +30,7 @@ export const update = async (
const token: string = req.cookies.jrt;
if (!token) {
throw new AppError("Invalid session. Please login.", 401);
throw new AppError("ERR_SESSION_EXPIRED", 401);
}
const { newToken, refreshToken } = await RefreshTokenService(token);

View File

@@ -8,7 +8,7 @@ import ListSettingsService from "../services/SettingServices/ListSettingsService
export const index = async (req: Request, res: Response): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can access resource.", 403);
throw new AppError("ERR_NO_PERMISSION", 403);
}
const settings = await ListSettingsService();
@@ -21,7 +21,7 @@ export const update = async (
res: Response
): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can access this route.", 403);
throw new AppError("ERR_NO_PERMISSION", 403);
}
const { settingKey: key } = req.params;
const { value } = req.body;

View File

@@ -17,7 +17,7 @@ type IndexQuery = {
export const index = async (req: Request, res: Response): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can access this route.", 403); // should be handled better.
throw new AppError("ERR_NO_PERMISSION", 403); // should be handled better.
}
const { searchParam, pageNumber } = req.query as IndexQuery;
@@ -36,9 +36,9 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
req.url === "/signup" &&
(await CheckSettingsHelper("userCreation")) === "disabled"
) {
throw new AppError("User creation is disabled by administrator.", 403);
throw new AppError("ERR_USER_CREATION_DISABLED", 403);
} else if (req.url !== "/signup" && req.user.profile !== "admin") {
throw new AppError("Only administrators can create users.", 403);
throw new AppError("ERR_NO_PERMISSION", 403);
}
const user = await CreateUserService({
@@ -70,7 +70,7 @@ export const update = async (
res: Response
): Promise<Response> => {
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can edit users.", 403);
throw new AppError("ERR_NO_PERMISSION", 403);
}
const { userId } = req.params;
@@ -94,7 +94,7 @@ export const remove = async (
const { userId } = req.params;
if (req.user.profile !== "admin") {
throw new AppError("Only administrators can delete users.", 403);
throw new AppError("ERR_NO_PERMISSION", 403);
}
await DeleteUserService(userId);

View File

@@ -7,7 +7,7 @@ const CheckSettings = async (key: string): Promise<string> => {
});
if (!setting) {
throw new AppError("No setting found with this id.", 404);
throw new AppError("ERR_NO_SETTING_FOUND", 404);
}
return setting.value;

View File

@@ -7,7 +7,7 @@ const GetDefaultWhatsApp = async (): Promise<Whatsapp> => {
});
if (!defaultWhatsapp) {
throw new AppError("ERR_NO_WAPP_FOUND");
throw new AppError("ERR_NO_DEF_WAPP_FOUND");
}
return defaultWhatsapp;

View File

@@ -19,20 +19,13 @@ export const GetWbotMessage = async (
const msgToDelete = chatMessages.find(msg => msg.id.id === messageId);
if (!msgToDelete) {
throw new AppError("msgNotFound");
throw new Error();
}
return msgToDelete;
} catch (err) {
console.log(err);
if (err.message === "msgNotFound") {
throw new AppError(
"Could not find a message witht this ID in WhatsApp chat."
);
}
throw new AppError(
"Could not valid WhatsApp contact. Check connections page"
);
throw new AppError("ERR_DELETE_WAPP_MSG");
}
};

View File

@@ -92,9 +92,7 @@ export const getWbot = (whatsappId: number): Session => {
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
if (sessionIndex === -1) {
throw new AppError(
"This WhatsApp session is not initialized. Check connections page."
);
throw new AppError("ERR_WAPP_NOT_INITIALIZED");
}
return sessions[sessionIndex];
};

View File

@@ -23,7 +23,7 @@ export const RefreshTokenService = async (token: string): Promise<Response> => {
try {
decoded = verify(token, authConfig.refreshSecret);
} catch (err) {
throw new AppError("Session expire. Please login.", 401);
throw new AppError("ERR_SESSION_EXPIRED", 401);
}
const { id, tokenVersion } = decoded as RefreshTokenPayload;
@@ -31,7 +31,7 @@ export const RefreshTokenService = async (token: string): Promise<Response> => {
const user = await ShowUserService(id);
if (user.tokenVersion !== tokenVersion) {
throw new AppError("Session revoked. Please login.", 401);
throw new AppError("ERR_SESSION_EXPIRED", 401);
}
const newToken = createAccessToken(user);

View File

@@ -25,7 +25,7 @@ const CreateContactService = async ({
});
if (numberExists) {
throw new AppError("A contact with this number already exists.");
throw new AppError("ERR_DUPLICATED_CONTACT");
}
const contact = await Contact.create(

View File

@@ -7,7 +7,7 @@ const DeleteContactService = async (id: string): Promise<void> => {
});
if (!contact) {
throw new AppError("No contact found with this ID.", 404);
throw new AppError("ERR_NO_CONTACT_FOUND", 404);
}
await contact.destroy();

View File

@@ -5,7 +5,7 @@ const ShowContactService = async (id: string | number): Promise<Contact> => {
const contact = await Contact.findByPk(id, { include: ["extraInfo"] });
if (!contact) {
throw new AppError("No contact found with this ID.", 404);
throw new AppError("ERR_NO_CONTACT_FOUND", 404);
}
return contact;

View File

@@ -32,7 +32,7 @@ const UpdateContactService = async ({
});
if (!contact) {
throw new AppError("No contact found with this ID.", 404);
throw new AppError("ERR_NO_CONTACT_FOUND", 404);
}
if (extraInfo) {

View File

@@ -22,7 +22,7 @@ const CreateMessageService = async ({
const ticket = await ShowTicketService(messageData.ticketId);
if (!ticket) {
throw new AppError("No ticket found with this ID", 404);
throw new AppError("ERR_NO_TICKET_FOUND", 404);
}
await Message.upsert(messageData);
@@ -32,7 +32,7 @@ const CreateMessageService = async ({
});
if (!message) {
throw new AppError("Error while creating message on database.", 501);
throw new AppError("ERR_CREATING_MESSAGE", 501);
}
return message;

View File

@@ -25,7 +25,7 @@ const ListMessagesService = async ({
const ticket = await ShowTicketService(ticketId);
if (!ticket) {
throw new AppError("No ticket found with this ID", 404);
throw new AppError("ERR_NO_TICKET_FOUND", 404);
}
const whereCondition = {

View File

@@ -15,7 +15,7 @@ const UpdateSettingService = async ({
});
if (!setting) {
throw new AppError("No setting found with this ID.", 404);
throw new AppError("ERR_NO_SETTING_FOUND", 404);
}
await setting.update({ value });

View File

@@ -31,7 +31,7 @@ const CreateTicketService = async ({
const ticket = await Ticket.findByPk(id, { include: ["contact"] });
if (!ticket) {
throw new AppError("Error, ticket not created.");
throw new AppError("ERR_CREATING_TICKET");
}
return ticket;

View File

@@ -7,7 +7,7 @@ const DeleteTicketService = async (id: string): Promise<Ticket> => {
});
if (!ticket) {
throw new AppError("No ticket found with this ID.", 404);
throw new AppError("ERR_NO_TICKET_FOUND", 404);
}
await ticket.destroy();

View File

@@ -21,7 +21,7 @@ const ShowTicketService = async (id: string | number): Promise<Ticket> => {
});
if (!ticket) {
throw new AppError("No ticket found with this ID", 404);
throw new AppError("ERR_NO_TICKET_FOUND", 404);
}
return ticket;

View File

@@ -38,7 +38,7 @@ const UpdateTicketService = async ({
});
if (!ticket) {
throw new AppError("No ticket found with this ID.", 404);
throw new AppError("ERR_NO_TICKET_FOUND", 404);
}
await SetTicketMessagesAsRead(ticket);

View File

@@ -9,7 +9,7 @@ const DeleteUserService = async (id: string): Promise<void> => {
});
if (!user) {
throw new AppError("No user found with this ID.", 404);
throw new AppError("ERR_NO_USER_FOUND", 404);
}
const userOpenTickets: Ticket[] = await user.$get("tickets", {

View File

@@ -7,7 +7,7 @@ const ShowUserService = async (id: string | number): Promise<User> => {
});
if (!user) {
throw new AppError("No user found with this ID.", 404);
throw new AppError("ERR_NO_USER_FOUND", 404);
}
return user;

View File

@@ -32,7 +32,7 @@ const UpdateUserService = async ({
});
if (!user) {
throw new AppError("No user found with this ID.", 404);
throw new AppError("ERR_NO_USER_FOUND", 404);
}
const schema = Yup.object().shape({

View File

@@ -15,11 +15,9 @@ const CheckIsValidContact = async (number: string): Promise<void> => {
} catch (err) {
console.log(err);
if (err.message === "invalidNumber") {
throw new AppError("This is not a valid whatsapp number.");
throw new AppError("ERR_WAPP_INVALID_CONTACT");
}
throw new AppError(
"Could not valid WhatsApp contact. Check connections page"
);
throw new AppError("ERR_WAPP_CHECK_CONTACT");
}
};

View File

@@ -149,7 +149,7 @@ const handlMedia = async (
const media = await msg.downloadMedia();
if (!media) {
throw new AppError("Cannot download media from whatsapp.");
throw new AppError("ERR_WAPP_DOWNLOAD_MEDIA");
}
if (!media.filename) {

View File

@@ -7,7 +7,7 @@ const DeleteWhatsApprService = async (id: string): Promise<void> => {
});
if (!whatsapp) {
throw new AppError("No whatsapp found with this ID.", 404);
throw new AppError("ERR_NO_WAPP_FOUND", 404);
}
await whatsapp.destroy();

View File

@@ -7,7 +7,7 @@ const ShowWhatsAppService = async (
const whatsapp = await Whatsapp.findByPk(id);
if (!whatsapp) {
throw new AppError("No whatsapp found with this conditions.", 404);
throw new AppError("ERR_NO_WAPP_FOUND", 404);
}
return whatsapp;

View File

@@ -51,7 +51,7 @@ const UpdateWhatsAppService = async ({
});
if (!whatsapp) {
throw new AppError("No whatsapp found with this ID.", 404);
throw new AppError("ERR_NO_WAPP_FOUND", 404);
}
await whatsapp.update({