Skip to content

Commit 5564dd8

Browse files
authored
refactor: broaden try/catch and add coverage (#1080)
1 parent a8e80a1 commit 5564dd8

File tree

2 files changed

+117
-96
lines changed

2 files changed

+117
-96
lines changed

src/utils/subject.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,32 @@ describe('utils/subject.ts', () => {
860860
expect(result).toBeNull();
861861
});
862862
});
863+
864+
describe('Error', () => {
865+
it('catches error and logs message', async () => {
866+
const consoleErrorSpy = jest
867+
.spyOn(console, 'error')
868+
.mockImplementation();
869+
870+
const mockError = new Error('Test error');
871+
const mockNotification = partialMockNotification({
872+
title: 'This issue will throw an error',
873+
type: 'Issue',
874+
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1',
875+
});
876+
877+
nock('https://api.github.com')
878+
.get('/repos/gitify-app/notifications-test/issues/1')
879+
.replyWithError(mockError);
880+
881+
await getGitifySubjectDetails(mockNotification, mockAccounts.token);
882+
883+
expect(consoleErrorSpy).toHaveBeenCalledWith(
884+
'Error occurred while fetching details for Issue notification: This issue will throw an error',
885+
mockError,
886+
);
887+
});
888+
});
863889
});
864890

865891
describe('getCheckSuiteState', () => {

src/utils/subject.ts

Lines changed: 91 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,30 @@ export async function getGitifySubjectDetails(
2323
notification: Notification,
2424
token: string,
2525
): Promise<GitifySubject> {
26-
switch (notification.subject.type) {
27-
case 'CheckSuite':
28-
return getGitifySubjectForCheckSuite(notification);
29-
case 'Commit':
30-
return getGitifySubjectForCommit(notification, token);
31-
case 'Discussion':
32-
return await getGitifySubjectForDiscussion(notification, token);
33-
case 'Issue':
34-
return await getGitifySubjectForIssue(notification, token);
35-
case 'PullRequest':
36-
return await getGitifySubjectForPullRequest(notification, token);
37-
case 'Release':
38-
return await getGitifySubjectForRelease(notification, token);
39-
case 'WorkflowRun':
40-
return getGitifySubjectForWorkflowRun(notification);
41-
default:
42-
return null;
26+
try {
27+
switch (notification.subject.type) {
28+
case 'CheckSuite':
29+
return getGitifySubjectForCheckSuite(notification);
30+
case 'Commit':
31+
return getGitifySubjectForCommit(notification, token);
32+
case 'Discussion':
33+
return await getGitifySubjectForDiscussion(notification, token);
34+
case 'Issue':
35+
return await getGitifySubjectForIssue(notification, token);
36+
case 'PullRequest':
37+
return await getGitifySubjectForPullRequest(notification, token);
38+
case 'Release':
39+
return await getGitifySubjectForRelease(notification, token);
40+
case 'WorkflowRun':
41+
return getGitifySubjectForWorkflowRun(notification);
42+
default:
43+
return null;
44+
}
45+
} catch (err) {
46+
console.error(
47+
`Error occurred while fetching details for ${notification.subject.type} notification: ${notification.subject.title}`,
48+
err,
49+
);
4350
}
4451
}
4552

@@ -106,33 +113,29 @@ async function getGitifySubjectForCommit(
106113
notification: Notification,
107114
token: string,
108115
): Promise<GitifySubject> {
109-
try {
110-
let user: User;
116+
let user: User;
111117

112-
if (notification.subject.latest_comment_url) {
113-
const commitComment = (
114-
await getCommitComment(notification.subject.latest_comment_url, token)
115-
).data;
118+
if (notification.subject.latest_comment_url) {
119+
const commitComment = (
120+
await getCommitComment(notification.subject.latest_comment_url, token)
121+
).data;
116122

117-
user = commitComment.user;
118-
} else {
119-
const commit = (await getCommit(notification.subject.url, token)).data;
123+
user = commitComment.user;
124+
} else {
125+
const commit = (await getCommit(notification.subject.url, token)).data;
120126

121-
user = commit.author;
122-
}
123-
124-
return {
125-
state: null,
126-
user: {
127-
login: user.login,
128-
html_url: user.html_url,
129-
avatar_url: user.avatar_url,
130-
type: user.type,
131-
},
132-
};
133-
} catch (err) {
134-
console.error('Commit subject retrieval failed');
127+
user = commit.author;
135128
}
129+
130+
return {
131+
state: null,
132+
user: {
133+
login: user.login,
134+
html_url: user.html_url,
135+
avatar_url: user.avatar_url,
136+
type: user.type,
137+
},
138+
};
136139
}
137140

138141
async function getGitifySubjectForDiscussion(
@@ -181,73 +184,65 @@ async function getGitifySubjectForIssue(
181184
notification: Notification,
182185
token: string,
183186
): Promise<GitifySubject> {
184-
try {
185-
const issue = (await getIssue(notification.subject.url, token)).data;
186-
187-
let issueCommentUser: User;
188-
189-
if (notification.subject.latest_comment_url) {
190-
const issueComment = (
191-
await getIssueOrPullRequestComment(
192-
notification.subject.latest_comment_url,
193-
token,
194-
)
195-
).data;
196-
issueCommentUser = issueComment.user;
197-
}
198-
199-
return {
200-
state: issue.state_reason ?? issue.state,
201-
user: {
202-
login: issueCommentUser?.login ?? issue.user.login,
203-
html_url: issueCommentUser?.html_url ?? issue.user.html_url,
204-
avatar_url: issueCommentUser?.avatar_url ?? issue.user.avatar_url,
205-
type: issueCommentUser?.type ?? issue.user.type,
206-
},
207-
};
208-
} catch (err) {
209-
console.error('Issue subject retrieval failed');
187+
const issue = (await getIssue(notification.subject.url, token)).data;
188+
189+
let issueCommentUser: User;
190+
191+
if (notification.subject.latest_comment_url) {
192+
const issueComment = (
193+
await getIssueOrPullRequestComment(
194+
notification.subject.latest_comment_url,
195+
token,
196+
)
197+
).data;
198+
issueCommentUser = issueComment.user;
210199
}
200+
201+
return {
202+
state: issue.state_reason ?? issue.state,
203+
user: {
204+
login: issueCommentUser?.login ?? issue.user.login,
205+
html_url: issueCommentUser?.html_url ?? issue.user.html_url,
206+
avatar_url: issueCommentUser?.avatar_url ?? issue.user.avatar_url,
207+
type: issueCommentUser?.type ?? issue.user.type,
208+
},
209+
};
211210
}
212211

213212
async function getGitifySubjectForPullRequest(
214213
notification: Notification,
215214
token: string,
216215
): Promise<GitifySubject> {
217-
try {
218-
const pr = (await getPullRequest(notification.subject.url, token)).data;
219-
220-
let prState: PullRequestStateType = pr.state;
221-
if (pr.merged) {
222-
prState = 'merged';
223-
} else if (pr.draft) {
224-
prState = 'draft';
225-
}
216+
const pr = (await getPullRequest(notification.subject.url, token)).data;
226217

227-
let prCommentUser: User;
218+
let prState: PullRequestStateType = pr.state;
219+
if (pr.merged) {
220+
prState = 'merged';
221+
} else if (pr.draft) {
222+
prState = 'draft';
223+
}
228224

229-
if (notification.subject.latest_comment_url) {
230-
const prComment = (
231-
await getIssueOrPullRequestComment(
232-
notification.subject.latest_comment_url,
233-
token,
234-
)
235-
).data;
236-
prCommentUser = prComment.user;
237-
}
225+
let prCommentUser: User;
238226

239-
return {
240-
state: prState,
241-
user: {
242-
login: prCommentUser?.login ?? pr.user.login,
243-
html_url: prCommentUser?.html_url ?? pr.user.html_url,
244-
avatar_url: prCommentUser?.avatar_url ?? pr.user.avatar_url,
245-
type: prCommentUser?.type ?? pr.user.type,
246-
},
247-
};
248-
} catch (err) {
249-
console.error('Pull Request subject retrieval failed');
227+
if (notification.subject.latest_comment_url) {
228+
const prComment = (
229+
await getIssueOrPullRequestComment(
230+
notification.subject.latest_comment_url,
231+
token,
232+
)
233+
).data;
234+
prCommentUser = prComment.user;
250235
}
236+
237+
return {
238+
state: prState,
239+
user: {
240+
login: prCommentUser?.login ?? pr.user.login,
241+
html_url: prCommentUser?.html_url ?? pr.user.html_url,
242+
avatar_url: prCommentUser?.avatar_url ?? pr.user.avatar_url,
243+
type: prCommentUser?.type ?? pr.user.type,
244+
},
245+
};
251246
}
252247

253248
async function getGitifySubjectForRelease(

0 commit comments

Comments
 (0)