From 0b01471b1c08ffdd47ed01a71edafe4f06e7b844 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 23 Sep 2025 21:52:01 +0100 Subject: [PATCH] Bug 38115: (follow-up) Improve error handling and connection management Enhance the export_records.pl script with better error handling and connection lifecycle management: 1. Connection Management: - Always call disconnect() after file transport operations - Ensure connections are cleaned up even on errors 2. Error Handling: - Replace unusual die+pod2usage with clear error messages - Add file existence validation before upload attempts - Provide detailed error messages with context - Consistent error message formatting 3. User Experience: - Add validation for --delete_local_after_run without --destination_server_id - Provide informative success messages to STDERR - Better error messages with specific failure reasons 4. Robustness: - Check if output file was created before upload - Check if file exists before attempting deletion - Graceful handling of missing upload directories These improvements make the script more reliable and user-friendly while maintaining all existing functionality. --- misc/export_records.pl | 48 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/misc/export_records.pl b/misc/export_records.pl index 6b0044a5bb9..1be0423e2b7 100755 --- a/misc/export_records.pl +++ b/misc/export_records.pl @@ -132,6 +132,11 @@ if ($destination_server_id) { unless $file_transport; } +# Validate flag combinations +if ( $delete_local_after_run && !$destination_server_id ) { + pod2usage("--delete_local_after_run requires --destination_server_id to be specified"); +} + if ($report_id) { # Check report exists @@ -355,23 +360,48 @@ if ($deleted_barcodes) { } if ($file_transport) { - $file_transport->connect - or die pod2usage( sprintf( "Unable to connect server (%s)", $destination_server_id ) ); + # Verify the file was created successfully before attempting upload + unless ( -f $filename ) { + die "Error: Output file '$filename' was not created successfully\n"; + } + + # Connect to the transport + unless ( $file_transport->connect ) { + die sprintf( "Error: Unable to connect to file transport server (ID: %s)\n", $destination_server_id ); + } + + # Change to upload directory if specified my $upload_dir = $file_transport->upload_directory; if ($upload_dir) { - $file_transport->change_directory($upload_dir) - or die pod2usage( - sprintf( "Unable to change directory on server (%s) to path (%s)", $destination_server_id, $upload_dir ) ); + unless ( $file_transport->change_directory($upload_dir) ) { + $file_transport->disconnect; + die sprintf( + "Error: Unable to change to upload directory '%s' on server (ID: %s)\n", $upload_dir, + $destination_server_id + ); + } + } + + # Upload the file + unless ( $file_transport->upload_file( $filename, $filename ) ) { + $file_transport->disconnect; + die sprintf( "Error: Unable to upload file '%s' to server (ID: %s)\n", $filename, $destination_server_id ); } - $file_transport->upload_file( $filename, $filename ) - or die pod2usage( sprintf( "Unable to upload file (%s) to server (%s)", $filename, $destination_server_id ) ); + # Always disconnect when done + $file_transport->disconnect; + + print STDERR "Successfully uploaded '$filename' to file transport server (ID: $destination_server_id)\n"; } if ($delete_local_after_run) { - unlink $filename - or die pod2usage( sprintf( "Unable to delete local file (%s)", $filename ) ); + if ( -f $filename ) { + unless ( unlink $filename ) { + die sprintf( "Error: Unable to delete local file '%s': %s\n", $filename, $! ); + } + print STDERR "Successfully deleted local file '$filename'\n"; + } } exit; -- 2.51.0