Skip to content

Commit fdd0784

Browse files
committed
Fix error messages for user_exception. Change locator adapter not found exception.
1 parent c6d98cb commit fdd0784

File tree

11 files changed

+132
-16
lines changed

11 files changed

+132
-16
lines changed

idl/wire/core/locator.wire

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@
88
#ifndef WIRE_CORE_LOCATOR_WIRE_
99
#define WIRE_CORE_LOCATOR_WIRE_
1010

11-
#include <wire/core/adapter_admin.wire>
1211
#include <wire/core/identity.wire>
1312
#include <wire/core/object.wire>
1413

1514
namespace wire {
1615
namespace core {
1716

1817
interface locator_registry;
19-
exception object_not_found{
18+
19+
exception locator_error {};
20+
21+
exception object_not_found : locator_error {
22+
identity id;
23+
};
24+
25+
exception adapter_not_found : locator_error {
2026
identity id;
2127
};
2228

@@ -40,7 +46,7 @@ interface locator {
4046
*/
4147
object*
4248
find_adapter(identity id) const
43-
throw(no_adapter);
49+
throw(adapter_not_found);
4450

4551
/**
4652
* Get the locator registry object
@@ -114,7 +120,7 @@ interface locator_registry {
114120
*/
115121
void
116122
remove_adapter(object* adapter)
117-
throw(no_adapter);
123+
throw(adapter_not_found);
118124
};
119125

120126
} /* namespace core */

include/wire/core/connection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class connection {
116116

117117
using encoding::incoming;
118118
if (any(opts.flags & invocation_flags::one_way)) {
119-
throw errors::runtime_error{ "Cannot send a non-void invocation one way" };
119+
throw errors::invalid_one_way_invocation{ "Cannot send a non-void invocation one way" };
120120
}
121121
encoding::outgoing out{ get_connector() };
122122
encoding::write(::std::back_inserter(out), args ...);

include/wire/errors/exceptions.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class connection_closed : public runtime_error {
7272
class connection_refused : public connection_closed {
7373
public:
7474
connection_refused(std::string const& msg) : connection_closed{msg} {}
75+
connection_refused(char const* msg) : connection_closed{msg} {}
7576
template < typename ... T >
7677
connection_refused(T const& ... args) : connection_closed(args ...) {}
7778
};
@@ -82,6 +83,7 @@ class connection_refused : public connection_closed {
8283
class connection_failed : public connection_closed {
8384
public:
8485
connection_failed(std::string const& message) : connection_closed(message) {}
86+
connection_failed(char const* msg) : connection_closed{msg} {}
8587
template < typename ... T >
8688
connection_failed(T const& ... args) : connection_closed(args ...) {}
8789
};
@@ -90,6 +92,7 @@ class connection_failed : public connection_closed {
9092
class request_timed_out : public runtime_error {
9193
public:
9294
request_timed_out(std::string const& msg) : runtime_error{msg} {}
95+
request_timed_out(char const* msg) : runtime_error{msg} {}
9396
template < typename ... T >
9497
request_timed_out(T const& ... args) : runtime_error(args ...) {}
9598
};

include/wire/errors/user_exception.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ struct exception_reader {
118118
{
119119
using errors::user_exception_factory;
120120
auto tmp = user_exception_factory::instance().read< exception_value >(begin, end);
121+
if (tmp)
122+
tmp->post_unmarshal();
121123
::std::swap(tmp, v);
122124
}
123125
};

src/wire/core/connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ create_listen_connection_impl(adapter_ptr adptr, functional::void_callback on_cl
9191
adapter_ptr a = adptr_weak.lock();
9292
// TODO Throw an error if adapter gone away
9393
if (!a) {
94-
throw errors::runtime_error{ "Adapter gone away" };
94+
throw errors::adapter_destroyed{ "Adapter gone away" };
9595
}
9696
return ::std::make_shared< Session >( server_side{}, a, on_close );
9797
}, on_close);

src/wire/core/connector.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ struct connector::impl {
617617
{
618618
connector_ptr conn = owner_.lock();
619619
if (!conn) {
620-
throw errors::runtime_error("Connector already destroyed");
620+
throw errors::connector_destroyed{"Connector already destroyed"};
621621
}
622622
if (find_adapter(id))
623623
throw errors::runtime_error("Adapter ", id, " is already configured");
@@ -631,7 +631,7 @@ struct connector::impl {
631631
{
632632
connector_ptr conn = owner_.lock();
633633
if (!conn) {
634-
throw errors::runtime_error("Connector already destroyed");
634+
throw errors::connector_destroyed{"Connector already destroyed"};
635635
}
636636
detail::adapter_options opts { {}, 0, false, -1, 0, false, options_.client_ssl};
637637
bidir_adapter_ = adapter::create_adapter(

src/wire/core/connector_admin_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ connector_admin_impl::get_adapter_ids(current const&) const
2323
auto cnctr = get_connector();
2424
if (cnctr)
2525
return cnctr->adapter_ids();
26-
throw errors::runtime_error{ "Connector is shut down" };
26+
throw errors::connector_destroyed{ "Connector is shut down" };
2727
}
2828
void
2929
connector_admin_impl::get_adapter_state(identity const& adapter_id,

src/wire/core/reference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ reference::get_connector() const
109109
{
110110
auto cnctr = connector_.lock();
111111
if (!cnctr)
112-
throw errors::runtime_error{ "Connector already destroyed" };
112+
throw errors::connector_destroyed{ "Connector already destroyed" };
113113
return cnctr;
114114
}
115115

src/wire/locator/locator_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ struct locator::impl {
196196
if (adapters_.find(f, id)) {
197197
return f->second;
198198
}
199-
throw core::no_adapter{id};
199+
throw core::adapter_not_found{id};
200200
}
201201

202202
void
@@ -268,7 +268,7 @@ struct locator::impl {
268268
auto id = adapter->wire_identity();
269269
proxy_map::accessor f;
270270
if (!adapters_.find(f, id)) {
271-
throw core::no_adapter{id};
271+
throw core::adapter_not_found{id};
272272
} else {
273273
#if DEBUG_OUTPUT >= 1
274274
::std::cerr <<::getpid() << " Locator remove adapter " << *adapter << "\n";

test/encoding/exception_io_test.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ::std::string const BASE = "::test::base_type";
2323
} /* namespace */
2424

2525
class base : public errors::user_exception {
26+
public:
27+
using ptr = ::std::shared_ptr<base>;
2628
public:
2729
base() : user_exception{} {}
2830
explicit
@@ -68,9 +70,22 @@ class base : public errors::user_exception {
6870
::std::exception_ptr
6971
make_exception_ptr() override
7072
{ return ::std::make_exception_ptr(*this); }
73+
protected:
74+
void
75+
stream_message(::std::ostream& os) const override
76+
{
77+
os << "base exception";
78+
}
79+
private:
80+
using factory_init_type = errors::user_exception_factory_init< base >;
81+
static factory_init_type _factory_init_;
7182
};
7283

84+
base::factory_init_type base::_factory_init_;
85+
7386
class derived : public base {
87+
public:
88+
using ptr = ::std::shared_ptr<derived>;
7489
public:
7590
derived() : base{}, some_int{} {};
7691

@@ -119,8 +134,50 @@ class derived : public base {
119134
{ return ::std::make_exception_ptr(*this); }
120135
public:
121136
::std::int32_t some_int;
137+
138+
protected:
139+
void
140+
stream_message(::std::ostream& os) const override
141+
{
142+
os << "derived exception " << some_int;
143+
}
144+
145+
private:
146+
using factory_init_type = errors::user_exception_factory_init<derived>;
147+
static factory_init_type _factory_init_;
122148
};
123149

150+
derived::factory_init_type derived::_factory_init_;
151+
152+
}
153+
154+
namespace detail {
155+
156+
template <>
157+
struct is_user_exception<test::base> : ::std::true_type {};
158+
template <>
159+
struct is_user_exception<test::derived> : ::std::true_type {};
160+
161+
} /* namespace detail */
162+
163+
164+
namespace test {
165+
166+
static_assert( detail::is_user_exception<base>::value,
167+
"Traits for base object are correct" );
168+
static_assert( detail::is_user_exception<derived>::value,
169+
"Traits for derived object are correct" );
170+
171+
static_assert( detail::wire_type< base::ptr >::value == detail::wire_types::EXCEPTION,
172+
"Correct wire type for base object");
173+
static_assert( detail::wire_type< derived::ptr >::value == detail::wire_types::EXCEPTION,
174+
"Correct wire type for derived object");
175+
176+
static_assert( ::std::is_base_of< detail::exception_reader<base>, detail::reader<base::ptr> >::value,
177+
"Correct reader for base object");
178+
static_assert( ::std::is_base_of< detail::exception_reader<derived>, detail::reader<derived::ptr> >::value,
179+
"Correct reader for base object");
180+
124181
TEST(IO, UserException)
125182
{
126183
outgoing out{ core::connector_ptr{} };
@@ -149,12 +206,60 @@ TEST(IO, UserException)
149206
::std::cerr << "Encaps size " << encaps.size() << "\n";
150207
::std::cerr << "Data left " << (encaps.end() - f) << "\n";
151208
EXPECT_NO_THROW(e.__wire_read(f, l, true));
209+
EXPECT_NO_THROW(e.post_unmarshal());
152210
::std::cerr << "Data left " << (encaps.end() - f) << "\n";
211+
EXPECT_EQ(::std::string{"base exception"}, e.what());
153212
derived d;
154213
l = encaps.end();
155214
EXPECT_NO_THROW(d.__wire_read(f, l, true));
215+
EXPECT_NO_THROW(d.post_unmarshal());
156216
::std::cerr << "Data left " << (encaps.end() - f) << "\n";
157217
EXPECT_EQ(100500, d.some_int);
218+
EXPECT_EQ(::std::string{"derived exception 100500"}, d.what());
219+
}
220+
}
221+
222+
TEST(IO, UserExceptionFactory)
223+
{
224+
outgoing out{ core::connector_ptr{} };
225+
{
226+
auto encaps = out.current_encapsulation();
227+
auto o = ::std::back_inserter(out);
228+
base e("ooops");
229+
e.__wire_write(o);
230+
::std::cerr << "Encaps size " << encaps.size() << "\n";
231+
derived d{100500};
232+
d.__wire_write(o);
233+
::std::cerr << "Encaps size " << encaps.size() << "\n";
234+
EXPECT_FALSE(encaps.empty());
235+
}
236+
incoming in{ message{}, ::std::move(out) };
237+
{
238+
auto encaps = in.current_encapsulation();
239+
EXPECT_FALSE(encaps.empty());
240+
241+
auto f = encaps.begin();
242+
auto l = encaps.end();
243+
::std::cerr << "Encaps size " << encaps.size() << "\n";
244+
::std::cerr << "Data left " << (encaps.end() - f) << "\n";
245+
246+
errors::user_exception_ptr e, d;
247+
read(f, l, e);
248+
read(f, l, d);
249+
encaps.read_indirection_table(f);
250+
251+
ASSERT_TRUE(e.get());
252+
ASSERT_TRUE(d.get());
253+
254+
auto bs = ::std::dynamic_pointer_cast< base >(e);
255+
EXPECT_TRUE(bs.get()) << "Correct type info";
256+
auto dv = ::std::dynamic_pointer_cast< derived >(e);
257+
EXPECT_FALSE(dv.get()) << "Correct type info";
258+
dv = ::std::dynamic_pointer_cast< derived >(d);
259+
EXPECT_TRUE(dv.get()) << "Correct type info";
260+
261+
EXPECT_EQ(::std::string{"base exception"}, e->what());
262+
EXPECT_EQ(::std::string{"derived exception 100500"}, d->what());
158263
}
159264
}
160265

0 commit comments

Comments
 (0)