Skip to content

Commit 0e1c1f9

Browse files
committed
Alert instead of throwing exception
1 parent 5dac400 commit 0e1c1f9

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"start": "yarn backend",
1212
"backend": "node ./src/backend",
1313
"notifier": "node ./src/notifier",
14-
"scraper": "node ./src/scraper",
14+
"scraper": "node --inspect ./src/scraper",
1515
"lint": "eslint . && prettier -c .",
1616
"test": "yarn jest --coverage src"
1717
},

src/common/__tests__/getTeams.spec.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const matches = [
2121
},
2222
]
2323

24-
describe('getTeams', () => {
24+
describe.only('getTeams', () => {
2525
test('getTeams', () => {
26-
expect(getTeams(matches)).toMatchInlineSnapshot(`
26+
expect(getTeams({ matches })).toMatchInlineSnapshot(`
2727
Array [
2828
"hellraisers",
2929
"melbet/extremum loser",
@@ -36,7 +36,9 @@ describe('getTeams', () => {
3636
]
3737
`)
3838
})
39-
test('throws exception', () => {
40-
expect(() => getTeams([{ title: 'TBDvs' }])).toThrow()
39+
test('invalid input returns empty array', () => {
40+
const matches = [{ title: 'TBDvs' }]
41+
42+
expect(getTeams({ matches })).toEqual([])
4143
})
4244
})

src/common/getTeams.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
const getMatchTeams = (match) => {
1+
function getMatchTeams({ alerter, match }) {
22
const { title } = match
33
const teams = title.toLowerCase().split(' vs ')
44

55
if (2 !== teams.length) {
6-
throw new Error(`Failed to get teams from '${title}'; teams=${teams}`)
6+
alerter?.warn(
7+
`Failed to get teams from '${title}'; teams=${teams}; match=${JSON.stringify(
8+
match,
9+
null,
10+
2,
11+
)}`,
12+
)
13+
}
14+
15+
if (1 === teams.length) {
16+
return []
717
}
818

919
return teams
1020
}
1121

12-
const getTeams = (matches) => {
13-
const teams = matches.map(getMatchTeams).flat(Infinity)
22+
function getTeams({ alerter, matches }) {
23+
const teams = matches
24+
.map((match) => getMatchTeams({ alerter, match }))
25+
.flat(Infinity)
1426

1527
return [...new Set(teams)]
1628
}

src/notifier/makeRawMessages.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const getFavoriteTeamsMatchesMessage = (
4646

4747
const matches = todayMatches
4848
.filter((match) => {
49-
const teams = getMatchTeams(match)
49+
// TODO: pass alerter
50+
const teams = getMatchTeams({ match })
5051
const isFavoriteTeamMatch =
5152
0 !== teams.filter((team) => favorites.includes(team)).length
5253

src/scraper/scraper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function scrap() {
4444
.then(() => axios(url))
4545
.then(({ data }) => {
4646
const matches = htmlToMatches(data, { root })
47-
const teams = getTeams(matches)
47+
const teams = getTeams({ alerter, matches })
4848

4949
const addTeamsPromise = addTeams(teams).catch(alerter.error)
5050
const saveMatchesPromise = saveFeaturedMatches(matches)

0 commit comments

Comments
 (0)