diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 1a80737..3c25915 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -53,6 +53,7 @@ const discordAuthorizeCallback = catchAsync(async function (req: ISessionRequest res.redirect(`${config.frontend.url}/callback?` + query); } catch (err) { logger.error({ err }, 'Failed to authorize discord account'); + const params = { statusCode: STATUS_CODE_ERROR, }; diff --git a/src/services/announcement.service.ts b/src/services/announcement.service.ts index 6011606..4f48fb4 100644 --- a/src/services/announcement.service.ts +++ b/src/services/announcement.service.ts @@ -7,8 +7,9 @@ import config from '../config'; import sagaService from './saga.service'; import platformService from './platform.service'; import Handlebars from 'handlebars'; -import logger from '../config/logger'; +import parentLogger from '../config/logger'; import communityService from './community.service'; +const logger = parentLogger.child({ module: 'AnnouncementService' }); const createDraftAnnouncement = async (announcementData: IAnnouncement) => { if (announcementData.draft === false) throw new Error('Cannot create a draft announcement with draft set to false'); @@ -37,6 +38,7 @@ const createScheduledAnnouncement = async (announcementData: IAnnouncement) => { await session.commitTransaction(); return announcement; } catch (error) { + logger.error(error, 'Failed to create announcement'); await session.abortTransaction(); throw error; } finally { @@ -69,6 +71,7 @@ const updateAndRescheduleAnnouncement = async ( await session.commitTransaction(); return newAnnouncement; } catch (error) { + logger.error(error, 'Failed to update and reschedule announcement'); await session.abortTransaction(); throw error; } finally { @@ -98,6 +101,7 @@ const updateAnnouncementAndRemoveJob = async ( await session.commitTransaction(); return newAnnouncement; } catch (error) { + logger.error(error, 'Failed to update announcement and remove job'); await session.abortTransaction(); throw error; } finally { @@ -128,6 +132,7 @@ const updateAnnouncementAndAddJob = async ( await session.commitTransaction(); return newAnnouncement; } catch (error) { + logger.error(error, 'Failed to update announcement and add job'); await session.abortTransaction(); throw error; } finally { @@ -235,7 +240,7 @@ const bullMQTriggeredAnnouncement = async (job: Job) => { await mongoose.connect(config.mongoose.serverURL); logger.info('Setuped Message Broker connection!'); } catch (error) { - logger.fatal('Failed to setup to Message Broker!!'); + logger.fatal(error, 'Failed to setup to Message Broker!!'); } const announcement = await Announcement.findById(announcementId); diff --git a/src/services/discord/channel.service.ts b/src/services/discord/channel.service.ts index f44ec38..fbc31e7 100644 --- a/src/services/discord/channel.service.ts +++ b/src/services/discord/channel.service.ts @@ -91,7 +91,7 @@ async function checkBotPermissions( } return false; } catch (error) { - logger.error({ error }, 'Failed to check bot permissions'); + logger.error(error, 'Failed to check bot permissions'); return false; } } diff --git a/src/services/discord/core.service.ts b/src/services/discord/core.service.ts index 2283615..84236e5 100644 --- a/src/services/discord/core.service.ts +++ b/src/services/discord/core.service.ts @@ -151,11 +151,10 @@ async function exchangeCode(code: string, redirect_uri: string): Promise throw new Error(`Failed to get user from Discord API: ${errorResponse}`); } } catch (error) { - logger.error({ error }, 'Failed to get user from Discord API'); + logger.error(error, 'Failed to get user from Discord API'); throw new ApiError(590, 'Can not fetch from discord API'); } } @@ -203,7 +202,7 @@ async function getBotFromDiscordAPI(): Promise { throw new Error(`Failed to get bot from Discord API: ${errorResponse}`); } } catch (error) { - logger.error({ error }, 'Failed to get bot from Discord API'); + logger.error(error, 'Failed to get bot from Discord API'); throw new ApiError(590, 'Can not fetch from discord API'); } } @@ -220,7 +219,7 @@ async function getBotPermissions(guildId: Snowflake): Promise> { const member = await guild.members.fetch(config.oAuth2.discord.clientId); return member.permissions.toArray(); } catch (error) { - logger.error({ error }, 'Failed to get list of permissions that bot has in a specific guild'); + logger.error(error, 'Failed to get list of permissions that bot has in a specific guild'); throw new ApiError(590, 'Failed to get list of permissions that bot has in a specific guild'); } } @@ -337,7 +336,7 @@ async function refreshAuth(refreshToken: string): Promise { memberActivity[activity].forEach((discordId: string) => allDiscordIds.add(discordId)); }); - return allDiscordIds; }; diff --git a/src/services/github/core.service.ts b/src/services/github/core.service.ts index cc6f32d..65c1401 100644 --- a/src/services/github/core.service.ts +++ b/src/services/github/core.service.ts @@ -24,7 +24,7 @@ async function generateAppAccessToken() { var token = jwt.sign(payload, privateKey, { algorithm: 'RS256' }); return token; } catch (error) { - logger.error({ error }, 'Failed to create access token for github app'); + logger.error(error, 'Failed to create access token for github app'); throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Failed to create access token for github app'); } } @@ -48,11 +48,10 @@ async function getInstallationAccessToken(appAccessToken: string, installationId return await response.json(); } else { const errorResponse = await response.text(); - logger.error({ error: errorResponse }, 'Failed to get installation access token'); - throw new Error(`Failed to get installation access token: ${errorResponse}`); + throw new Error(errorResponse); } } catch (error) { - logger.error({ installationId, error }, 'Failed to get installation access token'); + logger.error(error, 'Failed to get installation access token'); throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Failed to get installation access token'); } } @@ -76,11 +75,10 @@ async function getInstallationRepos(installationAccessToken: string, installatio return await response.json(); } else { const errorResponse = await response.text(); - logger.error({ error: errorResponse }, 'Failed to get installation repos'); - throw new Error(`Failed to get installation repos: ${errorResponse}`); + throw new Error(errorResponse); } } catch (error) { - logger.error({ installationId, error }, 'Failed to get installation repos'); + logger.error(error, 'Failed to get installation repos'); throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Failed to get installation repos'); } } @@ -104,11 +102,10 @@ async function getInstallationDetails(appAccessToken: string, installationId: st return await response.json(); } else { const errorResponse = await response.text(); - logger.error({ error: errorResponse }, 'Failed to get installation details'); - throw new Error(`Failed to get installation details: ${errorResponse}`); + throw new Error(errorResponse); } } catch (error) { - logger.error({ installationId, error }, 'Failed to get installation details'); + logger.error(error, 'Failed to get installation details'); throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Failed to get installation details'); } } diff --git a/src/services/google/core.service.ts b/src/services/google/core.service.ts index 56975dd..7f68721 100644 --- a/src/services/google/core.service.ts +++ b/src/services/google/core.service.ts @@ -69,11 +69,10 @@ async function getUserProfile(accessToken: string) { return await response.json(); } else { const errorResponse = await response.text(); - logger.error({ error: errorResponse }, 'Failed to get user profile'); - throw new Error(`Failed to get user profile: ${errorResponse}`); + throw new Error(errorResponse); } } catch (error) { - logger.error({ error }, 'Failed to get user profile'); + logger.error(error, 'Failed to get user profile'); throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Failed to get user profile'); } } diff --git a/src/services/notion/core.service.ts b/src/services/notion/core.service.ts index d4d6c06..defd616 100644 --- a/src/services/notion/core.service.ts +++ b/src/services/notion/core.service.ts @@ -31,11 +31,10 @@ async function exchangeCode(code: string) { return await response.json(); } else { const errorResponse = await response.text(); - logger.error({ error: errorResponse }, 'Failed to exchange notion code'); - throw new Error(`Failed to exchange notion code: ${errorResponse}`); + throw new Error(errorResponse); } } catch (error) { - logger.error({ redirect_uri: config.oAuth2.notion.callbackURI.connect, error }, 'Failed to exchange notion code'); + logger.error(error, 'Failed to exchange notion code'); throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Failed to exchange notion code'); } }