feat(Exception)
This commit is contained in:
2
Makefile
2
Makefile
@ -14,6 +14,7 @@ OBJS =\
|
||||
base58\
|
||||
base64\
|
||||
hash/sha256\
|
||||
Exception
|
||||
|
||||
TOOLS = ${LIB}
|
||||
|
||||
@ -23,6 +24,7 @@ TESTS =\
|
||||
test-base58\
|
||||
test-base64\
|
||||
hash/test-sha256\
|
||||
test-Exception
|
||||
|
||||
ifeq (${origin CC}, default)
|
||||
CC = g++
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include <basen/base58.hpp>
|
||||
#include <basen/base64.hpp>
|
||||
#include <basen/baseN.hpp>
|
||||
#include <basen/Exception.hpp>
|
||||
#include <basen/hash/sha256.hpp>
|
||||
#include <basen/hex.hpp>
|
||||
|
||||
|
||||
44
include/basen/Exception.hpp
Normal file
44
include/basen/Exception.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace basen
|
||||
{
|
||||
class Exception : public std::exception
|
||||
{
|
||||
public:
|
||||
enum class Code
|
||||
{
|
||||
BASE,
|
||||
PADDING,
|
||||
CHECKSUM,
|
||||
OVERFLOW,
|
||||
LENGTH,
|
||||
OUT_OF_ALPH,
|
||||
ALPH_COLLISION
|
||||
};
|
||||
static const std::unordered_map<Code, std::string> messages;
|
||||
|
||||
Exception(Code code, const std::source_location &location = std::source_location::current());
|
||||
const char *what() const noexcept override
|
||||
{
|
||||
return _what.c_str();
|
||||
}
|
||||
inline const char *message() const noexcept
|
||||
{
|
||||
return messages.at(_code).c_str();
|
||||
}
|
||||
inline Code code() const noexcept
|
||||
{
|
||||
return _code;
|
||||
}
|
||||
|
||||
private:
|
||||
Code _code;
|
||||
std::string _what;
|
||||
};
|
||||
}
|
||||
29
src/Exception.cpp
Normal file
29
src/Exception.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include <sstream>
|
||||
|
||||
#include <basen/Exception.hpp>
|
||||
|
||||
namespace basen
|
||||
{
|
||||
const std::unordered_map<Exception::Code, std::string> Exception::messages = {
|
||||
{Code::BASE, "incorrect base"},
|
||||
{Code::PADDING, "incorrect padding"},
|
||||
{Code::CHECKSUM, "incorrect checksum"},
|
||||
{Code::OVERFLOW, "overflow"},
|
||||
{Code::LENGTH, "not enough allocated length"},
|
||||
{Code::OUT_OF_ALPH, "out of alphabet"},
|
||||
{Code::ALPH_COLLISION, "alphabet contains same chars"},
|
||||
};
|
||||
Exception::Exception(Code code, const std::source_location &location)
|
||||
{
|
||||
_code = code;
|
||||
std::ostringstream oss;
|
||||
oss << "\033[34m"
|
||||
<< location.function_name()
|
||||
<< ':'
|
||||
<< location.line()
|
||||
<< ": \033[31mbasen::Exception:\n\t\033[0m"
|
||||
<< message()
|
||||
<< "\n";
|
||||
_what = oss.str();
|
||||
}
|
||||
}
|
||||
35
test/test-Exception.cpp
Normal file
35
test/test-Exception.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include <basen/Exception.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Exception, Exception)
|
||||
{
|
||||
EXPECT_ANY_THROW(throw basen::Exception(basen::Exception::Code::BASE));
|
||||
}
|
||||
TEST(Exception, message)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw basen::Exception(basen::Exception::Code::BASE);
|
||||
}
|
||||
catch (const basen::Exception &e)
|
||||
{
|
||||
EXPECT_STREQ(e.message(), "incorrect base");
|
||||
}
|
||||
}
|
||||
TEST(Exception, code)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw basen::Exception(basen::Exception::Code::BASE);
|
||||
}
|
||||
catch (const basen::Exception &e)
|
||||
{
|
||||
EXPECT_EQ(uint32_t(e.code()), uint32_t(basen::Exception::Code::BASE));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user