From d1fc44be60c31374eaae005bece43376e139f491 Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 11 Jul 2024 16:51:59 +0200 Subject: [PATCH] User the good error type --- .../src/services/homeAssistantService.ts | 6 ++--- .../backend/src/services/webdavService.ts | 26 +++++++------------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/nextcloud_backup/backend/src/services/homeAssistantService.ts b/nextcloud_backup/backend/src/services/homeAssistantService.ts index 258f524..f187b74 100644 --- a/nextcloud_backup/backend/src/services/homeAssistantService.ts +++ b/nextcloud_backup/backend/src/services/homeAssistantService.ts @@ -72,7 +72,7 @@ function getAddonList(): Promise>> { }); return result; }, - (error: Error) => { + (error: RequestError) => { messageManager.error("Fail to fetch addons list", error?.message); logger.error(`Fail to fetch addons list (${error?.message})`); logger.error(error); @@ -97,7 +97,7 @@ function getBackups(): Promise>> { statusTools.setStatus(status); return result; }, - (error: Error) => { + (error: RequestError) => { const status = statusTools.getStatus(); status.hass.ok = false; status.hass.last_check = DateTime.now(); @@ -146,7 +146,7 @@ function downloadSnapshot(id: string): Promise { ); return tmp_file; }, - (reason: Error) => { + (reason: RequestError) => { fs.unlinkSync(tmp_file); messageManager.error( "Fail to download Home Assistant backup", diff --git a/nextcloud_backup/backend/src/services/webdavService.ts b/nextcloud_backup/backend/src/services/webdavService.ts index 3b45af3..caa75ca 100644 --- a/nextcloud_backup/backend/src/services/webdavService.ts +++ b/nextcloud_backup/backend/src/services/webdavService.ts @@ -2,6 +2,7 @@ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ +import { randomUUID } from "crypto"; import { XMLParser } from "fast-xml-parser"; import fs from "fs"; import got, { @@ -17,10 +18,9 @@ import * as pathTools from "../tools/pathTools.js"; import * as statusTools from "../tools/status.js"; import type { WebdavBackup } from "../types/services/webdav.js"; import type { WebdavConfig } from "../types/services/webdavConfig.js"; +import { States } from "../types/status.js"; import { templateToRegexp } from "./backupConfigService.js"; import { getChunkEndpoint, getEndpoint } from "./webdavConfigService.js"; -import { States } from "../types/status.js"; -import { randomUUID } from "crypto"; const CHUNK_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client const CHUNK_NUMBER_SIZE = 5; // To add landing "0" @@ -56,12 +56,9 @@ export function checkWebdavLogin( status.webdav.last_check = DateTime.now(); return response; }, - (reason) => { + (reason: RequestError) => { if (!silent) { - messageManager.error( - "Fail to connect to Webdav", - (reason as Error).message - ); + messageManager.error("Fail to connect to Webdav", reason.message); } const status = statusTools.getStatus(); status.webdav = { @@ -72,7 +69,7 @@ export function checkWebdavLogin( statusTools.setStatus(status); logger.error(`Fail to connect to Webdav`); logger.error(reason); - return Promise.reject(reason as Error); + return Promise.reject(reason); } ); } @@ -166,13 +163,13 @@ export function getBackups( ); return extractBackupInfo(data, nameTemplate); }, - (reason) => { + (reason: RequestError) => { messageManager.error( `Fail to retrive webdav backups in ${folder} folder` ); logger.error(`Fail to retrive webdav backups in ${folder} folder`); logger.error(reason); - return Promise.reject(reason as Error); + return Promise.reject(reason); } ); } @@ -218,14 +215,11 @@ export function deleteBackup(path: string, config: WebdavConfig) { (response) => { return response; }, - (reason) => { - messageManager.error( - "Fail to delete backup in webdav", - (reason as Error)?.message - ); + (reason: RequestError) => { + messageManager.error("Fail to delete backup in webdav", reason.message); logger.error(`Fail to delete backup in Cloud`); logger.error(reason); - return Promise.reject(reason as Error); + return Promise.reject(reason); } ); }