Skip to content

Commit

Permalink
debug: add or update logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Sep 2, 2024
1 parent 5f64e76 commit 25ffd95
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
9 changes: 7 additions & 2 deletions src/services/announcement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/services/discord/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/services/discord/core.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,10 @@ async function exchangeCode(code: string, redirect_uri: string): Promise<IDiscor
return await response.json();
} else {
const errorResponse = await response.text();
logger.error({ error: errorResponse }, 'Failed to exchange discord code');
throw new Error(`Failed to exchange discord code: ${errorResponse}`);
throw new Error(errorResponse);
}
} catch (error) {
logger.error({ redirect_uri: config.oAuth2.discord.callbackURI.connect, error }, 'Failed to exchange discord code');
} catch (error: any) {
logger.error(error, 'Failed to exchange discord code');
throw new ApiError(590, 'Can not fetch from discord API');
}
}
Expand All @@ -179,7 +178,7 @@ async function getUserFromDiscordAPI(accessToken: string): Promise<IDiscordUser>
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');
}
}
Expand All @@ -203,7 +202,7 @@ async function getBotFromDiscordAPI(): Promise<IDiscordUser> {
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');
}
}
Expand All @@ -220,7 +219,7 @@ async function getBotPermissions(guildId: Snowflake): Promise<Array<string>> {
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');
}
}
Expand Down Expand Up @@ -337,7 +336,7 @@ async function refreshAuth(refreshToken: string): Promise<IDiscordOAuth2EchangeC
throw new Error();
}
} catch (error) {
logger.error({ error }, 'Failed to refresh discord auth');
logger.error(error, 'Failed to refresh discord auth');
throw new ApiError(httpStatus.INTERNAL_SERVER_ERROR, 'Can not fetch from discord API');
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/services/discord/guildMember.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ const queryGuildMembers = async (guildConnection: any, filter: any, options: Opt
totalResults,
};
} catch (error) {
logger.error({ guildConnection: guildConnection.name, filter, options, error }, 'Failed to query guild members');
logger.error(error, 'Failed to query guild members');
return {
results: [],
limit: 10,
Expand All @@ -307,7 +307,6 @@ const getAllDiscordIdsInLastedMemberActivity = async (platformConnection: Connec
memberActivities.forEach((activity) => {
memberActivity[activity].forEach((discordId: string) => allDiscordIds.add(discordId));
});

return allDiscordIds;
};

Expand Down
17 changes: 7 additions & 10 deletions src/services/github/core.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand All @@ -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');
}
}
Expand All @@ -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');
}
}
Expand All @@ -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');
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/services/google/core.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/services/notion/core.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down

0 comments on commit 25ffd95

Please sign in to comment.