Skip to content

Commit 53fc17e

Browse files
EbonsignoriCopilot
andauthored
[Sentry Auto-Fix] DOCS-G4: Error: Cannot set headers after they are sent to the client (#59285)
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: Ebonsignori <17055832+Ebonsignori@users.noreply.github.com>
1 parent 2849f8b commit 53fc17e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/search/middleware/search-routes.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import catchMiddlewareError from '@/observability/middleware/catch-middleware-er
1010
import { generalSearchRoute } from '@/search/lib/routes/general-search-route'
1111
import { aiSearchAutocompleteRoute } from '@/search/lib/routes/ai-search-autocomplete-route'
1212
import { combinedSearchRoute } from '@/search/lib/routes/combined-search-route'
13+
import { createLogger } from '@/observability/logger'
1314

15+
const logger = createLogger('search:middleware:search-routes')
1416
const router = express.Router()
1517

1618
router.get('/legacy', (req: Request, res: Response) => {
@@ -37,7 +39,16 @@ export async function handleGetSearchResultsError(
3739
const reports = FailBot.report(error, { url: req.url, ...options })
3840
if (reports) await Promise.all(reports)
3941
}
40-
res.status(500).json({ error: error.message })
42+
// Avoid "Cannot set headers after they are sent to the client" error
43+
// if response was already partially sent before the error occurred
44+
if (!res.headersSent) {
45+
res.status(500).json({ error: error.message })
46+
} else {
47+
logger.warn('Response headers already sent; unable to send error response.', {
48+
url: req.url,
49+
message: error?.message,
50+
})
51+
}
4152
}
4253

4354
// Redirects search routes to their latest versions

0 commit comments

Comments
 (0)