-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handling.ts
More file actions
119 lines (104 loc) · 3.39 KB
/
error-handling.ts
File metadata and controls
119 lines (104 loc) · 3.39 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
/**
* Error Handling Example
*
* Shows how to handle different error types from the SDK
*/
import {
OilPriceAPI,
AuthenticationError,
RateLimitError,
NotFoundError,
ServerError,
TimeoutError,
OilPriceAPIError
} from 'oilpriceapi';
const client = new OilPriceAPI({
apiKey: process.env.OIL_PRICE_API_KEY || 'your_api_key_here',
retries: 3,
retryDelay: 1000,
timeout: 10000,
debug: false
});
async function handleErrors() {
try {
// This will succeed if API key is valid
const prices = await client.getLatestPrices({ commodity: 'WTI_USD' });
console.log('Success:', prices[0].formatted);
} catch (error) {
// Handle specific error types
if (error instanceof AuthenticationError) {
console.error('❌ Authentication failed');
console.error('Check your API key at https://www.oilpriceapi.com');
console.error('Status:', error.statusCode); // 401
} else if (error instanceof RateLimitError) {
console.error('❌ Rate limit exceeded');
console.error('Try again later or upgrade your plan');
console.error('Status:', error.statusCode); // 429
// SDK automatically retries with exponential backoff
} else if (error instanceof NotFoundError) {
console.error('❌ Resource not found');
console.error('Check commodity code spelling');
console.error('Status:', error.statusCode); // 404
} else if (error instanceof TimeoutError) {
console.error('❌ Request timed out');
console.error('API might be slow, SDK retries automatically');
console.error(error.message); // Includes timeout duration
} else if (error instanceof ServerError) {
console.error('❌ Server error');
console.error('API is experiencing issues, SDK retries automatically');
console.error('Status:', error.statusCode); // 500+
} else if (error instanceof OilPriceAPIError) {
// Generic API error (fallback)
console.error('❌ API Error:', error.message);
console.error('Status:', error.statusCode);
console.error('Code:', error.code);
} else {
// Network or other errors
console.error('❌ Unexpected error:', error);
}
}
}
// Example: Graceful degradation with fallback
async function getPriceWithFallback(commodity: string) {
try {
const prices = await client.getLatestPrices({ commodity });
return prices[0];
} catch (error) {
if (error instanceof RateLimitError) {
// Use cached data if rate limited
console.log('Rate limited, using cached data...');
return getCachedPrice(commodity);
}
throw error; // Re-throw other errors
}
}
// Example: Retry with different strategy
async function getDataWithCustomRetry() {
const clientWithRetries = new OilPriceAPI({
apiKey: process.env.OIL_PRICE_API_KEY || 'your_api_key_here',
retries: 5,
retryDelay: 2000,
retryStrategy: 'exponential', // Exponential backoff
timeout: 15000
});
try {
return await clientWithRetries.getLatestPrices();
} catch (error) {
console.error('Failed after 5 retries:', error);
throw error;
}
}
// Mock cache function (implement your own caching)
function getCachedPrice(commodity: string) {
return {
code: commodity,
price: 75.00, // Cached value
formatted: '$75.00',
currency: 'USD',
created_at: new Date().toISOString(),
type: 'spot_price',
source: 'cache'
};
}
// Run example
handleErrors();