Skip to content

Commit

Permalink
More specific error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
quanghgx committed Oct 30, 2023
1 parent 6ccc031 commit 450175b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
13 changes: 7 additions & 6 deletions cpp/src/parquet/arrow/arrow_reader_writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5225,16 +5225,17 @@ TEST(TestArrowReadWrite, OperationsOnClosedWriter) {

// Operations on closed writer are incorrect. However, should not segfault
ASSERT_OK(writer->Close());
EXPECT_THROW_THAT(
[&]() { ASSERT_OK(writer->WriteTable(*table, 1)); }, ParquetException,
::testing::Property(&ParquetException::what,
::testing::HasSubstr("Cannot do operation on closed file")));
EXPECT_THROW_THAT([&]() { ASSERT_OK(writer->WriteTable(*table, 1)); }, ParquetException,
::testing::Property(
&ParquetException::what,
::testing::HasSubstr("Cannot get properties from closed file")));

// These two functions do not throw because they use PARQUET_CATCH_NOT_OK
// in their implementations.
ASSERT_RAISES_WITH_MESSAGE(IOError, "IOError: Cannot do operation on closed file",
ASSERT_RAISES_WITH_MESSAGE(IOError,
"IOError: Cannot append buffered-row-group to closed file",
writer->NewBufferedRowGroup());
ASSERT_RAISES_WITH_MESSAGE(IOError, "IOError: Cannot do operation on closed file",
ASSERT_RAISES_WITH_MESSAGE(IOError, "IOError: Cannot append row-group to closed file",
writer->NewRowGroup(1));
}

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/parquet/file_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,12 @@ void ParquetFileWriter::Close() {
}

RowGroupWriter* ParquetFileWriter::AppendRowGroup() {
AssertNotClosed();
AssertNotClosed("Cannot append row-group to closed file");
return contents_->AppendRowGroup();
}

RowGroupWriter* ParquetFileWriter::AppendBufferedRowGroup() {
AssertNotClosed();
AssertNotClosed("Cannot append buffered-row-group to closed file");
return contents_->AppendBufferedRowGroup();
}

Expand All @@ -650,18 +650,18 @@ RowGroupWriter* ParquetFileWriter::AppendRowGroup(int64_t num_rows) {

void ParquetFileWriter::AddKeyValueMetadata(
const std::shared_ptr<const KeyValueMetadata>& key_value_metadata) {
AssertNotClosed();
AssertNotClosed("Cannot add key-value metadata to closed file");
contents_->AddKeyValueMetadata(key_value_metadata);
}

const std::shared_ptr<WriterProperties>& ParquetFileWriter::properties() const {
AssertNotClosed();
AssertNotClosed("Cannot get properties from closed file");
return contents_->properties();
}

void ParquetFileWriter::AssertNotClosed() const {
void ParquetFileWriter::AssertNotClosed(std::string_view message) const {
if (contents_ == nullptr) {
throw ParquetException("Cannot do operation on closed file");
throw ParquetException(message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/parquet/file_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class PARQUET_EXPORT ParquetFileWriter {
const std::shared_ptr<FileMetaData> metadata() const;

/// Verify file is not closed.
void AssertNotClosed() const;
void AssertNotClosed(std::string_view message) const;

private:
// Holds a pointer to an instance of Contents implementation
Expand Down

0 comments on commit 450175b

Please sign in to comment.