-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-friendly-errors.js
More file actions
127 lines (111 loc) · 4.15 KB
/
Copy pathtest-friendly-errors.js
File metadata and controls
127 lines (111 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const axios = require('axios');
const baseURL = 'http://localhost:4000/api/v1';
async function testFriendlyErrorMessages() {
console.log('Testing friendly error messages for payment links...\n');
// Test 1: Non-existent payment link ID
console.log('1. Testing non-existent payment link:');
try {
await axios.get(`${baseURL}/payment-links/nonexistent123456789012/verify`);
} catch (error) {
if (error.response) {
console.log(' Status:', error.response.status);
console.log(' Error:', error.response.data.error);
}
}
// Test 2: Invalid payment link ID format
console.log('\n2. Testing invalid payment link ID format:');
try {
await axios.get(`${baseURL}/payment-links/invalid-id/verify`);
} catch (error) {
if (error.response) {
console.log(' Status:', error.response.status);
console.log(' Error:', error.response.data.error);
}
}
// Test 3: Accessing non-existent payment link
console.log('\n3. Testing access to non-existent payment link:');
try {
await axios.post(`${baseURL}/payment-links/nonexistent123456789012/access`);
} catch (error) {
if (error.response) {
console.log(' Status:', error.response.status);
console.log(' Error:', error.response.data.error);
}
}
// Test 4: Create a payment link, then disable it, then try to access
console.log('\n4. Testing disabled payment link access:');
try {
// First create a payment link
const createResponse = await axios.post(`${baseURL}/payment-links`, {
merchantId: 'test-merchant',
userId: 'test-user',
name: 'Test Business',
amount: '50.00',
currency: 'USD',
token: 'USDT',
selectedCurrency: 'USD',
paymentType: 'card',
description: 'Test payment for error handling'
});
const paymentLinkId = createResponse.data.data.id;
console.log(' Created payment link:', paymentLinkId);
// Disable the payment link
await axios.patch(`${baseURL}/payment-links/${paymentLinkId}/disable`, {
reason: 'Testing error messages'
});
console.log(' Payment link disabled');
// Try to access the disabled payment link
try {
await axios.post(`${baseURL}/payment-links/${paymentLinkId}/access`);
} catch (accessError) {
if (accessError.response) {
console.log(' Status:', accessError.response.status);
console.log(' Error:', accessError.response.data.error);
}
}
// Try to disable it again
console.log('\n5. Testing double disable:');
try {
await axios.patch(`${baseURL}/payment-links/${paymentLinkId}/disable`, {
reason: 'Testing double disable'
});
} catch (disableError) {
if (disableError.response) {
console.log(' Status:', disableError.response.status);
console.log(' Error:', disableError.response.data.error);
}
}
// Enable it back
await axios.patch(`${baseURL}/payment-links/${paymentLinkId}/enable`);
console.log('\n Payment link re-enabled');
// Try to enable it again
console.log('\n6. Testing double enable:');
try {
await axios.patch(`${baseURL}/payment-links/${paymentLinkId}/enable`);
} catch (enableError) {
if (enableError.response) {
console.log(' Status:', enableError.response.status);
console.log(' Error:', enableError.response.data.error);
}
}
} catch (error) {
console.error(' Setup error:', error.response?.data || error.message);
}
// Test 7: Missing payment link ID
console.log('\n7. Testing missing payment link ID:');
try {
await axios.get(`${baseURL}/payment-links//verify`);
} catch (error) {
if (error.response) {
console.log(' Status:', error.response.status);
console.log(' Error:', error.response.data.error || 'Route not found');
}
}
console.log('\n✅ Friendly error message testing completed!');
console.log('\nKey improvements:');
console.log('- Clear, user-friendly language');
console.log('- Helpful suggestions for next steps');
console.log('- Context-specific messages');
console.log('- Professional tone suitable for end users');
}
testFriendlyErrorMessages().catch(console.error);