Skip to content

Commit

Permalink
[C] Changed errors descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
seleznevae committed Feb 23, 2020
1 parent d91c7ce commit cd11dfd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
12 changes: 6 additions & 6 deletions lib/fort.c
Original file line number Diff line number Diff line change
Expand Up @@ -3618,18 +3618,18 @@ const char *ft_strerror(int error_code)
{
switch (error_code) {
case FT_MEMORY_ERROR:
return "Libfort error (out of memory)";
return "Out of memory";
case FT_GEN_ERROR:
return "Libfort error (general error)";
return "General error";
case FT_EINVAL:
return "Libfort error (invalid argument)";
return "Invalid argument";
case FT_INTERN_ERROR:
return "Libfort error (internal logic error)";
return "Internal libfort error";
default:
if (error_code < 0)
return "Libfort unknown error";
return "Unknown error code";
else
return "Libfort success";
return "Success";
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/fort.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ SOFTWARE.

/**
* Libfort internal logic error.
*
* Usually such errors mean that something is wrong in
* libfort internal logic and in most of cases cause of
* these errors is a library bug.
*/
#define FT_INTERN_ERROR -3

/**
* General error.
*
* Different errors that do not belong to the group of errors
* mentioned above.
*/
#define FT_GEN_ERROR -4

Expand Down
9 changes: 4 additions & 5 deletions lib/fort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@ class table: public property_owner<table<TT>>
: property_owner_t(FT_ANY_ROW, FT_ANY_COLUMN, this),
table_(ft_create_table())
{

if (table_ == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::bad_alloc();
}

/**
Expand All @@ -413,7 +412,7 @@ class table: public property_owner<table<TT>>
if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table_);
if (table_copy == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::runtime_error("Error during table copy");

stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
Expand Down Expand Up @@ -447,7 +446,7 @@ class table: public property_owner<table<TT>>
if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table_);
if (table_copy == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::runtime_error("Error during table copy");

stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
Expand Down Expand Up @@ -491,7 +490,7 @@ class table: public property_owner<table<TT>>
{
const char *str = c_str();
if (str == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::runtime_error("Error during table to string conversion");
return str;
}

Expand Down
7 changes: 7 additions & 0 deletions src/fort.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ SOFTWARE.

/**
* Libfort internal logic error.
*
* Usually such errors mean that something is wrong in
* libfort internal logic and in most of cases cause of
* these errors is a library bug.
*/
#define FT_INTERN_ERROR -3

/**
* General error.
*
* Different errors that do not belong to the group of errors
* mentioned above.
*/
#define FT_GEN_ERROR -4

Expand Down
9 changes: 4 additions & 5 deletions src/fort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@ class table: public property_owner<table<TT>>
: property_owner_t(FT_ANY_ROW, FT_ANY_COLUMN, this),
table_(ft_create_table())
{

if (table_ == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::bad_alloc();
}

/**
Expand All @@ -413,7 +412,7 @@ class table: public property_owner<table<TT>>
if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table_);
if (table_copy == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::runtime_error("Error during table copy");

stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
Expand Down Expand Up @@ -447,7 +446,7 @@ class table: public property_owner<table<TT>>
if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table_);
if (table_copy == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::runtime_error("Error during table copy");

stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
Expand Down Expand Up @@ -491,7 +490,7 @@ class table: public property_owner<table<TT>>
{
const char *str = c_str();
if (str == NULL)
throw std::runtime_error("Libfort runtime error");
throw std::runtime_error("Error during table to string conversion");
return str;
}

Expand Down
12 changes: 6 additions & 6 deletions src/fort_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,18 +1022,18 @@ const char *ft_strerror(int error_code)
{
switch (error_code) {
case FT_MEMORY_ERROR:
return "Libfort error (out of memory)";
return "Out of memory";
case FT_GEN_ERROR:
return "Libfort error (general error)";
return "General error";
case FT_EINVAL:
return "Libfort error (invalid argument)";
return "Invalid argument";
case FT_INTERN_ERROR:
return "Libfort error (internal logic error)";
return "Internal libfort error";
default:
if (error_code < 0)
return "Libfort unknown error";
return "Unknown error code";
else
return "Libfort success";
return "Success";
}
}

Expand Down
26 changes: 13 additions & 13 deletions tests/bb_tests/test_error_codes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ void test_error_codes(void)
{
// Nonnegative code is success
{
assert_str_equal(ft_strerror(FT_SUCCESS), "Libfort success");
assert_str_equal(ft_strerror(0), "Libfort success");
assert_str_equal(ft_strerror(1), "Libfort success");
assert_str_equal(ft_strerror(2), "Libfort success");
assert_str_equal(ft_strerror(42), "Libfort success");
assert_str_equal(ft_strerror(INT_MAX), "Libfort success");
assert_str_equal(ft_strerror(FT_SUCCESS), "Success");
assert_str_equal(ft_strerror(0), "Success");
assert_str_equal(ft_strerror(1), "Success");
assert_str_equal(ft_strerror(2), "Success");
assert_str_equal(ft_strerror(42), "Success");
assert_str_equal(ft_strerror(INT_MAX), "Success");
}

// Error codes
{
assert_str_equal(ft_strerror(FT_MEMORY_ERROR), "Libfort error (out of memory)");
assert_str_equal(ft_strerror(FT_EINVAL), "Libfort error (invalid argument)");
assert_str_equal(ft_strerror(FT_INTERN_ERROR), "Libfort error (internal logic error)");
assert_str_equal(ft_strerror(FT_GEN_ERROR), "Libfort error (general error)");
assert_str_equal(ft_strerror(FT_MEMORY_ERROR), "Out of memory");
assert_str_equal(ft_strerror(FT_EINVAL), "Invalid argument");
assert_str_equal(ft_strerror(FT_INTERN_ERROR), "Internal libfort error");
assert_str_equal(ft_strerror(FT_GEN_ERROR), "General error");

assert_str_equal(ft_strerror(-42), "Libfort unknown error");
assert_str_equal(ft_strerror(-666), "Libfort unknown error");
assert_str_equal(ft_strerror(-INT_MAX), "Libfort unknown error");
assert_str_equal(ft_strerror(-42), "Unknown error code");
assert_str_equal(ft_strerror(-666), "Unknown error code");
assert_str_equal(ft_strerror(-INT_MAX), "Unknown error code");
}
}

0 comments on commit cd11dfd

Please sign in to comment.