Skip to content

Commit a7aa335

Browse files
fix: Add defensive coding and error handling to domain processing loop (ENG-858)
Co-Authored-By: Nathan Clevenger <[email protected]>
1 parent 0b2d082 commit a7aa335

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

app/(apis)/route.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,42 @@ export const GET = API(async (request, { db, user, origin, url, domain, payload
7878
}
7979

8080
for (const d of filteredDomains) {
81-
if (d.endsWith('.do')) {
82-
let isInCategory = false
83-
for (const sites of Object.values(siteCategories)) {
84-
if (sites.includes(d)) {
85-
isInCategory = true
86-
break
87-
}
88-
}
89-
90-
if (!isInCategory) {
91-
const siteName = d.replace('.do', '')
92-
const description = getDomainDescription(d) || ''
93-
const siteTitle = `${titleCase(siteName)}${description ? ` - ${description}` : ''}`
94-
95-
let category = 'Other'
81+
if (typeof d !== 'string') {
82+
console.error(`Invalid domain encountered: ${d}`, typeof d)
83+
continue
84+
}
9685

97-
if (collectionSlugs.includes(siteName)) {
98-
category = 'Collections'
99-
if (!formattedSites['Collections']) {
100-
formattedSites['Collections'] = {}
86+
if (d.endsWith('.do')) {
87+
try {
88+
let isInCategory = false
89+
for (const sites of Object.values(siteCategories)) {
90+
if (sites.includes(d)) {
91+
isInCategory = true
92+
break
10193
}
10294
}
10395

104-
if (!formattedSites[category]) {
105-
formattedSites[category] = {}
96+
if (!isInCategory) {
97+
const siteName = d.replace('.do', '')
98+
const description = getDomainDescription(d) || ''
99+
const siteTitle = `${titleCase(siteName)}${description ? ` - ${description}` : ''}`
100+
101+
let category = 'Other'
102+
103+
if (collectionSlugs.includes(siteName)) {
104+
category = 'Collections'
105+
if (!formattedSites['Collections']) {
106+
formattedSites['Collections'] = {}
107+
}
108+
}
109+
110+
if (!formattedSites[category]) {
111+
formattedSites[category] = {}
112+
}
113+
formattedSites[category][siteTitle] = formatWithOptions(`sites/${siteName}`, d)
106114
}
107-
formattedSites[category][siteTitle] = formatWithOptions(`sites/${siteName}`, d)
115+
} catch (error) {
116+
console.error(`Error processing domain ${d}:`, error)
108117
}
109118
}
110119
}

0 commit comments

Comments
 (0)