fix: handle auth and server error during swap (#593)
* fix: change execution order for light node upgrade * refactor: grab new configuration from post config request * fix: only print successful light node upgrade when it really happens * fix: log full desktop side swap error (#596) * refactor: try to make the auth error in swap nicer * refactor: make error instruction consistent * fix: avoid overwriting daiToSwap when it is set manually
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
export class AuthError extends Error {
|
||||
constructor() {
|
||||
super('Bad API key')
|
||||
this.name = 'AuthError'
|
||||
}
|
||||
}
|
||||
|
||||
export function isAuthError(error: unknown): error is AuthError {
|
||||
return error instanceof Error && error.name === 'AuthError'
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { isAuthError } from './AuthError'
|
||||
|
||||
export class SwapError extends Error {
|
||||
snackbarMessage: string
|
||||
originalError?: Error
|
||||
@@ -16,6 +18,9 @@ export function isSwapError(error: unknown): error is SwapError {
|
||||
|
||||
export function wrapWithSwapError<T>(promise: Promise<T>, snackbarMessage: string): Promise<T> {
|
||||
return promise.catch((error: Error) => {
|
||||
if (isAuthError(error)) {
|
||||
throw new SwapError('Bad API key, reopen dashboard through Swarm Desktop', error)
|
||||
}
|
||||
throw new SwapError(snackbarMessage, error)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ export async function getBzzPriceAsDai(desktopUrl: string): Promise<Token> {
|
||||
return DaiToken.fromDecimal(response.data)
|
||||
}
|
||||
|
||||
export async function upgradeToLightNode(desktopUrl: string, rpcProvider: string): Promise<void> {
|
||||
await updateDesktopConfiguration(desktopUrl, {
|
||||
export function upgradeToLightNode(desktopUrl: string, rpcProvider: string): Promise<BeeConfig> {
|
||||
return updateDesktopConfiguration(desktopUrl, {
|
||||
'swap-enable': true,
|
||||
'swap-endpoint': rpcProvider,
|
||||
})
|
||||
@@ -43,8 +43,8 @@ export function getDesktopConfiguration(desktopUrl: string): Promise<BeeConfig>
|
||||
return getJson(`${desktopUrl}/config`)
|
||||
}
|
||||
|
||||
async function updateDesktopConfiguration(desktopUrl: string, values: Record<string, unknown>): Promise<void> {
|
||||
await postJson(`${desktopUrl}/config`, values)
|
||||
function updateDesktopConfiguration(desktopUrl: string, values: Record<string, unknown>): Promise<BeeConfig> {
|
||||
return postJson(`${desktopUrl}/config`, values)
|
||||
}
|
||||
|
||||
export async function restartBeeNode(desktopUrl: string): Promise<void> {
|
||||
|
||||
+11
-1
@@ -1,12 +1,13 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import axios from 'axios'
|
||||
import { AuthError } from './AuthError'
|
||||
|
||||
export function getJson<T extends Record<string, any>>(url: string): Promise<T> {
|
||||
return sendRequest(url, 'GET') as Promise<T>
|
||||
}
|
||||
|
||||
export function postJson<T extends Record<string, any>>(url: string, data?: T): Promise<T> {
|
||||
export function postJson<T extends Record<string, any>>(url: string, data?: Record<string, any>): Promise<T> {
|
||||
return sendRequest(url, 'POST', data) as Promise<T>
|
||||
}
|
||||
|
||||
@@ -27,6 +28,15 @@ export async function sendRequest(
|
||||
method,
|
||||
headers,
|
||||
data,
|
||||
}).catch(error => {
|
||||
if (error?.response?.status === 401) {
|
||||
throw new AuthError()
|
||||
}
|
||||
|
||||
if (error?.response?.data) {
|
||||
throw Error(`Request ${method} ${url} failed: ${JSON.stringify(error.response.data)}`)
|
||||
}
|
||||
throw error
|
||||
})
|
||||
|
||||
return response.data
|
||||
|
||||
Reference in New Issue
Block a user