Skip to content

Commit

Permalink
opt: improve error handling of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Oct 28, 2024
1 parent 80e0204 commit af4a500
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 68 deletions.
55 changes: 6 additions & 49 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "dotenv/config";

import { HTTPError } from "ky";
import fs from "node:fs";
import { MergedCustomFormatResource } from "./src/__generated__/mergedTypes";
import { configureRadarrApi, configureSonarrApi, getArrApi, unsetApi } from "./src/api";
Expand Down Expand Up @@ -207,63 +206,21 @@ const pipeline = async (value: ConfigArrInstance, arrType: ArrType) => {
if (!IS_DRY_RUN) {
for (const element of create) {
try {
const newProfile = await api.v3QualityprofileCreate(element as any).json(); // Ignore types
const newProfile = await api.v3QualityprofileCreate(element as any); // Ignore types
logger.info(`Created QualityProfile: ${newProfile.name}`);
} catch (error: any) {
let message;

logger.debug(`Error creating QualityProfile: ${error?.name}`);

if (error instanceof HTTPError) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const errorJson = await error.response.json();
logger.error(errorJson, `Failed creating QualityProfile (${element.name})`);
message = `Failed creating QualityProfile (${element.name}): Data ${JSON.stringify(errorJson)}`;
} 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
const errorJson = await error.request.json();
logger.error(errorJson, `Failed creating QualityProfile (${element.name}) during request (probably some connection errors)`);
message = `Failed creating QualityProfile (${element.name}): Data ${JSON.stringify(errorJson)}`;
} else {
// Something happened in setting up the request that triggered an Error
logger.error("Error", error.message);
}
} else {
message = `An not expected error happened. Feel free to open an issue with details to improve handling.`;
logger.error(message);
throw error;
}

throw new Error(message);
logger.error(`Failed creating QualityProfile (${element.name})`);
throw error;
}
}

for (const element of changedQPs) {
try {
const newProfile = await api.v3QualityprofileUpdate("" + element.id, element as any).json(); // Ignore types
const newProfile = await api.v3QualityprofileUpdate("" + element.id, element as any); // Ignore types
logger.info(`Updated QualityProfile: ${newProfile.name}`);
} 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 = `Failed updating QualityProfile (${element.name}): Data ${JSON.stringify(error.response.data)}`;
} 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.info(error.request);
} else {
// Something happened in setting up the request that triggered an Error
logger.info("Error", error.message);
}

throw new Error(message);
logger.error(`Failed updating QualityProfile (${element.name})`);
throw error;
}
}
} else if (create.length > 0 || changedQPs.length > 0) {
Expand Down
57 changes: 42 additions & 15 deletions src/__generated__/ky-client.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/custom-formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IS_DRY_RUN, IS_LOCAL_SAMPLE_MODE, compareCustomFormats, loadJsonFile, m

export const deleteAllCustomFormats = async () => {
const api = getArrApi();
const cfOnServer = await api.v3CustomformatList().json();
const cfOnServer = await api.v3CustomformatList();

for (const cf of cfOnServer) {
await api.v3CustomformatDelete(cf.id!);
Expand All @@ -22,7 +22,7 @@ export const loadServerCustomFormats = async (): Promise<MergedCustomFormatResou
return loadJsonFile<MergedCustomFormatResource[]>(path.resolve(__dirname, "../tests/samples/cfs.json"));
}
const api = getArrApi();
const cfOnServer = await api.v3CustomformatList().json();
const cfOnServer = await api.v3CustomformatList();
return cfOnServer;
};

Expand Down
2 changes: 1 addition & 1 deletion src/quality-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const loadQualityDefinitionFromServer = async (): Promise<MergedQualityDe
if (IS_LOCAL_SAMPLE_MODE) {
return loadJsonFile(path.resolve(__dirname, "../tests/samples/qualityDefinition.json"));
}
return await getArrApi().v3QualitydefinitionList().json();
return await getArrApi().v3QualitydefinitionList();
};

export const calculateQualityDefinitionDiff = (serverQDs: MergedQualityDefinitionResource[], trashQD: TrashQualityDefintion) => {
Expand Down
2 changes: 1 addition & 1 deletion src/quality-profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const loadQualityProfilesFromServer = async (): Promise<MergedQualityProf
}
const api = getArrApi();

const qualityProfiles = await api.v3QualityprofileList().json();
const qualityProfiles = await api.v3QualityprofileList();
// TODO type hack
return qualityProfiles as MergedQualityDefinitionResource[];
};
Expand Down

0 comments on commit af4a500

Please sign in to comment.