Skip to content

Commit 54e04f1

Browse files
authored
Merge pull request Sofie-Automation#302 from Sofie-Automation/fix/fetch-error-message
fix: improve error message thrown from fetch
2 parents 95d6c02 + 31c9f59 commit 54e04f1

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

  • shared/packages/worker/src/worker/accessorHandlers/lib

shared/packages/worker/src/worker/accessorHandlers/lib/fetch.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export function fetchWithController(
7070
): { response: Promise<Response>; controller: AbortController } {
7171
const controller = new AbortController()
7272

73+
const tmpError = new Error() // Used later in troubleshooting
74+
7375
return {
7476
response: new Promise((resolve, reject) => {
7577
const refreshTimeout = () => {
@@ -112,13 +114,29 @@ export function fetchWithController(
112114
// We should try again once as it often is a temporary issue:
113115

114116
doTheFetch().catch((err) => {
115-
reject(err)
117+
reject(handleError(err, tmpError.stack, url, options?.method))
116118
})
117119
} else {
118-
reject(err)
120+
reject(handleError(err, tmpError.stack, url, options?.method))
119121
}
120122
})
121123
}),
122124
controller,
123125
}
124126
}
127+
function handleError(err: unknown, orgStack: string | undefined, url: string, method?: string) {
128+
// Improve errors with the url and method info, and make sure the stack trace is preserved:
129+
if (err instanceof Error) {
130+
err.message = `Error when fetching ${method ?? ''} "${url}": ${err.message}`
131+
132+
const stackLines: string[] = [
133+
...(err.stack?.split('\n').slice(1) ?? []),
134+
'Original stack:',
135+
...(orgStack?.split('\n') ?? []),
136+
]
137+
err.stack = `${err.name}: ${err.message}\n${stackLines.join('\n')}`
138+
return err
139+
} else {
140+
return err
141+
}
142+
}

0 commit comments

Comments
 (0)