Removed useless try.catch
MODIFY
src/integration/billing/BillingIntegration.ts
src/integration/billing/BillingIntegration.ts
@@ -158,7 +158,6 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
}
public async synchronizeUser(tenantID: string, user: User): Promise<void> {
- try {
const exists = await this.userExists(user);
let newUser: BillingUser;
if (!exists) {
@@ -166,50 +165,11 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
} else {
newUser = await this.updateUser(user);
}
- try {
await UserStorage.saveUserBillingData(tenantID, user.id, newUser.billingData);
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'synchronizeUser',
- action: ServerAction.BILLING_SYNCHRONIZE_USERS,
- actionOnUser: user,
- message: 'Unable to save user Billing Data in e-Mobility',
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
- } catch (error) {
- if (!user.billingData) {
- user.billingData = {};
- }
- user.billingData.hasSynchroError = true;
- try {
- await UserStorage.saveUserBillingData(tenantID, user.id, user.billingData);
- } catch (error2) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'synchronizeUser',
- action: ServerAction.BILLING_SYNCHRONIZE_USERS,
- actionOnUser: user,
- message: 'Unable to save user Billing Data in e-Mobility',
- detailedMessages: { error: error2.message, stack: error2.stack }
- });
- }
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'synchronizeUser',
- action: ServerAction.BILLING_SYNCHRONIZE_USERS,
- actionOnUser: user,
- message: `Cannot synchronize user '${user.email}' with billing system`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
public async forceSynchronizeUser(tenantID: string, user: User): Promise<void> {
- let billingUser: BillingUser;
- try {
- billingUser = await this.getUserByEmail(user.email);
+ let billingUser = await this.getUserByEmail(user.email);
if (billingUser) {
if (user.billingData) {
// Only override user's customerID
@@ -231,16 +191,6 @@ export default abstract class BillingIntegration<T extends BillingSetting> {
module: MODULE_NAME, method: 'forceSynchronizeUser',
message: `Successfully forced the synchronization of the user '${user.email}'`,
});
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'forceSynchronizeUser',
- action: ServerAction.BILLING_FORCE_SYNCHRONIZE_USER,
- actionOnUser: user,
- message: `Cannot force synchronize user '${user.email}' with billing system`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
public async synchronizeInvoices(tenantID: string, user?: User): Promise<BillingUserSynchronizeAction> {

MODIFY
src/integration/billing/stripe/StripeBillingIntegration.ts
src/integration/billing/stripe/StripeBillingIntegration.ts
@@ -35,11 +35,7 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
this.settings.secretKey = Cypher.decrypt(settings.secretKey);
}
// Currently the public key is not encrypted
- try {
this.stripe = new Stripe(this.settings.secretKey);
- } catch (error) {
- delete this.stripe;
- }
}
public async checkConnection(): Promise<void> {
@@ -191,7 +187,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
limit: StripeBillingIntegration.STRIPE_MAX_LIST,
type: 'customer.*',
};
- try {
// Check Stripe
await this.checkConnection();
// Loop until all users are read
@@ -208,15 +203,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
request['starting_after'] = collectedCustomerIDs[collectedCustomerIDs.length - 1];
}
} while (request['has_more']);
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_SYNCHRONIZE_USERS,
- module: MODULE_NAME, method: 'getUpdatedCustomersForSynchronization',
- message: `Impossible to retrieve changed customers from Stripe Billing: ${error.message}`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
return collectedCustomerIDs;
}
@@ -238,7 +224,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
limit: StripeBillingIntegration.STRIPE_MAX_LIST,
type: 'invoice.*',
};
- try {
// Loop until all invoices are read
do {
events = await this.stripe.events.list(request);
@@ -262,15 +247,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
request['starting_after'] = collectedInvoiceIDs[collectedInvoiceIDs.length - 1];
}
} while (request['has_more']);
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_SYNCHRONIZE_INVOICES,
- module: MODULE_NAME, method: 'getUpdatedInvoiceIDsInBilling',
- message: `Impossible to retrieve changed invoices from Stripe Billing: ${error.message}`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
return collectedInvoiceIDs;
}
@@ -308,7 +284,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
await this.createInvoiceItem(user, stripeInvoice.id, invoiceItem, idempotencyKey);
} catch (error) {
// No pending invoice item found: Create one
- try {
invoiceItem = await this.stripe.invoiceItems.create({
customer: user.billingData.customerID,
currency: this.settings.currency.toLocaleLowerCase(),
@@ -321,15 +296,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
days_until_due: daysUntilDue,
auto_advance: false
});
- } catch (err) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_CREATE_INVOICE,
- module: MODULE_NAME, method: 'createInvoice',
- message: 'Failed to create invoice',
- detailedMessages: { error: err.message, stack: err.stack }
- });
- }
}
const invoice = {
invoiceID: stripeInvoice.id,
@@ -341,19 +307,9 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
createdOn: new Date(),
nbrOfItems: stripeInvoice.lines.total_count
} as Partial<BillingInvoice>;
- try {
invoice.user = await UserStorage.getUserByBillingID(this.tenantID, user.billingData.customerID);
invoice.id = await BillingStorage.saveInvoice(this.tenantID, invoice);
return { invoice: invoice as BillingInvoice, invoiceItem: invoiceItem };
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_CREATE_INVOICE,
- module: MODULE_NAME, method: 'createInvoice',
- message: 'Failed to save invoice in database',
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
public async createInvoiceItem(user: BillingUser, invoiceID: string, invoiceItem: BillingInvoiceItem, idempotencyKey?: string | number): Promise<BillingInvoiceItem> {
@@ -366,7 +322,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
message: 'Invoice item not provided',
});
}
- try {
return await this.stripe.invoiceItems.create({
customer: user.billingData.customerID,
currency: this.settings.currency.toLocaleLowerCase(),
@@ -376,15 +331,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
}, {
idempotency_key: idempotencyKey ? idempotencyKey.toString() : null
});
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- action: ServerAction.BILLING_CREATE_INVOICE_ITEM,
- module: MODULE_NAME, method: 'createInvoiceItem',
- message: 'Failed to create invoice item',
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
public async downloadInvoiceDocument(invoice: BillingInvoice): Promise<BillingInvoiceDocument> {
@@ -721,20 +667,9 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
await this.checkConnection();
const customer = await this.getCustomerByEmail(user.email);
if (customer && customer.id) {
- try {
await this.stripe.customers.del(
customer.id
);
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'updateUser',
- action: ServerAction.USER_CREATE,
- user: user,
- message: 'Cannot delete the User',
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
}
@@ -798,24 +733,14 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
} else {
customer = await this.getCustomerByEmail(user.email);
}
+ // Create
if (!customer) {
- try {
customer = await this.stripe.customers.create({
email: user.email,
description: description,
name: fullName,
preferred_locales: [locale]
});
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'modifyUser',
- action: ServerAction.USER_CREATE,
- user: user,
- message: 'Impossible to create a Stripe customer',
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
const dataToUpdate: any = {};
if (customer.description !== description) {
@@ -834,42 +759,12 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
dataToUpdate.preferred_locales = [locale];
}
// Update
- if (!Utils.isEmptyJSon(dataToUpdate)) {
- try {
+ if (customer.id) {
customer = await this.stripe.customers.update(
customer.id,
dataToUpdate
);
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'modifyUser',
- action: ServerAction.USER_CREATE,
- user: user,
- message: `Impossible to update Stripe customer '${customer.id}' with email '${user.email}'`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
}
- }
- // Payment method has to be stored in User's BillingData
- // const newPaymentMethod = req.body.paymentToken ? sanitize(req.body.paymentToken) : null;
- // if (newPaymentMethod) {
- // try {
- // customer = await this.stripe.customers.update(
- // customer.id,
- // { source: newPaymentMethod }
- // );
- // } catch (error) {
- // throw new BackendError({
- // source: Constants.CENTRAL_SERVER,
- // module: MODULE_NAME, method: 'modifyUser',
- // action: ServerAction.USER_CREATE,
- // user: user,
- // message: `Impossible to update Stripe customer '${customer.id}' with email '${user.email}'`,
- // detailedMessages: { error: error.message, stack: error.stack }
- // });
- // }
- // }
const billingMethod = this.retrieveBillingMethod(user);
let collectionMethod;
let daysUntilDue = 0;
@@ -891,7 +786,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
if (subscription && billingMethod !== BillingMethod.IMMEDIATE) {
// Check whether existing subscription needs to be updated
if (collectionMethod !== subscription.billing) {
- try {
if (collectionMethod === 'send_invoice') {
await this.stripe.subscriptions.update(
subscription.id,
@@ -906,34 +800,14 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
billing: 'charge_automatically',
});
}
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'modifyUser',
- action: ServerAction.USER_CREATE,
- user: user,
- message: `Impossible to update Stripe customer's subscription '${subscription.id}' with email '${user.email}'`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
if (billingPlan && billingPlan !== subscription.plan) {
- try {
await this.stripe.subscriptions.update(
subscription.id,
{
plan: billingPlan,
- });
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'modifyUser',
- action: ServerAction.USER_CREATE,
- user: user,
- message: `Impossible to update Stripe customer's subscription '${subscription.id}' with email '${user.email}'`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
}
+ );
}
}
if (!subscription && billingPlan && billingMethod && billingMethod !== BillingMethod.IMMEDIATE) {
@@ -943,7 +817,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
if (plan.interval === 'year' || plan.interval === 'month') {
billingCycleAnchor = moment().endOf('month').add(1, 'day').unix(); // Begin of next month
}
- try {
if (collectionMethod === 'send_invoice') {
subscription = await this.stripe.subscriptions.create({
customer: customer.id,
@@ -968,16 +841,6 @@ export default class StripeBillingIntegration extends BillingIntegration<StripeB
billing: 'charge_automatically',
});
}
- } catch (error) {
- throw new BackendError({
- source: Constants.CENTRAL_SERVER,
- module: MODULE_NAME, method: 'modifyUser',
- action: ServerAction.USER_CREATE,
- user: user,
- message: `Impossible to create new Stripe subscription for user with email '${user.email}'`,
- detailedMessages: { error: error.message, stack: error.stack }
- });
- }
}
return {
name: customer.name,

Removed useless try.catch