refactor custom exceptions

This commit is contained in:
Thomas Fussell 2016-07-29 18:55:49 -04:00
parent 8efd646c2b
commit 5bd1a79536
17 changed files with 127 additions and 174 deletions

View File

@ -34,11 +34,10 @@ namespace xlnt {
/// <summary> /// <summary>
/// Parent type of all custom exceptions thrown in this library. /// Parent type of all custom exceptions thrown in this library.
/// </summary> /// </summary>
class XLNT_CLASS error : public std::runtime_error class XLNT_CLASS exception : public std::runtime_error
{ {
public: public:
error(); exception(const std::string &message);
error(const std::string &message);
void set_message(const std::string &message); void set_message(const std::string &message);
@ -46,122 +45,114 @@ private:
std::string message_; std::string message_;
}; };
class XLNT_CLASS value_error : public error /// <summary>
/// Exception for a bad parameter value
/// </summary>
class XLNT_CLASS invalid_parameter : public exception
{ {
public: public:
value_error(); invalid_parameter();
}; };
/// <summary> /// <summary>
/// Error for string encoding not matching workbook encoding /// Exception for bad sheet names.
/// </summary> /// </summary>
class XLNT_CLASS unicode_decode_error : public error class XLNT_CLASS invalid_sheet_title : public exception
{ {
public: public:
unicode_decode_error(); invalid_sheet_title(const std::string &title);
unicode_decode_error(char c);
unicode_decode_error(std::uint8_t b);
}; };
/// <summary> /// <summary>
/// Error for bad sheet names. /// Exception for incorrectly formatted named ranges.
/// </summary> /// </summary>
class XLNT_CLASS sheet_title_error : public error class XLNT_CLASS invalid_named_range : public exception
{ {
public: public:
sheet_title_error(const std::string &title); invalid_named_range();
}; };
/// <summary> /// <summary>
/// Error for trying to modify a read-only workbook. /// Exception when a referenced number format is not in the stylesheet.
/// </summary> /// </summary>
class XLNT_CLASS read_only_workbook_error : public error class XLNT_CLASS missing_number_format : public exception
{
public:
read_only_workbook_error();
};
/// <summary>
/// Error for incorrectly formatted named ranges.
/// </summary>
class XLNT_CLASS named_range_error : public error
{
public:
named_range_error();
};
/// <summary>
/// Error when a referenced number format is not in the stylesheet.
/// </summary>
class XLNT_CLASS missing_number_format : public error
{ {
public: public:
missing_number_format(); missing_number_format();
}; };
/// <summary> /// <summary>
/// Error for trying to open a non-OOXML file. /// Exception for trying to open a non-XLSX file.
/// </summary> /// </summary>
class XLNT_CLASS invalid_file_error : public error class XLNT_CLASS invalid_file : public exception
{ {
public: public:
invalid_file_error(const std::string &filename); invalid_file(const std::string &filename);
}; };
/// <summary> /// <summary>
/// The data submitted which cannot be used directly in Excel files. It /// The data submitted which cannot be used directly in Excel files. It
/// must be removed or escaped. /// must be removed or escaped.
/// </summary> /// </summary>
class XLNT_CLASS illegal_character_error : public error class XLNT_CLASS illegal_character : public exception
{ {
public: public:
illegal_character_error(char c); illegal_character(char c);
}; };
/// <summary> /// <summary>
/// Error for any data type inconsistencies. /// Exception for any data type inconsistencies.
/// </summary> /// </summary>
class XLNT_CLASS data_type_error : public error class XLNT_CLASS invalid_data_type : public exception
{ {
public: public:
data_type_error(); invalid_data_type();
}; };
/// <summary> /// <summary>
/// Error for bad column names in A1-style cell references. /// Exception for bad column names in A1-style cell references.
/// </summary> /// </summary>
class XLNT_CLASS column_string_index_error : public error class XLNT_CLASS invalid_column_string_index : public exception
{ {
public: public:
column_string_index_error(); invalid_column_string_index();
}; };
/// <summary> /// <summary>
/// Error for converting between numeric and A1-style cell references. /// Exception for converting between numeric and A1-style cell references.
/// </summary> /// </summary>
class XLNT_CLASS cell_coordinates_error : public error class XLNT_CLASS invalid_cell_reference : public exception
{ {
public: public:
cell_coordinates_error(column_t column, row_t row); invalid_cell_reference(column_t column, row_t row);
cell_coordinates_error(const std::string &coord_string); invalid_cell_reference(const std::string &reference_string);
}; };
/// <summary> /// <summary>
/// Error when an attribute value is invalid. /// Exception when setting a class's attribute to an invalid value
/// </summary> /// </summary>
class XLNT_CLASS attribute_error : public error class XLNT_CLASS invalid_attribute : public exception
{ {
public: public:
attribute_error(); invalid_attribute();
}; };
/// <summary> /// <summary>
/// key_error /// Exception for a key that doesn't exist in a container
/// </summary> /// </summary>
class XLNT_CLASS key_error : public error class XLNT_CLASS key_not_found : public exception
{ {
public: public:
key_error(); key_not_found();
};
/// <summary>
/// Exception for a workbook with no visible worksheets
/// </summary>
class XLNT_CLASS no_visible_worksheets : public exception
{
public:
no_visible_worksheets();
}; };
} // namespace xlnt } // namespace xlnt

View File

@ -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))) 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()) if (hyperlink.length() == 0 || std::find(hyperlink.begin(), hyperlink.end(), ':') == hyperlink.end())
{ {
throw data_type_error(); throw invalid_data_type();
} }
d_->has_hyperlink_ = true; d_->has_hyperlink_ = true;
@ -472,7 +472,7 @@ void cell::set_formula(const std::string &formula)
{ {
if (formula.length() == 0) if (formula.length() == 0)
{ {
throw data_type_error(); throw invalid_data_type();
} }
if (formula[0] == '=') if (formula[0] == '=')
@ -494,7 +494,7 @@ std::string cell::get_formula() const
{ {
if (d_->formula_.empty()) if (d_->formula_.empty())
{ {
throw data_type_error(); throw invalid_data_type();
} }
return d_->formula_; return d_->formula_;
@ -509,7 +509,7 @@ void cell::set_comment(const xlnt::comment &c)
{ {
if (c.d_ != d_->comment_.get()) if (c.d_ != d_->comment_.get())
{ {
throw xlnt::attribute_error(); throw xlnt::invalid_attribute();
} }
if (!has_comment()) if (!has_comment())
@ -539,7 +539,7 @@ void cell::set_error(const std::string &error)
{ {
if (error.length() == 0 || error[0] != '#') if (error.length() == 0 || error[0] != '#')
{ {
throw data_type_error(); throw invalid_data_type();
} }
d_->value_text_.set_plain_string(error); d_->value_text_.set_plain_string(error);

View File

@ -74,7 +74,7 @@ cell_reference::cell_reference(column_t column_index, row_t row)
|| !(row_ <= constants::max_row()) || !(row_ <= constants::max_row())
|| !(column_ <= constants::max_column())) || !(column_ <= constants::max_column()))
{ {
throw cell_coordinates_error(column_, row_); throw invalid_cell_reference(column_, row_);
} }
} }
@ -138,7 +138,7 @@ std::pair<std::string, row_t> cell_reference::split_reference(const std::string
} }
else else
{ {
throw cell_coordinates_error(reference_string); throw invalid_cell_reference(reference_string);
} }
} }
else if (character == '$') else if (character == '$')
@ -163,7 +163,7 @@ std::pair<std::string, row_t> cell_reference::split_reference(const std::string
} }
else if (!std::isdigit(character, std::locale::classic())) 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<std::string, row_t> cell_reference::split_reference(const std::string
if (row_string.length() == 0) if (row_string.length() == 0)
{ {
throw cell_coordinates_error(reference_string); throw invalid_cell_reference(reference_string);
} }
if (column_string[0] == '$') if (column_string[0] == '$')

View File

@ -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()) 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; 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<std::size_t>(i)], std::locale::classic())) if (!std::isalpha(column_string[static_cast<std::size_t>(i)], std::locale::classic()))
{ {
throw column_string_index_error(); throw invalid_column_string_index();
} }
auto char_index = std::toupper(column_string[static_cast<std::size_t>(i)], std::locale::classic()) - 'A'; auto char_index = std::toupper(column_string[static_cast<std::size_t>(i)], std::locale::classic()) - 'A';
@ -65,8 +65,7 @@ std::string column_t::column_string_from_index(column_t::index_t column_index)
// columns // columns
if (column_index < constants::min_column() || column_index > constants::max_column()) if (column_index < constants::min_column() || column_index > constants::max_column())
{ {
// auto msg = "Column index out of bounds: " + std::to_string(column_index); throw invalid_column_string_index();
throw column_string_index_error();
} }
int temp = static_cast<int>(column_index); int temp = static_cast<int>(column_index);

View File

@ -264,7 +264,7 @@ public:
for(auto i : illegal_chrs) for(auto i : illegal_chrs)
{ {
std::string str(1, i); 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)); cell.set_value(std::string(1, 33));
@ -315,7 +315,7 @@ public:
xlnt::comment comm(cell, "text", "author"); xlnt::comment comm(cell, "text", "author");
auto c2 = ws.get_cell(xlnt::cell_reference(1, 2)); 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() 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_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("A1&"), xlnt::invalid_cell_reference);
TS_ASSERT_THROWS(xlnt::cell_reference("A"), xlnt::cell_coordinates_error); TS_ASSERT_THROWS(xlnt::cell_reference("A"), xlnt::invalid_cell_reference);
auto ref = xlnt::cell_reference("$B$7"); auto ref = xlnt::cell_reference("$B$7");
TS_ASSERT(ref.row_absolute()); TS_ASSERT(ref.row_absolute());

View File

@ -10,14 +10,14 @@ class test_index_types : public CxxTest::TestSuite
public: public:
void test_bad_string() 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(""), xlnt::invalid_column_string_index);
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("ABCD"), xlnt::invalid_column_string_index);
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("123"), xlnt::invalid_column_string_index);
} }
void test_bad_column() 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() void test_column_operators()

View File

@ -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())) 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()); 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") 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())) 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) catch (std::runtime_error)
{ {
throw invalid_file_error(filename); throw invalid_file(filename);
} }
return ::load_workbook(archive_, guess_types, data_only, workbook_, get_stylesheet()); return ::load_workbook(archive_, guess_types, data_only, workbook_, get_stylesheet());

View File

@ -54,7 +54,6 @@ struct workbook_impl
app_properties_(other.app_properties_), app_properties_(other.app_properties_),
guess_types_(other.guess_types_), guess_types_(other.guess_types_),
data_only_(other.data_only_), data_only_(other.data_only_),
read_only_(other.read_only_),
stylesheet_(other.stylesheet_), stylesheet_(other.stylesheet_),
manifest_(other.manifest_) manifest_(other.manifest_)
{ {
@ -76,7 +75,6 @@ struct workbook_impl
app_properties_ = other.app_properties_; app_properties_ = other.app_properties_;
guess_types_ = other.guess_types_; guess_types_ = other.guess_types_;
data_only_ = other.data_only_; data_only_ = other.data_only_;
read_only_ = other.read_only_;
manifest_ = other.manifest_; manifest_ = other.manifest_;
return *this; return *this;
@ -93,7 +91,6 @@ struct workbook_impl
bool guess_types_; bool guess_types_;
bool data_only_; bool data_only_;
bool read_only_;
stylesheet stylesheet_; stylesheet stylesheet_;

View File

@ -244,7 +244,7 @@ void workbook_serializer::write_workbook(pugi::xml_document &xml) const
if (num_visible == 0) if (num_visible == 0)
{ {
throw xlnt::value_error(); throw xlnt::no_visible_worksheets();
} }
auto root_node = xml.append_child("workbook"); auto root_node = xml.append_child("workbook");

View File

@ -26,89 +26,75 @@
namespace xlnt { namespace xlnt {
error::error() : std::runtime_error("xlnt::error") exception::exception(const std::string &message)
{
}
error::error(const std::string &message)
: std::runtime_error("xlnt::error : " + message) : std::runtime_error("xlnt::error : " + message)
{ {
set_message(message); set_message(message);
} }
void error::set_message(const std::string &message) void exception::set_message(const std::string &message)
{ {
message_ = message; message_ = message;
} }
sheet_title_error::sheet_title_error(const std::string &title) invalid_sheet_title::invalid_sheet_title(const std::string &title)
: error(std::string("bad worksheet title: ") + title) : exception(std::string("bad worksheet title: ") + title)
{ {
} }
column_string_index_error::column_string_index_error() invalid_column_string_index::invalid_column_string_index()
: error("column string index error") : exception("column string index error")
{ {
} }
data_type_error::data_type_error() invalid_data_type::invalid_data_type()
: error("data type error") : exception("data type error")
{ {
} }
named_range_error::named_range_error() invalid_named_range::invalid_named_range()
: error("named range not found or not owned by this worksheet") : exception("named range not found or not owned by this worksheet")
{ {
} }
invalid_file_error::invalid_file_error(const std::string &filename) invalid_file::invalid_file(const std::string &filename)
: error(std::string("couldn't open file: (") + filename + ")") : exception(std::string("couldn't open file: (") + filename + ")")
{ {
} }
cell_coordinates_error::cell_coordinates_error(column_t column, row_t row) invalid_cell_reference::invalid_cell_reference(column_t column, row_t row)
: error(std::string("bad cell coordinates: (") + std::to_string(column.index) + ", " + std::to_string(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) invalid_cell_reference::invalid_cell_reference(const std::string &coord_string)
: error(std::string("bad cell coordinates: (") + (coord_string.empty() ? "<empty>" : coord_string) + ")") : exception(std::string("bad cell coordinates: (") + (coord_string.empty() ? "<empty>" : coord_string) + ")")
{ {
} }
illegal_character_error::illegal_character_error(char c) illegal_character::illegal_character(char c)
: error(std::string("illegal character: (") + std::to_string(static_cast<unsigned char>(c)) + ")") : exception(std::string("illegal character: (") + std::to_string(static_cast<unsigned char>(c)) + ")")
{ {
} }
unicode_decode_error::unicode_decode_error() invalid_parameter::invalid_parameter()
: error("unicode decode error") : exception("invalid parameter")
{ {
} }
unicode_decode_error::unicode_decode_error(char) invalid_attribute::invalid_attribute()
: error("unicode decode error") : exception("bad attribute")
{ {
} }
value_error::value_error() key_not_found::key_not_found()
: error("value error") : exception("key not found in container")
{ {
} }
read_only_workbook_error::read_only_workbook_error() no_visible_worksheets::no_visible_worksheets()
: error("workbook is read-only") : exception("workbook needs at least one non-hidden worksheet to be saved")
{
}
attribute_error::attribute_error()
: error("bad attribute")
{
}
key_error::key_error()
: error("key not found in container")
{ {
} }

View File

@ -86,7 +86,7 @@ std::vector<std::pair<std::string, std::string>> split_named_range(const std::st
{ {
xlnt::range_reference ref(split[1]); xlnt::range_reference ref(split[1]);
} }
catch (xlnt::cell_coordinates_error) catch (xlnt::invalid_cell_reference)
{ {
split[1] = ""; split[1] = "";
} }

View File

@ -68,7 +68,7 @@ public:
xlnt::workbook wb; xlnt::workbook wb;
xlnt::excel_serializer serializer(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() void test_read_empty_archive()
@ -78,7 +78,7 @@ public:
xlnt::workbook wb; xlnt::workbook wb;
xlnt::excel_serializer serializer(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() void test_read_workbook_with_no_properties()
@ -471,7 +471,7 @@ public:
xlnt::workbook wb; xlnt::workbook wb;
xlnt::excel_serializer serializer(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() void test_bad_formats_xls()
@ -481,7 +481,7 @@ public:
xlnt::workbook wb; xlnt::workbook wb;
xlnt::excel_serializer serializer(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() void test_bad_formats_no()
@ -491,7 +491,7 @@ public:
xlnt::workbook wb; xlnt::workbook wb;
xlnt::excel_serializer serializer(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() void test_determine_document_type()
{ {
xlnt::workbook wb; 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("1_empty.txt")), xlnt::invalid_file);
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("2_not-empty.txt")), xlnt::invalid_file);
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("3_empty.zip")), xlnt::invalid_file);
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("4_not-package.zip")), xlnt::invalid_file);
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("5_visio.vsdx")), xlnt::invalid_file);
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("6_document.docx")), xlnt::invalid_file);
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("7_presentation.pptx")), xlnt::invalid_file);
} }
}; };

View File

@ -61,7 +61,7 @@ public:
{ {
xlnt::workbook wb1, wb2; xlnt::workbook wb1, wb2;
auto new_sheet = wb1.get_active_sheet(); 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); 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<int>(), 2); TS_ASSERT_EQUALS(wb.get_sheet_by_index(1).get_cell("B3").get_value<int>(), 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() void test_remove_sheet()
{ {
xlnt::workbook wb, wb2; xlnt::workbook wb, wb2;
@ -103,9 +96,9 @@ public:
new_sheet.set_title(title); new_sheet.set_title(title);
auto found_sheet = wb.get_sheet_by_name(title); auto found_sheet = wb.get_sheet_by_name(title);
TS_ASSERT_EQUALS(new_sheet, found_sheet); 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; 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() void test_get_sheet_by_name_const()
@ -123,7 +116,7 @@ public:
{ {
xlnt::workbook wb; xlnt::workbook wb;
TS_ASSERT_THROWS_NOTHING(wb["Sheet"]); 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++ // void test_delitem() {} doesn't make sense in c++

View File

@ -66,7 +66,7 @@ public:
xlnt::workbook_serializer serializer(wb); xlnt::workbook_serializer serializer(wb);
pugi::xml_document xml; 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() void test_write_empty_workbook()

View File

@ -62,8 +62,7 @@ namespace detail {
workbook_impl::workbook_impl() workbook_impl::workbook_impl()
: active_sheet_index_(0), : active_sheet_index_(0),
guess_types_(false), guess_types_(false),
data_only_(false), data_only_(false)
read_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) 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) 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() worksheet workbook::create_sheet()
{ {
if(get_read_only()) throw xlnt::read_only_workbook_error();
std::string title = "Sheet"; std::string title = "Sheet";
int index = 0; int index = 0;
@ -174,7 +171,7 @@ worksheet workbook::create_sheet()
void workbook::copy_sheet(xlnt::worksheet worksheet) 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_); xlnt::detail::worksheet_impl impl(*worksheet.d_);
auto new_sheet = create_sheet(); auto new_sheet = create_sheet();
@ -358,14 +355,14 @@ worksheet workbook::create_sheet(const std::string &title)
{ {
if (title.length() > 31) if (title.length() > 31)
{ {
throw sheet_title_error(title); throw invalid_sheet_title(title);
} }
if (std::find_if(title.begin(), title.end(), [](char c) { if (std::find_if(title.begin(), title.end(), [](char c) {
return c == '*' || c == ':' || c == '/' || c == '\\' || c == '?' || c == '[' || c == ']'; return c == '*' || c == ':' || c == '/' || c == '\\' || c == '?' || c == '[' || c == ']';
}) != title.end()) }) != title.end())
{ {
throw sheet_title_error(title); throw invalid_sheet_title(title);
} }
std::string unique_title = title; std::string unique_title = title;
@ -565,16 +562,6 @@ void workbook::set_data_only(bool data_only)
d_->data_only_ = 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*/) void workbook::set_code_name(const std::string & /*code_name*/)
{ {
} }

View File

@ -30,7 +30,7 @@ public:
{ {
xlnt::workbook wb; xlnt::workbook wb;
xlnt::worksheet ws(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() void test_worksheet_dimension()
@ -206,7 +206,7 @@ public:
{ {
xlnt::workbook wb; xlnt::workbook wb;
xlnt::worksheet ws(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() void test_get_named_range_wrong_sheet()
@ -217,7 +217,7 @@ public:
auto ws1 = wb[0]; auto ws1 = wb[0];
auto ws2 = wb[1]; auto ws2 = wb[1];
wb.create_named_range("wrong_sheet_range", ws1, "C5"); 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() void test_remove_named_range_bad()

View File

@ -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)) if (!get_workbook().has_named_range(name))
{ {
throw key_error(); throw key_not_found();
} }
if (!has_named_range(name)) if (!has_named_range(name))
{ {
throw named_range_error(); throw key_not_found();
} }
return get_range(d_->named_ranges_[name].get_targets()[0].second); return get_range(d_->named_ranges_[name].get_targets()[0].second);