Skip to content

Commit b43e234

Browse files
committed
error handling for file open failure.
1 parent 1064e01 commit b43e234

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/Log/Destination/File.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public function close(): void
8383

8484
public function write( string $text, Log\Data $data ): void
8585
{
86+
// Only write if file handle is valid
87+
if( !$this->_file || !is_resource( $this->_file ) )
88+
{
89+
return;
90+
}
91+
8692
fwrite( $this->_file,
8793
"$text\r\n" );
8894
}

tests/Log/Destination/FileTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,24 @@ public function testOpenFail()
4242

4343
$this->assertFalse( $Pass );
4444
}
45+
46+
public function testWriteWithInvalidFileHandle()
47+
{
48+
$File = new File( new CSV() );
49+
50+
// Open should fail
51+
$Pass = $File->open(
52+
[
53+
'file_name' => "//invalid//path/%DATE%"
54+
]
55+
);
56+
57+
$this->assertFalse( $Pass );
58+
59+
// Write should not crash even with invalid file handle
60+
$File->log( "Test message", RunLevel::ERROR );
61+
62+
// Test passes if no exception is thrown
63+
$this->assertTrue( true );
64+
}
4565
}

0 commit comments

Comments
 (0)