Skip to content

Commit bcf6339

Browse files
author
a-pavlov
committed
Move info structure to dedicated file, correct rest exception
1 parent de591bf commit bcf6339

9 files changed

Lines changed: 54 additions & 16 deletions

File tree

client_demo/netlicensing_client_demo.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ int main(int argc, char* argv[]) {
4848
}
4949
}
5050
catch (const netlicensing::RestException& e) {
51-
std::cerr << "REST error: " << e.what() << std::endl;
51+
std::cerr << e.what() << " code " << e.http_code() << std::endl;
52+
for (auto det : e.get_details()) {
53+
std::cerr << det.to_string() << std::endl;
54+
}
5255
return 2;
5356
}
5457
catch (const std::runtime_error& err) {

include/netlicensing/entity.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ class Entity {
7272
std::string to_string() const;
7373
};
7474

75-
struct Info {
76-
std::string value_;
77-
std::string id_;
78-
std::string type_;
79-
80-
void add_property(const std::string& name, const std::string& value);
81-
};
82-
8375
}
8476

8577
#endif //__ENTITY_H__

include/netlicensing/exception.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __EXCEPTION_H__
33

44
#include <stdexcept>
5+
#include "netlicensing/info.h"
56

67
namespace netlicensing {
78

@@ -35,12 +36,18 @@ class ServiceException : public std::runtime_error {
3536
class RestException : public std::runtime_error {
3637
private:
3738
int code_;
39+
std::list<Info> details_;
3840
public:
39-
RestException(const std::string& msg, int code) : std::runtime_error(msg), code_(code) {}
41+
RestException(const std::list<Info>& details, int code) :
42+
std::runtime_error("REST error"), code_(code), details_(details) {}
4043

4144
int http_code() const {
4245
return code_;
4346
}
47+
48+
const std::list<Info> get_details() const {
49+
return details_;
50+
}
4451
};
4552

4653
class MalformedArgumentsException : public std::runtime_error {

include/netlicensing/info.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __INFO_H__
2+
#define __INFO_H__
3+
4+
#include <string>
5+
6+
namespace netlicensing {
7+
8+
struct Info {
9+
std::string value_;
10+
std::string id_;
11+
std::string type_;
12+
13+
void add_property(const std::string& name, const std::string& value);
14+
std::string to_string() const;
15+
};
16+
17+
}
18+
19+
#endif //__INFO_H__

include/netlicensing/service.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "netlicensing/validation_result.h"
66
#include "netlicensing/mapper.h"
77
#include "netlicensing/traversal.h"
8+
#include "netlicensing/exception.h"
89

910
namespace netlicensing {
1011

@@ -36,6 +37,11 @@ inline std::list<ValidationResult> validate(Context& ctx,
3637
std::string res = ctx.get(endpoint, params, http_code);
3738
Mapper<ValidationResult> mp;
3839
traverse(mp, res);
40+
41+
if (http_code != 200) {
42+
throw RestException(mp.info_, http_code);
43+
}
44+
3945
return mp.items;
4046
}
4147

include/netlicensing/validation_result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __VALIDATION_RESULT_H__
33

44
#include "netlicensing/entity.h"
5+
#include "netlicensing/info.h"
56

67
namespace netlicensing {
78

src/entity.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,4 @@ std::string Entity::to_string() const {
2323
return ss.str();
2424
}
2525

26-
void Info::add_property(const std::string& name, const std::string& value) {
27-
if (name == "value") value_ = value;
28-
else if (name == "id") id_ = value;
29-
else if (name == "type") type_ = value;
30-
}
31-
3226
}

src/info.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "netlicensing/info.h"
2+
3+
namespace netlicensing {
4+
5+
void Info::add_property(const std::string& name, const std::string& value) {
6+
if (name == "value") value_ = value;
7+
else if (name == "id") id_ = value;
8+
else if (name == "type") type_ = value;
9+
}
10+
11+
std::string Info::to_string() const {
12+
return type_ + ": " + value_;
13+
}
14+
15+
}

tests/test_errors_parsing.cc

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

99
#include "netlicensing/entity.h"
1010
#include "netlicensing/traversal.h"
11+
#include "netlicensing/info.h"
1112

1213
using netlicensing::Info;
1314

0 commit comments

Comments
 (0)