Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: enhance error message #80

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# 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/

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" ]
78 changes: 40 additions & 38 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>["api"] | undefined;
let radarrClient: RadarrApi<unknown>["api"] | undefined;
Expand Down Expand Up @@ -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: {
Expand All @@ -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;
Expand All @@ -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: {
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export const logger = pino({
level: LOG_LEVEL,
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});

Expand Down