Skip to content

Commit a18cdf7

Browse files
Handle service network errors
1 parent f8000db commit a18cdf7

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

include/iota/api/service.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <iota/constants.hpp>
3131
#include <iota/errors/bad_request.hpp>
3232
#include <iota/errors/internal_server_error.hpp>
33-
#include <iota/errors/timeout.hpp>
33+
#include <iota/errors/network.hpp>
3434
#include <iota/errors/unauthorized.hpp>
3535
#include <iota/errors/unrecognized.hpp>
3636

@@ -66,8 +66,12 @@ class Service {
6666
{ "X-IOTA-API-Version", APIVersion } };
6767
auto res = cpr::Post(url, body, headers, cpr::Timeout{ timeout * 1000 });
6868

69+
if (res.error.code != cpr::ErrorCode::OK)
70+
throw Errors::Network(res.error.message);
71+
6972
json resJson;
7073
std::string error;
74+
7175
try {
7276
resJson = json::parse(res.text);
7377

@@ -76,10 +80,10 @@ class Service {
7680
}
7781
} catch (const std::runtime_error&) {
7882
if (res.elapsed >= timeout) {
79-
throw Errors::Timeout("IRI timed out after " + std::to_string(timeout) + "s");
83+
throw Errors::Network("Time out after " + std::to_string(timeout) + "s");
8084
}
8185

82-
throw Errors::Unrecognized("invalid reply from node (unrecognized format): " + res.text);
86+
throw Errors::Unrecognized("Invalid reply from node (unrecognized format): " + res.text);
8387
}
8488

8589
Response response;
@@ -96,7 +100,7 @@ class Service {
96100
throw Errors::InternalServerError(error);
97101
default:
98102
if (res.elapsed >= timeout) {
99-
throw Errors::Timeout("IRI timed out after " + std::to_string(timeout) + "s");
103+
throw Errors::Network("Time out after " + std::to_string(timeout) + "s");
100104
}
101105

102106
throw Errors::Unrecognized(error);

include/iota/errors/timeout.hpp renamed to include/iota/errors/network.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ namespace IOTA {
3131

3232
namespace Errors {
3333

34-
class Timeout : public Generic {
34+
class Network : public Generic {
3535
public:
36-
Timeout(const std::string& content) : Generic(content){};
36+
Network(const std::string& content) : Generic(content){};
3737
};
3838

3939
} // namespace Errors

test/source/errors/timeout_test.cpp renamed to test/source/errors/network_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
#include <gtest/gtest.h>
2727

28-
#include <iota/errors/timeout.hpp>
28+
#include <iota/errors/network.hpp>
2929
#include <test/utils/expect_exception.hpp>
3030

31-
TEST(Errors, Timeout) {
32-
EXPECT_EXCEPTION(throw(IOTA::Errors::Timeout("Timeout"));, IOTA::Errors::Timeout, "Timeout")
31+
TEST(Errors, Network) {
32+
EXPECT_EXCEPTION(throw(IOTA::Errors::Network("Network"));, IOTA::Errors::Network, "Network")
3333
}

0 commit comments

Comments
 (0)