dumpResponseToFile function
Saves HTML content to dump file for debugging purposes.
This function is used to save HTML content to timestamped files in a dump directory. It's useful for debugging scraping issues by preserving the HTML content that was processed.
The function:
- Creates a dump directory if it doesn't exist
- Saves the current dump.html content to a timestamped file
- Updates dump.html with the new HTML content
Parameters:
html
: The HTML content to savedebug
: Whether debug mode is enabled (only saves if true)
Example:
dumpResponse('<html>...</html>', debug);
Implementation
void dumpResponseToFile({required String html, required bool debug}) {
if (!debug) return;
try {
// Generate timestamp for unique filename
final timestamp = DateTime.now().millisecondsSinceEpoch;
final currentDir = Directory.current;
// Ensure dump folder exists
final dumpDir = Directory(path.join(currentDir.path, 'dump'));
if (!dumpDir.existsSync()) {
dumpDir.createSync(recursive: true);
print('Created dump folder at: ${dumpDir.path}');
}
// The main dump.html file inside /dump
final dumpLocation = File(path.join(dumpDir.path, 'dump.html'));
String oldFileContent = '';
if (dumpLocation.existsSync()) {
// Read current dump.html
oldFileContent = dumpLocation.readAsStringSync();
// Save old dump content to a timestamped file inside /dump
final oldDump = File(path.join(dumpDir.path, '$timestamp.html'));
oldDump.writeAsStringSync(oldFileContent);
}
// Update or create dump.html with new content
dumpLocation.writeAsStringSync(html, mode: FileMode.write);
print('Dump saved at: ${dumpLocation.path}');
} catch (e) {
print('Unable to save file, error: $e');
}
}