fix: handle a case when headscale is unreachable
This commit is contained in:
parent
27f310fb35
commit
8148e242dc
@ -8,8 +8,16 @@ export class HeadscaleError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class FatalError extends Error {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message)
|
||||||
|
this.name = 'FatalError'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||||
export async function pull<T>(url: string, key: string) {
|
export async function pull<T>(url: string, key: string) {
|
||||||
|
try {
|
||||||
const prefix = process.env.HEADSCALE_URL!
|
const prefix = process.env.HEADSCALE_URL!
|
||||||
const response = await fetch(`${prefix}/api/${url}`, {
|
const response = await fetch(`${prefix}/api/${url}`, {
|
||||||
headers: {
|
headers: {
|
||||||
@ -21,10 +29,14 @@ export async function pull<T>(url: string, key: string) {
|
|||||||
throw new HeadscaleError(await response.text(), response.status)
|
throw new HeadscaleError(await response.text(), response.status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json() as Promise<T>
|
return await (response.json() as Promise<T>)
|
||||||
|
} catch {
|
||||||
|
throw new FatalError('The Headscale server is not reachable')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function post<T>(url: string, key: string, body?: unknown) {
|
export async function post<T>(url: string, key: string, body?: unknown) {
|
||||||
|
try {
|
||||||
const prefix = process.env.HEADSCALE_URL!
|
const prefix = process.env.HEADSCALE_URL!
|
||||||
const response = await fetch(`${prefix}/api/${url}`, {
|
const response = await fetch(`${prefix}/api/${url}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -38,6 +50,9 @@ export async function post<T>(url: string, key: string, body?: unknown) {
|
|||||||
throw new HeadscaleError(await response.text(), response.status)
|
throw new HeadscaleError(await response.text(), response.status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json() as Promise<T>
|
return await (response.json() as Promise<T>)
|
||||||
|
} catch {
|
||||||
|
throw new FatalError('The Headscale server is not reachable')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user