From a3a531b5961e0e6c5374e7561566d4c6aa507e38 Mon Sep 17 00:00:00 2001 From: Eduard Marbach Date: Sat, 5 Oct 2024 21:04:30 +0200 Subject: [PATCH 1/3] opt: enhance error message --- src/api.ts | 78 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/api.ts b/src/api.ts index e2f7895..6cda343 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,7 @@ import { Api as RadarrApi } from "./__generated__/generated-radarr-api"; import { Api as SonarrApi } from "./__generated__/generated-sonarr-api"; import { logger } from "./logger"; +import { ArrType } from "./types"; let sonarrClient: SonarrApi["api"] | undefined; let radarrClient: RadarrApi["api"] | undefined; @@ -28,9 +29,42 @@ export const getSonarrApi = () => { throw new Error("Please configure API first."); }; +const validateParams = (url: string, apiKey: string, arrType: ArrType) => { + const arrLabel = arrType === "RADARR" ? "Radarr" : "Sonarr"; + + if (!url) { + const message = `URL not correctly configured for ${arrLabel} API!`; + logger.error(message); + throw new Error(message); + } + if (!apiKey) { + const message = `API Key not correctly configured for ${arrLabel} API!`; + logger.error(message); + throw new Error(message); + } +}; + +const handleErrorApi = (error: any, arrType: ArrType) => { + let message; + const arrLabel = arrType === "RADARR" ? "Radarr" : "Sonarr"; + + error.message && logger.error(`Error configuring ${arrLabel} API: ${error.message}`); + + if (error.response) { + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + message = `Unable to retrieve data from ${arrLabel} API. Server responded with status code ${error.response.status}: ${error.response.statusText}. Please check the API server status or your request parameters.`; + } else { + // Something happened in setting up the request that triggered an Error + message = `An unexpected error occurred while setting up the ${arrLabel} request: ${error.message}. Please try again.`; + } + + throw new Error(message); +}; + export const configureSonarrApi = async (url: string, apiKey: string) => { - sonarrClient = undefined; - radarrClient = undefined; + unsetApi(); + validateParams(url, apiKey, "SONARR"); const api = new SonarrApi({ headers: { @@ -51,23 +85,7 @@ export const configureSonarrApi = async (url: string, apiKey: string) => { try { await sonarrClient.v3MetadataList(); } catch (error: any) { - let message; - - if (error.response) { - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - message = `Could not load from Sonarr API: Status ${error.response.status} - ${error.response.statusText}`; - } else if (error.request) { - // The request was made but no response was received - // `error.request` is an instance of XMLHttpRequest in the browser and an instance of - // http.ClientRequest in node.js - logger.error(error.request); - } else { - // Something happened in setting up the request that triggered an Error - logger.error("Error", error.message); - } - - throw new Error(message); + handleErrorApi(error, "SONARR"); } return sonarrClient; @@ -82,8 +100,8 @@ export const getRadarrpi = () => { }; export const configureRadarrApi = async (url: string, apiKey: string) => { - sonarrClient = undefined; - radarrClient = undefined; + unsetApi(); + validateParams(url, apiKey, "RADARR"); const api = new RadarrApi({ headers: { @@ -104,23 +122,7 @@ export const configureRadarrApi = async (url: string, apiKey: string) => { try { await radarrClient.v3MetadataList(); } catch (error: any) { - let message; - - if (error.response) { - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - message = `Could not load from Radarr API: Status ${error.response.status} - ${error.response.statusText}`; - } else if (error.request) { - // The request was made but no response was received - // `error.request` is an instance of XMLHttpRequest in the browser and an instance of - // http.ClientRequest in node.js - logger.error(error.request); - } else { - // Something happened in setting up the request that triggered an Error - logger.error("Error", error.message); - } - - throw new Error(message); + handleErrorApi(error, "RADARR"); } return radarrClient; From 02462ddec0b662aac77dfb1cff819fa49dae6ecd Mon Sep 17 00:00:00 2001 From: Eduard Marbach Date: Sat, 5 Oct 2024 21:24:45 +0200 Subject: [PATCH 2/3] refactor: disable enforced coloring --- src/logger.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index dad9144..aef2b38 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -6,9 +6,6 @@ export const logger = pino({ level: LOG_LEVEL, transport: { target: "pino-pretty", - options: { - colorize: true, - }, }, }); From 05437fb7e49769df7633607da465cc31dc134f40 Mon Sep 17 00:00:00 2001 From: Eduard Marbach Date: Mon, 7 Oct 2024 12:54:48 +0200 Subject: [PATCH 3/3] opt: try adding support for custom UID/GID executions --- Dockerfile | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index dd2421b..f88fcc2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,24 @@ # https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md FROM node:22.9.0-slim AS base -ENV PNPM_HOME="/pnpm" +ENV PNPM_HOME="/opt/pnpm" +ENV COREPACK_HOME="/opt/corepack" ENV PATH="$PNPM_HOME:$PATH" RUN apt-get update && apt-get install -y \ git \ - && rm -rf /var/lib/apt/lists/* \ - && corepack enable \ - && corepack prepare pnpm@9 --activate + && rm -rf /var/lib/apt/lists/* WORKDIR /app COPY package.json pnpm-lock.yaml /app/ +# Do it here to add the packageManager field to the package.json +RUN corepack enable \ + && corepack prepare pnpm@9 --activate \ + && corepack use pnpm@9 + FROM base AS prod-deps RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile FROM base -RUN corepack use pnpm@9 COPY --from=prod-deps /app/node_modules /app/node_modules COPY index.ts /app/ COPY src/ /app/src/ @@ -23,6 +26,8 @@ COPY src/ /app/src/ ENV CONFIG_LOCATION=/app/config/config.yml ENV SECRETS_LOCATION=/app/config/secrets.yml +RUN chmod uga+rw -R /app/package.json + #USER node CMD [ "pnpm", "start" ]