Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

Commit 8ef9797

Browse files
committed
feat: throw errors if arguments are falsy
1 parent dd9a6f3 commit 8ef9797

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

lib/index.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,19 @@ const SUBSCRIPTION_ACTIVE_STATUS = [
8383

8484
class Subscriptions {
8585

86+
get ERROR_INVALID_ARGUMENTS() {
87+
return 'INVALID_ARGUMENTS'
88+
}
89+
8690
/**
8791
*
8892
* @param {String} identifier a unique value that can be used to query this subscription
8993
* @param {SubscriptionCreatedPayload} subscription
9094
*/
9195
async addSubscription(identifier, subscription) {
92-
// if (!userId || !subscription) {
93-
// logger.error(`addSubscription called with falsy arguments ${userId} ${subscription}`)
94-
// return Promise.reject(this.REASON_INVALID_REQUEST)
95-
// }
96+
if (!identifier || !subscription) {
97+
return Promise.reject(new Error(this.ERROR_INVALID_ARGUMENTS))
98+
}
9699

97100
const statusModel = {
98101
currency: subscription.currency,
@@ -124,10 +127,9 @@ class Subscriptions {
124127
* @param {SubscriptionUpdatedPayload} subscription
125128
*/
126129
async updateSubscription(subscription) {
127-
// if (!userId || !subscriptionId || !subscription) {
128-
// logger.error('updateSubscription called with falsy arguments')
129-
// return Promise.reject(this.REASON_INVALID_REQUEST)
130-
// }
130+
if (!subscription) {
131+
return Promise.reject(new Error(this.ERROR_INVALID_ARGUMENTS))
132+
}
131133

132134
const statusModel = {
133135
currency: subscription.currency,
@@ -157,10 +159,9 @@ class Subscriptions {
157159
* @param {SubscriptionCancelledPayload} subscription
158160
*/
159161
async cancelSubscription(subscription) {
160-
// if (!userId || !subscriptionId || !subscription) {
161-
// logger.error('cancelSubscription called with falsy arguments')
162-
// return Promise.reject(this.REASON_INVALID_REQUEST)
163-
// }
162+
if (!subscription) {
163+
return Promise.reject(new Error(this.ERROR_INVALID_ARGUMENTS))
164+
}
164165

165166
const statusModel = {
166167
currency: subscription.currency,

test/spec/index.spec.js

+39
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ describe('PaddleIntegration', () => {
4747
expect(status.quantity).to.equal(createPayload.quantity)
4848
expect(status.start_at).to.equal(createPayload.event_time)
4949
})
50+
it('throws if identifier is falsy', () => {
51+
return paddleIntegration.addSubscription(null, 'not-null').then(() => {
52+
return Promise.reject('Must throw an error')
53+
}, (error) => {
54+
console.log(error.message)
55+
if (error.message !== 'INVALID_ARGUMENTS') {
56+
return Promise.reject('Must throw an error "INVALID_ARGUMENTS')
57+
}
58+
})
59+
})
60+
it('throws if subscription is falsy', () => {
61+
return paddleIntegration.addSubscription('not-null', null).then(() => {
62+
return Promise.reject('Must throw an error')
63+
}, (error) => {
64+
console.log(error.message)
65+
if (error.message !== 'INVALID_ARGUMENTS') {
66+
return Promise.reject('Must throw an error "INVALID_ARGUMENTS')
67+
}
68+
})
69+
})
5070
})
5171

5272
describe('.updateSubscription', () => {
@@ -107,6 +127,16 @@ describe('PaddleIntegration', () => {
107127
expect(status2.quantity).to.equal(updatePayload.new_quantity)
108128
expect(status2.start_at).to.equal(updatePayload.event_time)
109129
})
130+
it('throws if subscription is falsy', () => {
131+
return paddleIntegration.updateSubscription(null).then(() => {
132+
return Promise.reject('Must throw an error')
133+
}, (error) => {
134+
console.log(error.message)
135+
if (error.message !== 'INVALID_ARGUMENTS') {
136+
return Promise.reject('Must throw an error "INVALID_ARGUMENTS')
137+
}
138+
})
139+
})
110140
})
111141

112142
describe('.cancelSubscription', () => {
@@ -153,6 +183,15 @@ describe('PaddleIntegration', () => {
153183
expect(status.quantity).to.equal(cancelPayload.quantity)
154184
expect(status.start_at).to.equal(cancelPayload.cancellation_effective_date)
155185
})
186+
it('throws if subscription is falsy', () => {
187+
return paddleIntegration.cancelSubscription(null).then(() => {
188+
return Promise.reject('Must throw an error')
189+
}, (error) => {
190+
if (error.message !== 'INVALID_ARGUMENTS') {
191+
return Promise.reject('Must throw an error "INVALID_ARGUMENTS')
192+
}
193+
})
194+
})
156195
})
157196

158197
describe('.hasActiveSubscription', () => {

0 commit comments

Comments
 (0)