small fix & better error handling for api requests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import axios from "axios";
|
||||
import { Config } from "../config/config.js";
|
||||
import { printNetworkError } from "./general.js";
|
||||
import { logWarning } from "./logger.js";
|
||||
|
||||
type HomeAssistantEntity = {
|
||||
@@ -53,10 +54,10 @@ export async function getDeskPosition(): Promise<DeskPositionResult | null> {
|
||||
raw,
|
||||
asBoolean: position === 1,
|
||||
asText: () => {
|
||||
if (position === 1) return 'standing';
|
||||
if (position === 0) return 'sitting';
|
||||
return 'unknown';
|
||||
}
|
||||
if (position === 1) return "standing";
|
||||
if (position === 0) return "sitting";
|
||||
return "unknown";
|
||||
},
|
||||
};
|
||||
}
|
||||
return null;
|
||||
@@ -87,28 +88,33 @@ export async function getTemperatures(): Promise<Array<HomeAssistantEntity>> {
|
||||
async function sendRequestToHomeAssistantStates(
|
||||
entity_id: string,
|
||||
): Promise<HomeAssistantEntity> {
|
||||
const url = `${Config.homeassistant.api_url}states/"${entity_id}`
|
||||
//logInfo(`sending request to ${url}`)
|
||||
try {
|
||||
const url = `${Config.homeassistant.api_url}states/${entity_id}`;
|
||||
|
||||
const response = await axios.get<HomeAssistantEntity>(
|
||||
url,
|
||||
{
|
||||
const response = await axios.get<HomeAssistantEntity>(url, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${Config.homeassistant.api_token}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
return response.data;
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function sendRequestToHomeAssistantWebhook(
|
||||
webhook_id: string,
|
||||
): Promise<unknown> {
|
||||
const url = `${Config.homeassistant.api_url}webhook/${webhook_id}`
|
||||
//logInfo(`sending request to ${url}`)
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import axios from "axios";
|
||||
import { Config } from "../config/config.js";
|
||||
import { printNetworkError } from "./general.js";
|
||||
import { logWarning } from "./logger.js";
|
||||
|
||||
type Song = {
|
||||
@@ -61,21 +62,30 @@ export async function setVolumeToTidal(volume: number): Promise<Volume | null> {
|
||||
}
|
||||
|
||||
async function sendGetRequestToTidal<T>(endpoint: string): Promise<T> {
|
||||
const response = await axios.get<T>(
|
||||
`${Config.tidal.host}:${Config.tidal.port}/${endpoint}`,
|
||||
);
|
||||
try {
|
||||
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>(
|
||||
endpoint: string,
|
||||
data: unknown,
|
||||
): Promise<T> {
|
||||
const response = await axios.post<T>(
|
||||
`${Config.tidal.host}:${Config.tidal.port}/${endpoint}`,
|
||||
data,
|
||||
);
|
||||
try {
|
||||
const url = `${Config.tidal.host}:${Config.tidal.port}/${endpoint}`;
|
||||
|
||||
return response.data;
|
||||
const response = await axios.post<T>(url, data);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import * as readline from "node:readline";
|
||||
import type { UserIdResolvable } from "@twurple/api";
|
||||
import axios from "axios";
|
||||
import * as readline from "node:readline";
|
||||
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> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user