export async function GetProfile(context: RouterContext): Promise<void> {
let timeoutId = 0
try {
//request time out
timeoutId = setTimeout(() => {
if (context.response.writable) {
context.throw(408) // <-- równoznaczne z "throw new Error(...)"
}
}, RequestTimeout)
const searchParams = new URLSearchParams(context.request.url.search.substring(1))
const profile = searchParams.get("profileid")
const count = searchParams.get("count")
//check if arguments are provided
if (profile === undefined || count === undefined) {
context.throw(418)
return
}
//
// { ... }
//
if (cached === null) {
context.throw(404)
return
}
context.response.headers.set("Access-Control-Allow-Origin", "*")
context.response.type = "application/json"
context.response.body = cached
return
}
finally {
clearTimeout(timeoutId)
}
}
Czego bym nie wyrzucił w setTimeout(...) to cała aplikacja się kładzie, wszystkie wyjątki wyrzucane bezpośrednio w GetProfile(...) są łapane przez "Oak" framework, niestety oak nie udostępnia żadnej uniwersalnej metody do obsługi timeoutów