small fix & better error handling for api requests

This commit is contained in:
Darius
2025-11-17 13:57:08 +01:00
parent ff22377c83
commit d2d0d934c2
4 changed files with 66 additions and 30 deletions

View File

@@ -1,6 +1,11 @@
// Folder-specific settings
//
// For a full list of overridable settings, and general information on folder-specific settings,
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
{ {
"code_actions_on_format": { "code_actions_on_format": {
"source.organizeImports.biome": true, "source.organizeImports": true,
"source.fixAll.biome": true "source.fixAll.eslint": true
} },
"format_on_save": "on"
} }

View File

@@ -1,5 +1,6 @@
import axios from "axios"; import axios from "axios";
import { Config } from "../config/config.js"; import { Config } from "../config/config.js";
import { printNetworkError } from "./general.js";
import { logWarning } from "./logger.js"; import { logWarning } from "./logger.js";
type HomeAssistantEntity = { type HomeAssistantEntity = {
@@ -53,10 +54,10 @@ export async function getDeskPosition(): Promise<DeskPositionResult | null> {
raw, raw,
asBoolean: position === 1, asBoolean: position === 1,
asText: () => { asText: () => {
if (position === 1) return 'standing'; if (position === 1) return "standing";
if (position === 0) return 'sitting'; if (position === 0) return "sitting";
return 'unknown'; return "unknown";
} },
}; };
} }
return null; return null;
@@ -87,28 +88,33 @@ export async function getTemperatures(): Promise<Array<HomeAssistantEntity>> {
async function sendRequestToHomeAssistantStates( async function sendRequestToHomeAssistantStates(
entity_id: string, entity_id: string,
): Promise<HomeAssistantEntity> { ): Promise<HomeAssistantEntity> {
const url = `${Config.homeassistant.api_url}states/"${entity_id}` try {
//logInfo(`sending request to ${url}`) const url = `${Config.homeassistant.api_url}states/${entity_id}`;
const response = await axios.get<HomeAssistantEntity>( const response = await axios.get<HomeAssistantEntity>(url, {
url,
{
headers: { headers: {
Authorization: `Bearer ${Config.homeassistant.api_token}`, Authorization: `Bearer ${Config.homeassistant.api_token}`,
}, },
}, });
);
return response.data; return response.data;
} catch (error) {
printNetworkError(error);
throw error;
}
} }
async function sendRequestToHomeAssistantWebhook( async function sendRequestToHomeAssistantWebhook(
webhook_id: string, webhook_id: string,
): Promise<unknown> { ): Promise<unknown> {
const url = `${Config.homeassistant.api_url}webhook/${webhook_id}` try {
//logInfo(`sending request to ${url}`) const url = `${Config.homeassistant.api_url}webhook/${webhook_id}`;
const response = await axios.post<HomeAssistantEntity>(url); const response = await axios.post<HomeAssistantEntity>(url);
return response.data; return response.data;
} catch (error) {
printNetworkError(error);
throw error;
}
} }

View File

@@ -1,5 +1,6 @@
import axios from "axios"; import axios from "axios";
import { Config } from "../config/config.js"; import { Config } from "../config/config.js";
import { printNetworkError } from "./general.js";
import { logWarning } from "./logger.js"; import { logWarning } from "./logger.js";
type Song = { type Song = {
@@ -61,21 +62,30 @@ export async function setVolumeToTidal(volume: number): Promise<Volume | null> {
} }
async function sendGetRequestToTidal<T>(endpoint: string): Promise<T> { async function sendGetRequestToTidal<T>(endpoint: string): Promise<T> {
const response = await axios.get<T>( try {
`${Config.tidal.host}:${Config.tidal.port}/${endpoint}`, const url = `${Config.tidal.host}:${Config.tidal.port}/${endpoint}`;
);
return response.data; const response = await axios.get<T>(url);
return response.data;
} catch (error) {
printNetworkError(error);
throw error;
}
} }
async function sendPostRequestToTidal<T>( async function sendPostRequestToTidal<T>(
endpoint: string, endpoint: string,
data: unknown, data: unknown,
): Promise<T> { ): Promise<T> {
const response = await axios.post<T>( try {
`${Config.tidal.host}:${Config.tidal.port}/${endpoint}`, const url = `${Config.tidal.host}:${Config.tidal.port}/${endpoint}`;
data,
);
return response.data; const response = await axios.post<T>(url, data);
return response.data;
} catch (error) {
printNetworkError(error);
throw error;
}
} }

View File

@@ -1,7 +1,8 @@
import * as readline from "node:readline";
import type { UserIdResolvable } from "@twurple/api"; import type { UserIdResolvable } from "@twurple/api";
import axios from "axios";
import * as readline from "node:readline";
import { apiClient } from "../core/client.js"; import { apiClient } from "../core/client.js";
import { logSuccess, logWarning } from "./logger.js"; import { logError, logSuccess, logWarning } from "./logger.js";
export async function getUserId(username: string): Promise<UserIdResolvable> { export async function getUserId(username: string): Promise<UserIdResolvable> {
const user = await apiClient.users.getUserByName(username); const user = await apiClient.users.getUserByName(username);
@@ -62,3 +63,17 @@ export function secondsToReadable(
"", "",
); );
} }
export function printNetworkError(error: unknown) {
if (axios.isAxiosError(error)) {
logError("Axios error details:", {
message: error.message,
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
url: error.config?.url,
});
} else {
logError("Unexpected error:", error);
}
}