Skip to content

Commit d68fb57

Browse files
committed
Added a small project for generic network errors.
1 parent 184d278 commit d68fb57

File tree

8 files changed

+140
-3
lines changed

8 files changed

+140
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ endif(DOXYGEN_FOUND)
157157
if(CPP-NETLIB_BUILD_SINGLE_LIB)
158158
include_directories(
159159
${CMAKE_CURRENT_SOURCE_DIR}/config/src
160+
${CMAKE_CURRENT_SOURCE_DIR}/error/src
160161
${CMAKE_CURRENT_SOURCE_DIR}/concurrency/src
161162
${CMAKE_CURRENT_SOURCE_DIR}/http/src
162163
${CMAKE_CURRENT_SOURCE_DIR}/logging/src
@@ -169,6 +170,7 @@ endif()
169170
add_subdirectory(uri)
170171
#add_subdirectory(message)
171172
add_subdirectory(logging)
173+
add_subdirectory(error)
172174
#add_subdirectory(concurrency)
173175
add_subdirectory(http)
174176
#add_subdirectory(mime)
@@ -182,7 +184,7 @@ if(CPP-NETLIB_BUILD_SINGLE_LIB)
182184
${CPP-NETLIB_MESSAGE_SRCS}
183185
${CPP-NETLIB_MESSAGE_DIRECTIVES_SRCS}
184186
${CPP-NETLIB_MESSAGE_WRAPPERS_SRCS}
185-
${CPP-NETLIB_LoOGGING_SRCS}
187+
${CPP-NETLIB_LOGGING_SRCS}
186188
${CPP-NETLIB_HTTP_CLIENT_SRCS}
187189
${CPP-NETLIB_HTTP_CLIENT_CONNECTIONS_SRCS}
188190
${CPP-NETLIB_HTTP_SERVER_SRCS}

concurrency/test/thread_pool_test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <gtest/gtest.h>
1010
#include <network/concurrency/thread_pool.hpp>
11-
// #include <boost/bind.hpp>
1211
#include <functional>
1312

1413
using network::concurrency::thread_pool;
@@ -42,5 +41,5 @@ TEST(concurrency_test, post_work) {
4241
ASSERT_NO_THROW(pool.post(std::bind(&foo::bar, &instance, 2)));
4342
// require that pool is destroyed here, RAII baby
4443
}
45-
ASSERT_EQ(instance.val(), 3);
44+
ASSERT_EQ(3, instance.val());
4645
}

error/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) Glyn Matthews 2013.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
add_subdirectory(src)
7+
8+
if(CPP-NETLIB_BUILD_TESTS)
9+
enable_testing()
10+
add_subdirectory(test)
11+
endif(CPP-NETLIB_BUILD_TESTS)
12+
13+
# propagate sources to parent directory for one-lib-build
14+
set(CPP-NETLIB_ERROR_SRCS ${CPP-NETLIB_ERROR_SRCS} PARENT_SCOPE)

error/src/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Glyn Matthews 2013.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
include_directories(${CPP-NETLIB_SOURCE_DIR}/error/src)
7+
8+
set(CPP-NETLIB_ERROR_SRCS
9+
error.cpp)
10+
11+
if(NOT CPP-NETLIB_BUILD_SINGLE_LIB)
12+
add_library(network_error ${CPP-NETLIB_ERROR_SRCS})
13+
endif()
14+
15+
# prepend current directory to make paths absolute
16+
prependToElements( "${CMAKE_CURRENT_SOURCE_DIR}/"
17+
CPP-NETLIB_ERROR_SRCS )
18+
19+
# propagate sources to parent directory for one-lib-build
20+
set(CPP-NETLIB_ERROR_SRCS ${CPP-NETLIB_ERROR_SRCS} PARENT_SCOPE)

error/src/error.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <network/error.hpp>
7+
#include <cstring>
8+
9+
namespace network {
10+
11+
class network_category_impl : public std::error_category {
12+
13+
public:
14+
15+
network_category_impl() = default;
16+
17+
virtual ~network_category_impl() noexcept;
18+
19+
virtual const char *name() const noexcept;
20+
21+
virtual std::string message(int ev) const;
22+
23+
};
24+
25+
network_category_impl::~network_category_impl() noexcept {
26+
27+
}
28+
29+
const char *network_category_impl::name() const noexcept {
30+
static const char name[] = "network_error";
31+
return name;
32+
}
33+
34+
std::string network_category_impl::message(int ev) const {
35+
return std::strerror(ev);
36+
}
37+
38+
const std::error_category &network_category() {
39+
static network_category_impl category;
40+
return category;
41+
}
42+
} // namespace network

error/src/network/error.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (C) 2013 by Glyn Matthews
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef NETWORK_ERROR_INC
7+
#define NETWORK_ERROR_INC
8+
9+
/**
10+
* \file
11+
* \brief Contains a set of error classes and exceptions for
12+
* network connections.
13+
*/
14+
15+
#include <system_error>
16+
17+
namespace network {
18+
/**
19+
* \brief Gets the error category for network errors.
20+
*/
21+
const std::error_category &network_category();
22+
} // namespace network
23+
24+
#endif // NETWORK_ERROR_INC

error/test/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) Glyn Matthews 2013.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
include_directories(${CPP-NETLIB_SOURCE_DIR}/error/src
7+
${GTEST_INCLUDE_DIRS}
8+
${CPP-NETLIB_SOURCE_DIR}
9+
)
10+
11+
if (CPP-NETLIB_BUILD_TESTS)
12+
add_executable(cpp-netlib-error_test error_test.cpp)
13+
target_link_libraries(cpp-netlib-error_test
14+
${link_cppnetlib_lib}
15+
network_error
16+
${Boost_LIBRARIES}
17+
${GTEST_BOTH_LIBRARIES}
18+
${CMAKE_THREAD_LIBS_INIT})
19+
set_target_properties(cpp-netlib-error_test PROPERTIES
20+
RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
21+
add_test(cpp-netlib-error_test
22+
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-error_test)
23+
endif()

error/test/error_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Glyn Matthews 2013.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#include <gtest/gtest.h>
7+
#include <network/error.hpp>
8+
#include <cerrno>
9+
10+
TEST(error_test, system_error) {
11+
std::system_error error(EPIPE, network::network_category());
12+
ASSERT_EQ(EPIPE, error.code().value());
13+
}

0 commit comments

Comments
 (0)