Add improved error handling for 4xx errors from ES.

This commit is contained in:
Patrick FIc
2025-09-11 13:02:59 -07:00
parent 2d61cda828
commit 9df3165998
5 changed files with 102 additions and 80 deletions

View File

@@ -7,41 +7,41 @@ const { promises: fsPromises } = require("fs");
// Function to write job object to logs subfolder
async function writeJobToLogsFolder(job, fileName) {
try {
// Get the directory where electron-log stores its files
const logFilePath = log.transports.file.getFile().path;
const logsDir = path.dirname(logFilePath);
try {
// Get the directory where electron-log stores its files
const logFilePath = log.transports.file.getFile().path;
const logsDir = path.dirname(logFilePath);
// Create a subfolder for job objects
const jobLogsDir = path.join(logsDir, 'esjson');
// Create a subfolder for job objects
const jobLogsDir = path.join(logsDir, "esjson");
// Ensure the directory exists
await fsPromises.mkdir(jobLogsDir, { recursive: true });
// Ensure the directory exists
await fsPromises.mkdir(jobLogsDir, { recursive: true });
// Write the job object as JSON
const jobFilePath = path.join(jobLogsDir, `${fileName}.json`);
await fsPromises.writeFile(jobFilePath, JSON.stringify(job, null, 2), 'utf8');
// Write the job object as JSON
const jobFilePath = path.join(jobLogsDir, `${fileName}.json`);
await fsPromises.writeFile(jobFilePath, JSON.stringify(job, null, 2), "utf8");
log.debug(`Job object written to: ${jobFilePath}`);
console.log(`Job object written to: ${jobFilePath}`);
return jobFilePath;
} catch (error) {
log.error('Error writing job object to logs folder:', error);
throw error;
}
log.debug(`Job object written to: ${jobFilePath}`);
console.log(`Job object written to: ${jobFilePath}`);
return jobFilePath;
} catch (error) {
log.error("Error writing job object to logs folder:", error);
throw error;
}
}
async function ScrubEstimate({ job }) {
//These are hard coded as they are not secure values and checking happens based on other values.
//No secret or private information is exposed.
const basicAuthUser = "Imex2";
const basicAuthpassword = "Patrick";
const estimateScrubberUrl = "https://insurtechtoolkit.com/api/sendems";
const sendingEntityId = '87330f61-412b-4251-baaa-d026565b23c5'
//These are hard coded as they are not secure values and checking happens based on other values.
//No secret or private information is exposed.
const basicAuthUser = "Imex2";
const basicAuthpassword = "Patrick";
const estimateScrubberUrl = "https://insurtechtoolkit.com/api/sendems";
const sendingEntityId = "87330f61-412b-4251-baaa-d026565b23c5";
try {
//Perform data manipulation on the job object
if (!job) {
console.error("No job provided to ScrubEstimate");
return;
console.error("No job provided to ScrubEstimate");
return;
}
//Set shop metrics
@@ -53,47 +53,47 @@ async function ScrubEstimate({ job }) {
job.g_ttl_amt = job.clm_total;
job.source_system = "M"; //Requested by Steven.
delete job.clm_total;
delete job.bodyshop //Bodyshop has to be passed through the object as we don't have access to the store here.
delete job.bodyshop; //Bodyshop has to be passed through the object as we don't have access to the store here.
//Adjust the rates field to be MAT_TYPE instead of MATL_TYPE
if (job.rates && Array.isArray(job.rates)) {
job.rates.forEach(rate => {
if (rate.MATL_TYPE) {
rate.MAT_TYPE = rate.MATL_TYPE;
delete rate.MATL_TYPE;
}
});
job.rates.forEach((rate) => {
if (rate.MATL_TYPE) {
rate.MAT_TYPE = rate.MATL_TYPE;
delete rate.MATL_TYPE;
}
});
}
//Lower case the rates & totals
if (job.rates && Array.isArray(job.rates)) {
job.rates = job.rates.map(rate => {
const lowercasedRate = {};
for (const [key, value] of Object.entries(rate)) {
lowercasedRate[key.toLowerCase()] = value;
}
return lowercasedRate;
});
job.rates = job.rates.map((rate) => {
const lowercasedRate = {};
for (const [key, value] of Object.entries(rate)) {
lowercasedRate[key.toLowerCase()] = value;
}
return lowercasedRate;
});
}
if (job.totals && Array.isArray(job.totals)) {
job.totals = job.totals.map(total => {
const lowercasedTotal = {};
for (const [key, value] of Object.entries(total)) {
lowercasedTotal[key.toLowerCase()] = value;
}
return lowercasedTotal;
});
job.totals = job.totals.map((total) => {
const lowercasedTotal = {};
for (const [key, value] of Object.entries(total)) {
lowercasedTotal[key.toLowerCase()] = value;
}
return lowercasedTotal;
});
}
const fileName = `RPS-Scrub-${job.id}-${job.clm_no}-${Date.now()}`;
// Write job object to logs subfolder
try {
await writeJobToLogsFolder(job, fileName);
await writeJobToLogsFolder(job, fileName);
} catch (error) {
log.error('Failed to write job to logs folder:', error);
// Continue with the rest of the function even if this fails
log.error("Failed to write job to logs folder:", error);
// Continue with the rest of the function even if this fails
}
const formData = new FormData();
@@ -101,21 +101,21 @@ async function ScrubEstimate({ job }) {
formData.append("file", new Blob([jsonString], { type: "application/json" }), `${fileName}.json`);
const result = await axios.post(estimateScrubberUrl, formData, {
auth: {
username: basicAuthUser,
password: basicAuthpassword
},
headers: formData.getHeaders ? formData.getHeaders() : {}
auth: {
username: basicAuthUser,
password: basicAuthpassword
},
headers: formData.getHeaders ? formData.getHeaders() : {}
});
const resultPDFUrl = result?.data?.report_link
const resultPDFUrl = result?.data?.report_link;
// log.log("Estimate Scrubber Result:", result.data, resultPDFUrl);
const b = BrowserWindow.getAllWindows()[0];
b.webContents.send(ipcTypes.app.toRenderer.scrubResults, {
jobid: job.id,
items: result.data?.identified_item,
pdfUrl: resultPDFUrl
jobid: job.id,
items: result.data?.identified_item,
pdfUrl: resultPDFUrl
});
// const pdfWindow = new BrowserWindow({
@@ -127,6 +127,21 @@ async function ScrubEstimate({ job }) {
// pdfWindow.loadURL(resultPDFUrl);
// pdfWindow.focus();
return resultPDFUrl
return resultPDFUrl;
} catch (error) {
log.error("Error while scrubbing estimate:", error);
log.error("Error Response Data:", error.response?.data);
const mainWindow = BrowserWindow.getAllWindows()[0];
if (error.status === 400) {
mainWindow.webContents.send(ipcTypes.app.toRenderer.scrubError, {
message: error.response?.data || "Error encountered sending estimate to Estimate Scrubber."
});
} else if (error.status === 401) {
mainWindow.webContents.send(ipcTypes.app.toRenderer.scrubError, {
message: "Authentication with Estimate Scrubber failed." || error.response?.data
});
}
}
}
exports.ScrubEstimate = ScrubEstimate
exports.ScrubEstimate = ScrubEstimate;