import {
ThreatClient,
Chain,
WebacyError,
AuthenticationError,
ValidationError,
RateLimitError,
NotFoundError,
NetworkError,
} from '@webacy-xyz/sdk';
async function safeAnalyze(client: ThreatClient, address: string) {
try {
return await client.addresses.analyze(address);
} catch (error) {
// Handle specific error types
if (error instanceof AuthenticationError) {
throw new Error('Please check your API key configuration');
}
if (error instanceof ValidationError) {
throw new Error(`Invalid address: ${error.getRecoverySuggestion()}`);
}
if (error instanceof RateLimitError) {
const waitTime = error.retryAfter || 60;
throw new Error(`Too many requests. Please wait ${waitTime} seconds.`);
}
if (error instanceof NotFoundError) {
return null; // Address not found, return null
}
if (error instanceof NetworkError) {
throw new Error('Network error. Please check your connection.');
}
// Log unexpected errors with request ID for support
if (error instanceof WebacyError) {
console.error('Unexpected error:', error.toJSON());
if (error.requestId) {
console.error(`Request ID for support: ${error.requestId}`);
}
}
throw error;
}
}