-
Notifications
You must be signed in to change notification settings - Fork 288
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
Update CostAndUsageReports.ts with try/catch error handling to enable unknown data to be gracefully handled #1226
base: changeset-release/trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,58 +156,67 @@ export default class CostAndUsageReports { | |
getEstimatesFromInputData( | ||
inputData: LookupTableInput[], | ||
): LookupTableOutput[] { | ||
const result: LookupTableOutput[] = [] | ||
const unknownRows: CostAndUsageReportsRow[] = [] | ||
const result: LookupTableOutput[] = []; | ||
const unknownRows: CostAndUsageReportsRow[] = []; | ||
|
||
inputData.map((inputDataRow: LookupTableInput) => { | ||
const costAndUsageReportRow = new CostAndUsageReportsRow( | ||
null, | ||
'', | ||
'', | ||
inputDataRow.region, | ||
inputDataRow.serviceName, | ||
inputDataRow.usageType, | ||
inputDataRow.usageUnit, | ||
inputDataRow.vCpus != '' ? parseFloat(inputDataRow.vCpus) : null, | ||
1, | ||
1, | ||
{}, | ||
) | ||
|
||
const footprintEstimate = this.getFootprintEstimateFromUsageRow( | ||
costAndUsageReportRow, | ||
unknownRows, | ||
) | ||
|
||
if (footprintEstimate) { | ||
result.push({ | ||
serviceName: inputDataRow.serviceName, | ||
region: inputDataRow.region, | ||
usageType: inputDataRow.usageType, | ||
vCpus: inputDataRow.vCpus, | ||
kilowattHours: footprintEstimate.kilowattHours, | ||
co2e: footprintEstimate.co2e, | ||
}) | ||
try { | ||
const costAndUsageReportRow = new CostAndUsageReportsRow( | ||
null, | ||
'', | ||
'', | ||
inputDataRow.region, | ||
inputDataRow.serviceName, | ||
inputDataRow.usageType, | ||
inputDataRow.usageUnit, | ||
inputDataRow.vCpus != '' ? parseFloat(inputDataRow.vCpus) : null, | ||
1, | ||
1, | ||
{}, | ||
); | ||
|
||
const footprintEstimate = this.getFootprintEstimateFromUsageRow( | ||
costAndUsageReportRow, | ||
unknownRows, | ||
); | ||
|
||
if (footprintEstimate) { | ||
result.push({ | ||
serviceName: inputDataRow.serviceName, | ||
region: inputDataRow.region, | ||
usageType: inputDataRow.usageType, | ||
vCpus: inputDataRow.vCpus, | ||
kilowattHours: footprintEstimate.kilowattHours, | ||
co2e: footprintEstimate.co2e, | ||
}); | ||
} | ||
} catch (error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you confirm that this will in fact reach the catch block? i am not sure that the issue here would actually trigger an error, however, I may be wrong. |
||
console.error(`Error processing inputDataRow: ${JSON.stringify(inputDataRow)}. Error: ${error.message}`); | ||
} | ||
}) | ||
}); | ||
|
||
if (result.length > 0) { | ||
unknownRows.map((inputDataRow: CostAndUsageReportsRow) => { | ||
const footprintEstimate = this.getEstimateForUnknownUsage(inputDataRow) | ||
if (footprintEstimate) | ||
result.push({ | ||
serviceName: inputDataRow.serviceName, | ||
region: inputDataRow.region, | ||
usageType: inputDataRow.usageType, | ||
vCpus: inputDataRow.vCpus, | ||
kilowattHours: footprintEstimate.kilowattHours, | ||
co2e: footprintEstimate.co2e, | ||
}) | ||
}) | ||
try { | ||
const footprintEstimate = this.getEstimateForUnknownUsage(inputDataRow); | ||
if (footprintEstimate) | ||
result.push({ | ||
serviceName: inputDataRow.serviceName, | ||
region: inputDataRow.region, | ||
usageType: inputDataRow.usageType, | ||
vCpus: inputDataRow.vCpus, | ||
kilowattHours: footprintEstimate.kilowattHours, | ||
co2e: footprintEstimate.co2e, | ||
}); | ||
} catch (error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thought as above. I think if the error we are trying to fix is the NaN issue, we may be better to error handle at the root where the arithmetic occurs to aggregate the results. I could also see this potentially being console warnings. |
||
console.error(`Error processing unknownRow: ${JSON.stringify(inputDataRow)}. Error: ${error.message}`); | ||
} | ||
}); | ||
} | ||
|
||
return result | ||
} | ||
return result; | ||
} | ||
|
||
|
||
private getFootprintEstimateFromUsageRow( | ||
costAndUsageReportRow: CostAndUsageReportsRow, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can undo these changes where you add the semi-colons. It may not play well with the linter