-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-error-handling.js
More file actions
120 lines (100 loc) · 3.19 KB
/
test-error-handling.js
File metadata and controls
120 lines (100 loc) · 3.19 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
/* global console */
// Test script for error handling with improved validation
import { FirecrawlClient } from 'firecrawl-simple-client';
import { URL } from 'url';
// Custom validation wrapper for the client
class EnhancedFirecrawlClient extends FirecrawlClient {
constructor(config) {
super(config);
}
// Override scrapeWebpage with validation
async scrapeWebpage(options) {
// Validate URL
if (!options.url) {
throw new Error('URL is required');
}
if (typeof options.url !== 'string') {
throw new Error('URL must be a string');
}
if (!options.url.startsWith('http://') && !options.url.startsWith('https://')) {
throw new Error('URL must start with http:// or https://');
}
try {
new URL(options.url);
} catch {
throw new Error('Invalid URL format');
}
return super.scrapeWebpage(options);
}
// Override generateSitemap with validation
async generateSitemap(options) {
// Validate URL
if (!options.url) {
throw new Error('URL is required');
}
if (typeof options.url !== 'string') {
throw new Error('URL must be a string');
}
if (!options.url.startsWith('http://') && !options.url.startsWith('https://')) {
throw new Error('URL must start with http:// or https://');
}
try {
new URL(options.url);
} catch {
throw new Error('Invalid URL format');
}
return super.generateSitemap(options);
}
// Override getCrawlStatus with validation
async getCrawlStatus(jobId) {
// Validate job ID
if (!jobId) {
throw new Error('Job ID is required');
}
if (typeof jobId !== 'string') {
throw new Error('Job ID must be a string');
}
return super.getCrawlStatus(jobId);
}
}
async function testErrorHandling() {
try {
console.log('Testing error handling...');
const client = new EnhancedFirecrawlClient({
apiUrl: 'http://localhost:3002/v1',
});
// Test invalid URL for scrape tool
console.log('\nTesting scrape tool with invalid URL:');
try {
const result = await client.scrapeWebpage({
url: 'invalid-url',
formats: ['markdown'],
});
console.log('Scrape result:', JSON.stringify(result, null, 2));
} catch (error) {
console.log('Scrape error (expected):', error.message);
}
// Test invalid URL for map tool
console.log('\nTesting map tool with invalid URL:');
try {
const result = await client.generateSitemap({
url: 'invalid-url',
});
console.log('Map result:', JSON.stringify(result, null, 2));
} catch (error) {
console.log('Map error (expected):', error.message);
}
// Test invalid job ID for check crawl status tool
console.log('\nTesting check crawl status tool with invalid job ID:');
try {
const result = await client.getCrawlStatus(null);
console.log('Check crawl status result:', JSON.stringify(result, null, 2));
} catch (error) {
console.log('Check crawl status error (expected):', error.message);
}
console.log('\nError handling tests completed successfully!');
} catch (error) {
console.error('Unexpected error during testing:', error);
}
}
testErrorHandling();