From 5bd1a79536c0d51b51bc53d17f887bc973076591 Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Fri, 29 Jul 2016 18:55:49 -0400 Subject: [PATCH] refactor custom exceptions --- include/xlnt/utils/exceptions.hpp | 99 +++++++++---------- source/cell/cell.cpp | 12 +-- source/cell/cell_reference.cpp | 8 +- source/cell/index_types.cpp | 7 +- source/cell/tests/test_cell.hpp | 8 +- source/cell/tests/test_index_types.hpp | 8 +- source/detail/excel_serializer.cpp | 6 +- source/detail/workbook_impl.hpp | 3 - source/detail/workbook_serializer.cpp | 2 +- source/utils/exceptions.cpp | 66 +++++-------- source/workbook/named_range.cpp | 2 +- source/workbook/tests/test_read.hpp | 24 ++--- source/workbook/tests/test_workbook.hpp | 15 +-- source/workbook/tests/test_write_workbook.hpp | 2 +- source/workbook/workbook.cpp | 25 ++--- source/worksheet/tests/test_worksheet.hpp | 6 +- source/worksheet/worksheet.cpp | 8 +- 17 files changed, 127 insertions(+), 174 deletions(-) diff --git a/include/xlnt/utils/exceptions.hpp b/include/xlnt/utils/exceptions.hpp index a0a38a3f..70a92ecd 100644 --- a/include/xlnt/utils/exceptions.hpp +++ b/include/xlnt/utils/exceptions.hpp @@ -34,11 +34,10 @@ namespace xlnt { /// /// Parent type of all custom exceptions thrown in this library. /// -class XLNT_CLASS error : public std::runtime_error +class XLNT_CLASS exception : public std::runtime_error { public: - error(); - error(const std::string &message); + exception(const std::string &message); void set_message(const std::string &message); @@ -46,122 +45,114 @@ private: std::string message_; }; -class XLNT_CLASS value_error : public error +/// +/// Exception for a bad parameter value +/// +class XLNT_CLASS invalid_parameter : public exception { public: - value_error(); + invalid_parameter(); }; /// -/// Error for string encoding not matching workbook encoding +/// Exception for bad sheet names. /// -class XLNT_CLASS unicode_decode_error : public error +class XLNT_CLASS invalid_sheet_title : public exception { public: - unicode_decode_error(); - unicode_decode_error(char c); - unicode_decode_error(std::uint8_t b); + invalid_sheet_title(const std::string &title); }; /// -/// Error for bad sheet names. +/// Exception for incorrectly formatted named ranges. /// -class XLNT_CLASS sheet_title_error : public error +class XLNT_CLASS invalid_named_range : public exception { public: - sheet_title_error(const std::string &title); + invalid_named_range(); }; /// -/// Error for trying to modify a read-only workbook. +/// Exception when a referenced number format is not in the stylesheet. /// -class XLNT_CLASS read_only_workbook_error : public error -{ -public: - read_only_workbook_error(); -}; - -/// -/// Error for incorrectly formatted named ranges. -/// -class XLNT_CLASS named_range_error : public error -{ -public: - named_range_error(); -}; - -/// -/// Error when a referenced number format is not in the stylesheet. -/// -class XLNT_CLASS missing_number_format : public error +class XLNT_CLASS missing_number_format : public exception { public: missing_number_format(); }; /// -/// Error for trying to open a non-OOXML file. +/// Exception for trying to open a non-XLSX file. /// -class XLNT_CLASS invalid_file_error : public error +class XLNT_CLASS invalid_file : public exception { public: - invalid_file_error(const std::string &filename); + invalid_file(const std::string &filename); }; /// /// The data submitted which cannot be used directly in Excel files. It /// must be removed or escaped. /// -class XLNT_CLASS illegal_character_error : public error +class XLNT_CLASS illegal_character : public exception { public: - illegal_character_error(char c); + illegal_character(char c); }; /// -/// Error for any data type inconsistencies. +/// Exception for any data type inconsistencies. /// -class XLNT_CLASS data_type_error : public error +class XLNT_CLASS invalid_data_type : public exception { public: - data_type_error(); + invalid_data_type(); }; /// -/// Error for bad column names in A1-style cell references. +/// Exception for bad column names in A1-style cell references. /// -class XLNT_CLASS column_string_index_error : public error +class XLNT_CLASS invalid_column_string_index : public exception { public: - column_string_index_error(); + invalid_column_string_index(); }; /// -/// Error for converting between numeric and A1-style cell references. +/// Exception for converting between numeric and A1-style cell references. /// -class XLNT_CLASS cell_coordinates_error : public error +class XLNT_CLASS invalid_cell_reference : public exception { public: - cell_coordinates_error(column_t column, row_t row); - cell_coordinates_error(const std::string &coord_string); + invalid_cell_reference(column_t column, row_t row); + invalid_cell_reference(const std::string &reference_string); }; /// -/// Error when an attribute value is invalid. +/// Exception when setting a class's attribute to an invalid value /// -class XLNT_CLASS attribute_error : public error +class XLNT_CLASS invalid_attribute : public exception { public: - attribute_error(); + invalid_attribute(); }; /// -/// key_error +/// Exception for a key that doesn't exist in a container /// -class XLNT_CLASS key_error : public error +class XLNT_CLASS key_not_found : public exception { public: - key_error(); + key_not_found(); +}; + +/// +/// Exception for a workbook with no visible worksheets +/// +class XLNT_CLASS no_visible_worksheets : public exception +{ +public: + no_visible_worksheets(); }; } // namespace xlnt diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index be0385f8..5f5855a7 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -148,7 +148,7 @@ std::string cell::check_string(const std::string &to_check) { if (c >= 0 && (c <= 8 || c == 11 || c == 12 || (c >= 14 && c <= 31))) { - throw xlnt::illegal_character_error(c); + throw xlnt::illegal_character(c); } } @@ -456,7 +456,7 @@ void cell::set_hyperlink(const std::string &hyperlink) { if (hyperlink.length() == 0 || std::find(hyperlink.begin(), hyperlink.end(), ':') == hyperlink.end()) { - throw data_type_error(); + throw invalid_data_type(); } d_->has_hyperlink_ = true; @@ -472,7 +472,7 @@ void cell::set_formula(const std::string &formula) { if (formula.length() == 0) { - throw data_type_error(); + throw invalid_data_type(); } if (formula[0] == '=') @@ -494,7 +494,7 @@ std::string cell::get_formula() const { if (d_->formula_.empty()) { - throw data_type_error(); + throw invalid_data_type(); } return d_->formula_; @@ -509,7 +509,7 @@ void cell::set_comment(const xlnt::comment &c) { if (c.d_ != d_->comment_.get()) { - throw xlnt::attribute_error(); + throw xlnt::invalid_attribute(); } if (!has_comment()) @@ -539,7 +539,7 @@ void cell::set_error(const std::string &error) { if (error.length() == 0 || error[0] != '#') { - throw data_type_error(); + throw invalid_data_type(); } d_->value_text_.set_plain_string(error); diff --git a/source/cell/cell_reference.cpp b/source/cell/cell_reference.cpp index 57f3e4d9..b1a9e619 100644 --- a/source/cell/cell_reference.cpp +++ b/source/cell/cell_reference.cpp @@ -74,7 +74,7 @@ cell_reference::cell_reference(column_t column_index, row_t row) || !(row_ <= constants::max_row()) || !(column_ <= constants::max_column())) { - throw cell_coordinates_error(column_, row_); + throw invalid_cell_reference(column_, row_); } } @@ -138,7 +138,7 @@ std::pair cell_reference::split_reference(const std::string } else { - throw cell_coordinates_error(reference_string); + throw invalid_cell_reference(reference_string); } } else if (character == '$') @@ -163,7 +163,7 @@ std::pair cell_reference::split_reference(const std::string } else if (!std::isdigit(character, std::locale::classic())) { - throw cell_coordinates_error(reference_string); + throw invalid_cell_reference(reference_string); } } } @@ -172,7 +172,7 @@ std::pair cell_reference::split_reference(const std::string if (row_string.length() == 0) { - throw cell_coordinates_error(reference_string); + throw invalid_cell_reference(reference_string); } if (column_string[0] == '$') diff --git a/source/cell/index_types.cpp b/source/cell/index_types.cpp index 34a2c34c..1880c38c 100644 --- a/source/cell/index_types.cpp +++ b/source/cell/index_types.cpp @@ -33,7 +33,7 @@ column_t::index_t column_t::column_index_from_string(const std::string &column_s { if (column_string.length() > 3 || column_string.empty()) { - throw column_string_index_error(); + throw invalid_column_string_index(); } column_t::index_t column_index = 0; @@ -43,7 +43,7 @@ column_t::index_t column_t::column_index_from_string(const std::string &column_s { if (!std::isalpha(column_string[static_cast(i)], std::locale::classic())) { - throw column_string_index_error(); + throw invalid_column_string_index(); } auto char_index = std::toupper(column_string[static_cast(i)], std::locale::classic()) - 'A'; @@ -65,8 +65,7 @@ std::string column_t::column_string_from_index(column_t::index_t column_index) // columns if (column_index < constants::min_column() || column_index > constants::max_column()) { - // auto msg = "Column index out of bounds: " + std::to_string(column_index); - throw column_string_index_error(); + throw invalid_column_string_index(); } int temp = static_cast(column_index); diff --git a/source/cell/tests/test_cell.hpp b/source/cell/tests/test_cell.hpp index e1813163..39c085d4 100644 --- a/source/cell/tests/test_cell.hpp +++ b/source/cell/tests/test_cell.hpp @@ -264,7 +264,7 @@ public: for(auto i : illegal_chrs) { std::string str(1, i); - TS_ASSERT_THROWS(cell.set_value(str), xlnt::illegal_character_error); + TS_ASSERT_THROWS(cell.set_value(str), xlnt::illegal_character); } cell.set_value(std::string(1, 33)); @@ -315,7 +315,7 @@ public: xlnt::comment comm(cell, "text", "author"); auto c2 = ws.get_cell(xlnt::cell_reference(1, 2)); - TS_ASSERT_THROWS(c2.set_comment(comm), xlnt::attribute_error); + TS_ASSERT_THROWS(c2.set_comment(comm), xlnt::invalid_attribute); } void test_remove_comment() @@ -632,8 +632,8 @@ public: TS_ASSERT_EQUALS((xlnt::cell_reference("A1"), xlnt::cell_reference("B2")), xlnt::range_reference("A1:B2")); - TS_ASSERT_THROWS(xlnt::cell_reference("A1&"), xlnt::cell_coordinates_error); - TS_ASSERT_THROWS(xlnt::cell_reference("A"), xlnt::cell_coordinates_error); + TS_ASSERT_THROWS(xlnt::cell_reference("A1&"), xlnt::invalid_cell_reference); + TS_ASSERT_THROWS(xlnt::cell_reference("A"), xlnt::invalid_cell_reference); auto ref = xlnt::cell_reference("$B$7"); TS_ASSERT(ref.row_absolute()); diff --git a/source/cell/tests/test_index_types.hpp b/source/cell/tests/test_index_types.hpp index 91e0c651..dc217f95 100644 --- a/source/cell/tests/test_index_types.hpp +++ b/source/cell/tests/test_index_types.hpp @@ -10,14 +10,14 @@ class test_index_types : public CxxTest::TestSuite public: void test_bad_string() { - TS_ASSERT_THROWS(xlnt::column_t::column_index_from_string(""), xlnt::column_string_index_error); - TS_ASSERT_THROWS(xlnt::column_t::column_index_from_string("ABCD"), xlnt::column_string_index_error); - TS_ASSERT_THROWS(xlnt::column_t::column_index_from_string("123"), xlnt::column_string_index_error); + TS_ASSERT_THROWS(xlnt::column_t::column_index_from_string(""), xlnt::invalid_column_string_index); + TS_ASSERT_THROWS(xlnt::column_t::column_index_from_string("ABCD"), xlnt::invalid_column_string_index); + TS_ASSERT_THROWS(xlnt::column_t::column_index_from_string("123"), xlnt::invalid_column_string_index); } void test_bad_column() { - TS_ASSERT_THROWS(xlnt::column_t::column_string_from_index(0), xlnt::column_string_index_error); + TS_ASSERT_THROWS(xlnt::column_t::column_string_from_index(0), xlnt::invalid_column_string_index); } void test_column_operators() diff --git a/source/detail/excel_serializer.cpp b/source/detail/excel_serializer.cpp index 637ada01..6c03e18f 100644 --- a/source/detail/excel_serializer.cpp +++ b/source/detail/excel_serializer.cpp @@ -75,7 +75,7 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl if(!archive.has_file(xlnt::constants::part_content_types())) { - throw xlnt::invalid_file_error("missing [Content Types].xml"); + throw xlnt::invalid_file("missing [Content Types].xml"); } xlnt::manifest_serializer ms(wb.get_manifest()); @@ -85,7 +85,7 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl if (ms.determine_document_type() != "excel") { - throw xlnt::invalid_file_error("package is not an OOXML SpreadsheetML"); + throw xlnt::invalid_file("package is not an OOXML SpreadsheetML"); } if(archive.has_file(xlnt::constants::part_core())) @@ -218,7 +218,7 @@ bool excel_serializer::load_workbook(const std::string &filename, bool guess_typ } catch (std::runtime_error) { - throw invalid_file_error(filename); + throw invalid_file(filename); } return ::load_workbook(archive_, guess_types, data_only, workbook_, get_stylesheet()); diff --git a/source/detail/workbook_impl.hpp b/source/detail/workbook_impl.hpp index e94bbe8b..f048dde5 100644 --- a/source/detail/workbook_impl.hpp +++ b/source/detail/workbook_impl.hpp @@ -54,7 +54,6 @@ struct workbook_impl app_properties_(other.app_properties_), guess_types_(other.guess_types_), data_only_(other.data_only_), - read_only_(other.read_only_), stylesheet_(other.stylesheet_), manifest_(other.manifest_) { @@ -76,7 +75,6 @@ struct workbook_impl app_properties_ = other.app_properties_; guess_types_ = other.guess_types_; data_only_ = other.data_only_; - read_only_ = other.read_only_; manifest_ = other.manifest_; return *this; @@ -93,7 +91,6 @@ struct workbook_impl bool guess_types_; bool data_only_; - bool read_only_; stylesheet stylesheet_; diff --git a/source/detail/workbook_serializer.cpp b/source/detail/workbook_serializer.cpp index b3dee719..46a3dd20 100644 --- a/source/detail/workbook_serializer.cpp +++ b/source/detail/workbook_serializer.cpp @@ -244,7 +244,7 @@ void workbook_serializer::write_workbook(pugi::xml_document &xml) const if (num_visible == 0) { - throw xlnt::value_error(); + throw xlnt::no_visible_worksheets(); } auto root_node = xml.append_child("workbook"); diff --git a/source/utils/exceptions.cpp b/source/utils/exceptions.cpp index af6b1426..50eea8cf 100644 --- a/source/utils/exceptions.cpp +++ b/source/utils/exceptions.cpp @@ -26,89 +26,75 @@ namespace xlnt { -error::error() : std::runtime_error("xlnt::error") -{ -} - -error::error(const std::string &message) +exception::exception(const std::string &message) : std::runtime_error("xlnt::error : " + message) { set_message(message); } -void error::set_message(const std::string &message) +void exception::set_message(const std::string &message) { message_ = message; } -sheet_title_error::sheet_title_error(const std::string &title) - : error(std::string("bad worksheet title: ") + title) +invalid_sheet_title::invalid_sheet_title(const std::string &title) + : exception(std::string("bad worksheet title: ") + title) { } -column_string_index_error::column_string_index_error() - : error("column string index error") +invalid_column_string_index::invalid_column_string_index() + : exception("column string index error") { } -data_type_error::data_type_error() - : error("data type error") +invalid_data_type::invalid_data_type() + : exception("data type error") { } -named_range_error::named_range_error() - : error("named range not found or not owned by this worksheet") +invalid_named_range::invalid_named_range() + : exception("named range not found or not owned by this worksheet") { } -invalid_file_error::invalid_file_error(const std::string &filename) - : error(std::string("couldn't open file: (") + filename + ")") +invalid_file::invalid_file(const std::string &filename) + : exception(std::string("couldn't open file: (") + filename + ")") { } -cell_coordinates_error::cell_coordinates_error(column_t column, row_t row) - : error(std::string("bad cell coordinates: (") + std::to_string(column.index) + ", " + std::to_string(row) + +invalid_cell_reference::invalid_cell_reference(column_t column, row_t row) + : exception(std::string("bad cell coordinates: (") + std::to_string(column.index) + ", " + std::to_string(row) + ")") { } -cell_coordinates_error::cell_coordinates_error(const std::string &coord_string) - : error(std::string("bad cell coordinates: (") + (coord_string.empty() ? "" : coord_string) + ")") +invalid_cell_reference::invalid_cell_reference(const std::string &coord_string) + : exception(std::string("bad cell coordinates: (") + (coord_string.empty() ? "" : coord_string) + ")") { } -illegal_character_error::illegal_character_error(char c) - : error(std::string("illegal character: (") + std::to_string(static_cast(c)) + ")") +illegal_character::illegal_character(char c) + : exception(std::string("illegal character: (") + std::to_string(static_cast(c)) + ")") { } -unicode_decode_error::unicode_decode_error() - : error("unicode decode error") +invalid_parameter::invalid_parameter() + : exception("invalid parameter") { } -unicode_decode_error::unicode_decode_error(char) - : error("unicode decode error") +invalid_attribute::invalid_attribute() + : exception("bad attribute") { } -value_error::value_error() - : error("value error") +key_not_found::key_not_found() + : exception("key not found in container") { } -read_only_workbook_error::read_only_workbook_error() - : error("workbook is read-only") -{ -} - -attribute_error::attribute_error() - : error("bad attribute") -{ -} - -key_error::key_error() - : error("key not found in container") +no_visible_worksheets::no_visible_worksheets() + : exception("workbook needs at least one non-hidden worksheet to be saved") { } diff --git a/source/workbook/named_range.cpp b/source/workbook/named_range.cpp index bd220f8e..c2dcb536 100644 --- a/source/workbook/named_range.cpp +++ b/source/workbook/named_range.cpp @@ -86,7 +86,7 @@ std::vector> split_named_range(const std::st { xlnt::range_reference ref(split[1]); } - catch (xlnt::cell_coordinates_error) + catch (xlnt::invalid_cell_reference) { split[1] = ""; } diff --git a/source/workbook/tests/test_read.hpp b/source/workbook/tests/test_read.hpp index 1f41e9a6..16d35c6b 100644 --- a/source/workbook/tests/test_read.hpp +++ b/source/workbook/tests/test_read.hpp @@ -68,7 +68,7 @@ public: xlnt::workbook wb; xlnt::excel_serializer serializer(wb); - TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error); + TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file); } void test_read_empty_archive() @@ -78,7 +78,7 @@ public: xlnt::workbook wb; xlnt::excel_serializer serializer(wb); - TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error); + TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file); } void test_read_workbook_with_no_properties() @@ -471,7 +471,7 @@ public: xlnt::workbook wb; xlnt::excel_serializer serializer(wb); - TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error); + TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file); } void test_bad_formats_xls() @@ -481,7 +481,7 @@ public: xlnt::workbook wb; xlnt::excel_serializer serializer(wb); - TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error); + TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file); } void test_bad_formats_no() @@ -491,7 +491,7 @@ public: xlnt::workbook wb; xlnt::excel_serializer serializer(wb); - TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file_error); + TS_ASSERT_THROWS(serializer.load_workbook(path), xlnt::invalid_file); } @@ -557,12 +557,12 @@ public: void test_determine_document_type() { xlnt::workbook wb; - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("1_empty.txt")), xlnt::invalid_file_error); - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("2_not-empty.txt")), xlnt::invalid_file_error); - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("3_empty.zip")), xlnt::invalid_file_error); - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("4_not-package.zip")), xlnt::invalid_file_error); - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("5_visio.vsdx")), xlnt::invalid_file_error); - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("6_document.docx")), xlnt::invalid_file_error); - TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("7_presentation.pptx")), xlnt::invalid_file_error); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("1_empty.txt")), xlnt::invalid_file); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("2_not-empty.txt")), xlnt::invalid_file); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("3_empty.zip")), xlnt::invalid_file); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("4_not-package.zip")), xlnt::invalid_file); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("5_visio.vsdx")), xlnt::invalid_file); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("6_document.docx")), xlnt::invalid_file); + TS_ASSERT_THROWS(wb.load(path_helper::get_data_directory("7_presentation.pptx")), xlnt::invalid_file); } }; diff --git a/source/workbook/tests/test_workbook.hpp b/source/workbook/tests/test_workbook.hpp index 7e98f606..72b99939 100644 --- a/source/workbook/tests/test_workbook.hpp +++ b/source/workbook/tests/test_workbook.hpp @@ -61,7 +61,7 @@ public: { xlnt::workbook wb1, wb2; auto new_sheet = wb1.get_active_sheet(); - TS_ASSERT_THROWS(wb2.copy_sheet(new_sheet), xlnt::value_error); + TS_ASSERT_THROWS(wb2.copy_sheet(new_sheet), xlnt::invalid_parameter); TS_ASSERT_THROWS(wb2.get_index(new_sheet), std::runtime_error); } @@ -78,13 +78,6 @@ public: TS_ASSERT_EQUALS(wb.get_sheet_by_index(1).get_cell("B3").get_value(), 2); } - void test_create_sheet_readonly() - { - xlnt::workbook wb; - wb.set_read_only(true); - TS_ASSERT_THROWS(wb.create_sheet(), xlnt::read_only_workbook_error); - } - void test_remove_sheet() { xlnt::workbook wb, wb2; @@ -103,9 +96,9 @@ public: new_sheet.set_title(title); auto found_sheet = wb.get_sheet_by_name(title); TS_ASSERT_EQUALS(new_sheet, found_sheet); - TS_ASSERT_THROWS(wb.get_sheet_by_name("error"), xlnt::key_error); + TS_ASSERT_THROWS(wb.get_sheet_by_name("error"), xlnt::key_not_found); const auto &wb_const = wb; - TS_ASSERT_THROWS(wb_const.get_sheet_by_name("error"), xlnt::key_error); + TS_ASSERT_THROWS(wb_const.get_sheet_by_name("error"), xlnt::key_not_found); } void test_get_sheet_by_name_const() @@ -123,7 +116,7 @@ public: { xlnt::workbook wb; TS_ASSERT_THROWS_NOTHING(wb["Sheet"]); - TS_ASSERT_THROWS(wb["NotThere"], xlnt::key_error); + TS_ASSERT_THROWS(wb["NotThere"], xlnt::key_not_found); } // void test_delitem() {} doesn't make sense in c++ diff --git a/source/workbook/tests/test_write_workbook.hpp b/source/workbook/tests/test_write_workbook.hpp index 72282ce8..126e20ad 100644 --- a/source/workbook/tests/test_write_workbook.hpp +++ b/source/workbook/tests/test_write_workbook.hpp @@ -66,7 +66,7 @@ public: xlnt::workbook_serializer serializer(wb); pugi::xml_document xml; - TS_ASSERT_THROWS(serializer.write_workbook(xml), xlnt::value_error); + TS_ASSERT_THROWS(serializer.write_workbook(xml), xlnt::no_visible_worksheets); } void test_write_empty_workbook() diff --git a/source/workbook/workbook.cpp b/source/workbook/workbook.cpp index 900a2331..3c5fb7db 100644 --- a/source/workbook/workbook.cpp +++ b/source/workbook/workbook.cpp @@ -62,8 +62,7 @@ namespace detail { workbook_impl::workbook_impl() : active_sheet_index_(0), guess_types_(false), - data_only_(false), - read_only_(false) + data_only_(false) { } @@ -103,7 +102,7 @@ const worksheet workbook::get_sheet_by_name(const std::string &name) const } } - throw key_error(); + throw key_not_found(); } worksheet workbook::get_sheet_by_name(const std::string &name) @@ -116,7 +115,7 @@ worksheet workbook::get_sheet_by_name(const std::string &name) } } - throw key_error(); + throw key_not_found(); } worksheet workbook::get_sheet_by_index(std::size_t index) @@ -148,8 +147,6 @@ bool workbook::has_named_range(const std::string &name) const worksheet workbook::create_sheet() { - if(get_read_only()) throw xlnt::read_only_workbook_error(); - std::string title = "Sheet"; int index = 0; @@ -174,7 +171,7 @@ worksheet workbook::create_sheet() void workbook::copy_sheet(xlnt::worksheet worksheet) { - if(worksheet.d_->parent_ != this) throw xlnt::value_error(); + if(worksheet.d_->parent_ != this) throw xlnt::invalid_parameter(); xlnt::detail::worksheet_impl impl(*worksheet.d_); auto new_sheet = create_sheet(); @@ -358,14 +355,14 @@ worksheet workbook::create_sheet(const std::string &title) { if (title.length() > 31) { - throw sheet_title_error(title); + throw invalid_sheet_title(title); } if (std::find_if(title.begin(), title.end(), [](char c) { return c == '*' || c == ':' || c == '/' || c == '\\' || c == '?' || c == '[' || c == ']'; }) != title.end()) { - throw sheet_title_error(title); + throw invalid_sheet_title(title); } std::string unique_title = title; @@ -565,16 +562,6 @@ void workbook::set_data_only(bool data_only) d_->data_only_ = data_only; } -bool workbook::get_read_only() const -{ - return d_->read_only_; -} - -void workbook::set_read_only(bool read_only) -{ - d_->read_only_ = read_only; -} - void workbook::set_code_name(const std::string & /*code_name*/) { } diff --git a/source/worksheet/tests/test_worksheet.hpp b/source/worksheet/tests/test_worksheet.hpp index 7a8ce47a..ee4b2dc3 100644 --- a/source/worksheet/tests/test_worksheet.hpp +++ b/source/worksheet/tests/test_worksheet.hpp @@ -30,7 +30,7 @@ public: { xlnt::workbook wb; xlnt::worksheet ws(wb); - TS_ASSERT_THROWS(xlnt::cell_reference invalid(xlnt::column_t((xlnt::column_t::index_t)0), 0), xlnt::cell_coordinates_error); + TS_ASSERT_THROWS(xlnt::cell_reference invalid(xlnt::column_t((xlnt::column_t::index_t)0), 0), xlnt::invalid_cell_reference); } void test_worksheet_dimension() @@ -206,7 +206,7 @@ public: { xlnt::workbook wb; xlnt::worksheet ws(wb); - TS_ASSERT_THROWS(ws.get_named_range("bad_range"), xlnt::key_error); + TS_ASSERT_THROWS(ws.get_named_range("bad_range"), xlnt::key_not_found); } void test_get_named_range_wrong_sheet() @@ -217,7 +217,7 @@ public: auto ws1 = wb[0]; auto ws2 = wb[1]; wb.create_named_range("wrong_sheet_range", ws1, "C5"); - TS_ASSERT_THROWS(ws2.get_named_range("wrong_sheet_range"), xlnt::named_range_error); + TS_ASSERT_THROWS(ws2.get_named_range("wrong_sheet_range"), xlnt::key_not_found); } void test_remove_named_range_bad() diff --git a/source/worksheet/worksheet.cpp b/source/worksheet/worksheet.cpp index 498cbdc6..af501732 100644 --- a/source/worksheet/worksheet.cpp +++ b/source/worksheet/worksheet.cpp @@ -104,9 +104,9 @@ void worksheet::create_named_range(const std::string &name, const range_referenc } } - catch(xlnt::error) + catch(xlnt::exception) { - // not a valid cell reference + // must not be a valid cell reference } @@ -350,12 +350,12 @@ range worksheet::get_named_range(const std::string &name) { if (!get_workbook().has_named_range(name)) { - throw key_error(); + throw key_not_found(); } if (!has_named_range(name)) { - throw named_range_error(); + throw key_not_found(); } return get_range(d_->named_ranges_[name].get_targets()[0].second);