Skip to content

Commit 9f6b8b5

Browse files
committed
replace remaining instances of stop if stoppable
1 parent 538e6e0 commit 9f6b8b5

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/viam/sdk/module/service.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,8 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
185185
"unable to remove resource " + name.to_string() + " as it doesn't exist.");
186186
}
187187

188-
try {
189-
Stoppable::stop_if_stoppable(res);
190-
} catch (const std::exception& err) {
191-
VIAM_SDK_LOG(error) << "unable to stop resource: " << err.what();
188+
if (auto stoppable = std::dynamic_pointer_cast<Stoppable>(res)) {
189+
stoppable->stop();
192190
}
193191

194192
manager->remove(name);

src/viam/sdk/tests/mocks/mock_robot.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,31 +252,33 @@ ::grpc::Status MockRobotService::StopAll(::grpc::ServerContext*,
252252
const std::shared_ptr<Resource> resource = r.second;
253253
const ResourceName rn = to_proto(resource->get_resource_name());
254254
const std::string rn_ = rn.SerializeAsString();
255-
if (extra.find(rn_) != extra.end()) {
256-
try {
257-
Stoppable::stop_if_stoppable(resource, extra.at(rn_));
258-
} catch (const std::runtime_error& err) {
255+
256+
auto stop_without_extra = [&status_message,
257+
&status](const std::shared_ptr<Resource>& resource) {
258+
if (auto stoppable = std::dynamic_pointer_cast<Stoppable>(resource)) {
259259
try {
260+
stoppable->stop();
261+
return;
262+
} catch (const std::runtime_error& err) {
260263
status_message = err.what();
261-
Stoppable::stop_if_stoppable(resource);
262-
} catch (std::runtime_error& err) {
263-
status_message = err.what();
264-
status = grpc::UNKNOWN;
265264
} catch (...) {
266265
status_message = "unknown error";
267-
status = grpc::UNKNOWN;
268266
}
267+
status = grpc::UNKNOWN;
269268
}
270-
} else {
269+
};
270+
271+
if (extra.find(rn_) != extra.end()) {
271272
try {
272-
Stoppable::stop_if_stoppable(resource);
273-
} catch (std::runtime_error& err) {
273+
if (auto stoppable = std::dynamic_pointer_cast<Stoppable>(resource)) {
274+
stoppable->stop(extra.at(rn_));
275+
}
276+
} catch (const std::runtime_error& err) {
274277
status_message = err.what();
275-
status = grpc::UNKNOWN;
276-
} catch (...) {
277-
status_message = "unknown error";
278-
status = grpc::UNKNOWN;
278+
stop_without_extra(resource);
279279
}
280+
} else {
281+
stop_without_extra(resource);
280282
}
281283
}
282284

0 commit comments

Comments
 (0)