User the good error type

This commit is contained in:
SebClem 2024-07-11 16:51:59 +02:00
parent bdb5148e0b
commit d1fc44be60
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
2 changed files with 13 additions and 19 deletions

View File

@ -72,7 +72,7 @@ function getAddonList(): Promise<Response<SupervisorResponse<AddonData>>> {
}); });
return result; return result;
}, },
(error: Error) => { (error: RequestError) => {
messageManager.error("Fail to fetch addons list", error?.message); messageManager.error("Fail to fetch addons list", error?.message);
logger.error(`Fail to fetch addons list (${error?.message})`); logger.error(`Fail to fetch addons list (${error?.message})`);
logger.error(error); logger.error(error);
@ -97,7 +97,7 @@ function getBackups(): Promise<Response<SupervisorResponse<BackupData>>> {
statusTools.setStatus(status); statusTools.setStatus(status);
return result; return result;
}, },
(error: Error) => { (error: RequestError) => {
const status = statusTools.getStatus(); const status = statusTools.getStatus();
status.hass.ok = false; status.hass.ok = false;
status.hass.last_check = DateTime.now(); status.hass.last_check = DateTime.now();
@ -146,7 +146,7 @@ function downloadSnapshot(id: string): Promise<string> {
); );
return tmp_file; return tmp_file;
}, },
(reason: Error) => { (reason: RequestError) => {
fs.unlinkSync(tmp_file); fs.unlinkSync(tmp_file);
messageManager.error( messageManager.error(
"Fail to download Home Assistant backup", "Fail to download Home Assistant backup",

View File

@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { randomUUID } from "crypto";
import { XMLParser } from "fast-xml-parser"; import { XMLParser } from "fast-xml-parser";
import fs from "fs"; import fs from "fs";
import got, { import got, {
@ -17,10 +18,9 @@ import * as pathTools from "../tools/pathTools.js";
import * as statusTools from "../tools/status.js"; import * as statusTools from "../tools/status.js";
import type { WebdavBackup } from "../types/services/webdav.js"; import type { WebdavBackup } from "../types/services/webdav.js";
import type { WebdavConfig } from "../types/services/webdavConfig.js"; import type { WebdavConfig } from "../types/services/webdavConfig.js";
import { States } from "../types/status.js";
import { templateToRegexp } from "./backupConfigService.js"; import { templateToRegexp } from "./backupConfigService.js";
import { getChunkEndpoint, getEndpoint } from "./webdavConfigService.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_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client
const CHUNK_NUMBER_SIZE = 5; // To add landing "0" const CHUNK_NUMBER_SIZE = 5; // To add landing "0"
@ -56,12 +56,9 @@ export function checkWebdavLogin(
status.webdav.last_check = DateTime.now(); status.webdav.last_check = DateTime.now();
return response; return response;
}, },
(reason) => { (reason: RequestError) => {
if (!silent) { if (!silent) {
messageManager.error( messageManager.error("Fail to connect to Webdav", reason.message);
"Fail to connect to Webdav",
(reason as Error).message
);
} }
const status = statusTools.getStatus(); const status = statusTools.getStatus();
status.webdav = { status.webdav = {
@ -72,7 +69,7 @@ export function checkWebdavLogin(
statusTools.setStatus(status); statusTools.setStatus(status);
logger.error(`Fail to connect to Webdav`); logger.error(`Fail to connect to Webdav`);
logger.error(reason); logger.error(reason);
return Promise.reject(reason as Error); return Promise.reject(reason);
} }
); );
} }
@ -166,13 +163,13 @@ export function getBackups(
); );
return extractBackupInfo(data, nameTemplate); return extractBackupInfo(data, nameTemplate);
}, },
(reason) => { (reason: RequestError) => {
messageManager.error( messageManager.error(
`Fail to retrive webdav backups in ${folder} folder` `Fail to retrive webdav backups in ${folder} folder`
); );
logger.error(`Fail to retrive webdav backups in ${folder} folder`); logger.error(`Fail to retrive webdav backups in ${folder} folder`);
logger.error(reason); 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) => { (response) => {
return response; return response;
}, },
(reason) => { (reason: RequestError) => {
messageManager.error( messageManager.error("Fail to delete backup in webdav", reason.message);
"Fail to delete backup in webdav",
(reason as Error)?.message
);
logger.error(`Fail to delete backup in Cloud`); logger.error(`Fail to delete backup in Cloud`);
logger.error(reason); logger.error(reason);
return Promise.reject(reason as Error); return Promise.reject(reason);
} }
); );
} }