-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle errors in ProcessBillingBatchService (#161)
https://eaflood.atlassian.net/browse/WATER-3924 Should an error occur during the supplementary bill run process it brings down the service. We're not handling the exception and because we moved the process to the background in our bill runs controller, there is nothing to deal with it when thrown. So, we need to protect the service from _any_ errors during the process, to prevent the service from going down. But we also need to mimic the legacy service and use an 'error code' to provide extra information about the error. We've already used this concept with [Create error bill run when CM fails](#104) where we apply a code to the `billing_batch` record which the UI then uses to indicate what happened to the user. This change deals with both; ensuring we handle any exceptions raised and where possible, setting a relevant error code when updating the `billing_batch` status to 'errored'.
- Loading branch information
1 parent
daceafc
commit 6ce785e
Showing
4 changed files
with
383 additions
and
86 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
app/services/supplementary-billing/handle-errored-billing-batch.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict' | ||
|
||
/** | ||
* Handles an errored billing batch (setting status etc.) | ||
* @module HandleErroredBillingBatchService | ||
*/ | ||
|
||
const BillingBatchModel = require('../../models/water/billing-batch.model.js') | ||
|
||
/** | ||
* Sets the status of the specified billing batch to `error`, and logs an error if this can't be done. | ||
* | ||
* We keep this in a separate service so we don't need to worry about multiple/nested try-catch blocks in cases where a | ||
* billing batch fails and setting its status to error also fails. | ||
* | ||
* Note that although this is async we would generally not call it asyncronously as the intent is you can call it and | ||
* continue with whatever error logging is required | ||
* | ||
* @param {string} billingBatchId UUID of the billing batch to be marked with `error` status | ||
* @param {number} [errorCode] Numeric error code as defined in BillingBatchModel. Defaults to `null` | ||
*/ | ||
async function go (billingBatchId, errorCode = null) { | ||
try { | ||
await _updateBillingBatch(billingBatchId, errorCode) | ||
} catch (error) { | ||
global.GlobalNotifier.omfg('Failed to set error status on billing batch', { error, billingBatchId, errorCode }) | ||
} | ||
} | ||
|
||
async function _updateBillingBatch (billingBatchId, errorCode) { | ||
await BillingBatchModel.query() | ||
.findById(billingBatchId) | ||
.patch({ | ||
status: 'error', | ||
errorCode | ||
}) | ||
} | ||
|
||
module.exports = { | ||
go | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.