Let the enduser handle not found errors

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
2021-01-08 15:20:19 +01:00
parent b180369514
commit c4fff9b370
2 changed files with 36 additions and 26 deletions

View File

@@ -23,7 +23,7 @@ export class Request<T = any> {
this.url = url
}
public async get(): Promise<T> {
public async get(): Promise<T | undefined> {
const now = new Date()
if (
this.fetched &&
@@ -34,22 +34,17 @@ export class Request<T = any> {
}
// Fetch Response
try {
const resp = await fetch(this.url, {
headers: {
"Content-Type": "text/plain"
}
})
if (resp.status !== 200) {
throw new Error(`Error request ended with the code (${resp.status})`)
const resp = await fetch(this.url, {
headers: {
"Content-Type": "text/plain"
}
const response = await resp.json()
this.response = response
this.fetched = now
return response
} catch (e) {
console.error(e)
throw new Error('An error occured')
})
if (resp.status !== 200) {
return undefined
}
const response = await resp.json()
this.response = response
this.fetched = now
return response
}
}