ot::error_message

This commit is contained in:
Frédéric Mangano-Tarumi 2018-11-13 18:04:26 -05:00
parent b0e8813be6
commit 5ff99b620c
5 changed files with 72 additions and 7 deletions

View File

@ -20,6 +20,7 @@ add_library(
libopustags
OBJECT
src/cli.cc
src/error.cc
src/ogg.cc
src/opus.cc
)

26
src/error.cc Normal file
View File

@ -0,0 +1,26 @@
#include <opustags.h>
static const char* messages[] = {
"OK",
"Need to exit",
"Bad command-line arguments",
"Integer overflow",
"Standard error",
"End of file",
"libogg error",
"Bad magic number",
"Overflowing magic number",
"Overflowing vendor length",
"Overflowing vendor data",
"Overflowing comment count",
"Overflowing comment length",
"Overflowing comment data",
};
const char* ot::error_message(ot::status code)
{
if (code >= ot::status::sentinel)
return nullptr;
auto index = static_cast<size_t>(code);
return messages[index];
}

View File

@ -15,15 +15,16 @@
namespace ot {
/**
* Possible error status.
* Possible error status, ranging from #ok (0) to #sentinel.
*
* The overflowing error family means that the end of packet was reached when
* attempting to read the overflowing value. For example,
* overflowing_comment_count means that after reading the vendor string, less
* than 4 bytes were left in the packet.
* Use #error_message to get an error message from a status code.
*
* The overflowing error family means that the end of packet was reached when attempting to read the
* overflowing value. For example, overflowing_comment_count means that after reading the vendor
* string, less than 4 bytes were left in the packet.
*/
enum class status {
ok,
ok = 0,
exit_now,
bad_arguments,
int_overflow,
@ -39,8 +40,15 @@ enum class status {
overflowing_comment_count,
overflowing_comment_length,
overflowing_comment_data,
/**
* Last error code, for integrity checking.
* Do not use it, it does not represent any error.
*/
sentinel,
};
const char* error_message(status code);
/**
* \defgroup ogg Ogg
* \brief Helpers to work with libogg.

View File

@ -1,8 +1,11 @@
add_executable(opus.t EXCLUDE_FROM_ALL opus.cc)
target_link_libraries(opus.t libopustags)
add_executable(error.t EXCLUDE_FROM_ALL error.cc)
target_link_libraries(error.t libopustags)
add_custom_target(
check
COMMAND prove "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS opustags opus.t
DEPENDS opustags opus.t error.t
)

27
t/error.cc Normal file
View File

@ -0,0 +1,27 @@
#include <opustags.h>
#include "tap.h"
static void check_message()
{
if (strcmp(ot::error_message(ot::status::ok), "OK") != 0)
throw failure("unexpected message for code ok");
if (strcmp(ot::error_message(ot::status::overflowing_comment_data), "Overflowing comment data") != 0)
throw failure("unexpected message for overflowing_comment_data");
}
static void check_sentinel()
{
if (ot::error_message(ot::status::sentinel) != nullptr)
throw failure("the sentinel should not have a message");
auto too_far = static_cast<ot::status>(static_cast<int>(ot::status::sentinel) + 1);
if (ot::error_message(too_far) != nullptr)
throw failure("an invalid status should not have a message");
}
int main()
{
std::cout << "1..2\n";
run(check_message, "check a few error messages");
run(check_sentinel, "ensure the sentinel is respected");
return 0;
}