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

Add new custom ExpandedError #296

Merged
merged 7 commits into from
Jul 6, 2023
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
19 changes: 19 additions & 0 deletions app/errors/expanded.error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

class ExpandedError extends Error {
/**
* Custom error that allows additional data properties to be assigned to the error instance
*
* @param {String} message - Message that will be given to the error instance
* @param {Object} data - An object containing the additional data properties to be assigned to the error instance
*/
constructor (message, data) {
super(message)

for (const [key, value] of Object.entries(data)) {
this[key] = value
}
}
}

module.exports = ExpandedError
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

const BillingBatchModel = require('../../models/water/billing-batch.model.js')

const ChargingModuleCreateBillRunService = require('../charging-module/create-bill-run.service.js')
const CheckLiveBillRunService = require('./check-live-bill-run.service.js')
const CreateBillingBatchService = require('./create-billing-batch.service.js')
const CreateBillingBatchEventService = require('./create-billing-batch-event.service.js')
const ExpandedError = require('../../errors/expanded.error.js')

/**
* Initiate a new billing batch
Expand All @@ -27,7 +27,7 @@ async function go (financialYearEndings, regionId, userEmail) {
const liveBillRunExists = await CheckLiveBillRunService.go(regionId, financialYearEndings.toFinancialYearEnding)

if (liveBillRunExists) {
throw Error(`Batch already live for region ${regionId}`)
throw new ExpandedError('Batch already live for region', { regionId })
}

const chargingModuleResult = await ChargingModuleCreateBillRunService.go(regionId, 'sroc')
Expand Down
15 changes: 9 additions & 6 deletions app/services/supplementary-billing/reissue-invoice.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { randomUUID } = require('crypto')

const ChargingModuleReissueInvoiceService = require('../charging-module/reissue-invoice.service.js')
const ChargingModuleViewInvoiceService = require('../charging-module/view-invoice.service.js')
const ExpandedError = require('../../errors/expanded.error.js')
const GenerateBillingInvoiceLicenceService = require('./generate-billing-invoice-licence.service.js')
const GenerateBillingInvoiceService = require('./generate-billing-invoice.service.js')

Expand Down Expand Up @@ -236,9 +237,10 @@ async function _sendReissueRequest (billingBatchExternalId, invoiceExternalId) {
const result = await ChargingModuleReissueInvoiceService.go(billingBatchExternalId, invoiceExternalId)

if (!result.succeeded) {
const error = new Error('Charging Module reissue request failed')
error.billingBatchExternalId = billingBatchExternalId
error.invoiceExternalId = invoiceExternalId
const error = new ExpandedError(
'Charging Module reissue request failed',
{ billingBatchExternalId, invoiceExternalId }
)

throw error
}
Expand All @@ -253,9 +255,10 @@ async function _sendViewInvoiceRequest (billingBatch, reissueInvoiceId) {
const result = await ChargingModuleViewInvoiceService.go(billingBatch.externalId, reissueInvoiceId)

if (!result.succeeded) {
const error = new Error('Charging Module view invoice request failed')
error.billingBatchExternalId = billingBatch.externalId
error.reissueInvoiceExternalId = reissueInvoiceId
const error = new ExpandedError(
'Charging Module view invoice request failed',
{ billingBatchExternalId: billingBatch.externalId, reissueInvoiceExternalId: reissueInvoiceId }
)

throw error
}
Expand Down
35 changes: 35 additions & 0 deletions test/errors/expanded.error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const ExpandedError = require('../../app/errors/expanded.error.js')

describe('ExpandedError', () => {
describe('when instantiated with additional data properties', () => {
let additionalData

beforeEach(() => {
additionalData = {
billingBatchId: '37f2871b-e0a7-471f-902b-1e55b09d6d88',
details: {
type: 'supplementary',
status: 'errored'
}
}
})

it('will assign those to the error instance', () => {
const result = new ExpandedError('My test error', additionalData)

expect(result.message).to.equal('My test error')
expect(result.billingBatchId).to.equal(additionalData.billingBatchId)
expect(result.details).to.equal(additionalData.details)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ describe('Initiate Billing Batch service', () => {
const err = await expect(InitiateBillingBatchService.go(financialYearEndings, regionId, user)).to.reject()

expect(err).to.be.an.error()
expect(err.message).to.equal(`Batch already live for region ${regionId}`)
expect(err.message).to.equal('Batch already live for region')
expect(err.regionId).to.equal(regionId)
})
})
})
Expand Down