Skip to content

Commit 82ee340

Browse files
Adjust result.h error condition behavior (#128)
1 parent 6a133e9 commit 82ee340

4 files changed

Lines changed: 157 additions & 61 deletions

File tree

‎.github/workflows/builds.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ permissions:
5353
env:
5454
CARGO_TERM_COLOR: always
5555
# Pinned commit for cpp-example-collection smoke build (https://github.com/livekit-examples/cpp-example-collection)
56-
CPP_EXAMPLE_COLLECTION_REF: f231c0c75028d1dcf13edcecd369d030d2c7c8d4
56+
CPP_EXAMPLE_COLLECTION_REF: 46083ea4e5d3def8e44a53148c2c7800131efca0
5757
# vcpkg binary caching for Windows
5858
VCPKG_DEFAULT_TRIPLET: x64-windows-static-md
5959
VCPKG_DEFAULT_HOST_TRIPLET: x64-windows-static-md

‎include/livekit/result.h‎

Lines changed: 97 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#pragma once
1818

19-
#include <cassert>
2019
#include <optional>
20+
#include <stdexcept>
2121
#include <type_traits>
2222
#include <utility>
2323
#include <variant>
@@ -34,9 +34,9 @@ namespace livekit {
3434
* - a success value of type `T`, or
3535
* - an error value of type `E`
3636
*
37-
* Accessors are intentionally non-throwing. Calling `value()` on an error
38-
* result, or `error()` on a success result, is a programmer error and will
39-
* trip the debug assertion.
37+
* Accessors validate their preconditions before returning. Calling `value()`
38+
* on an error result, or `error()` on a success result, throws `std::logic_error`.
39+
* Avoid this by checking `ok()` / `has_error()` / if (result) before calling `value()` or `error()`.
4040
*/
4141
template <typename T, typename E>
4242
class [[nodiscard]] Result {
@@ -60,64 +60,83 @@ class [[nodiscard]] Result {
6060
/// Allows `if (result)` style success checks.
6161
explicit operator bool() const noexcept { return ok(); }
6262

63-
// TODO (AEG): clang-tidy flagged these accessors because the signatures are
64-
// marked noexcept, but std::get can throw a std::bad_variant_access exception
65-
// on std::variant specifically. Investigate if this is actually a concern or
66-
// if the types are safe within this class (unit test ideal).
67-
68-
/// Access the success value. Requires `ok() == true`.
69-
// NOLINTNEXTLINE(bugprone-exception-escape)
70-
T& value() & noexcept {
71-
assert(ok());
63+
/// Access the success value.
64+
///
65+
/// @throws std::logic_error if `ok() == false`.
66+
T& value() & {
67+
if (!ok()) {
68+
throw std::logic_error("Result::value() called on an error result");
69+
}
7270
return std::get<0>(storage_);
7371
}
7472

75-
/// Access the success value. Requires `ok() == true`.
76-
// NOLINTNEXTLINE(bugprone-exception-escape)
77-
const T& value() const& noexcept {
78-
assert(ok());
73+
/// Access the success value.
74+
///
75+
/// @throws std::logic_error if `ok() == false`.
76+
const T& value() const& {
77+
if (!ok()) {
78+
throw std::logic_error("Result::value() called on an error result");
79+
}
7980
return std::get<0>(storage_);
8081
}
8182

82-
/// Move the success value out. Requires `ok() == true`.
83-
// NOLINTNEXTLINE(bugprone-exception-escape)
84-
T&& value() && noexcept {
85-
assert(ok());
83+
/// Move the success value out.
84+
///
85+
/// @throws std::logic_error if `ok() == false`.
86+
T&& value() && {
87+
if (!ok()) {
88+
throw std::logic_error("Result::value() called on an error result");
89+
}
8690
return std::get<0>(std::move(storage_));
8791
}
8892

89-
/// Move the success value out. Requires `ok() == true`.
90-
// NOLINTNEXTLINE(bugprone-exception-escape)
91-
const T&& value() const&& noexcept {
92-
assert(ok());
93+
/// Move the success value out.
94+
///
95+
/// @throws std::logic_error if `ok() == false`.
96+
const T&& value() const&& {
97+
if (!ok()) {
98+
throw std::logic_error("Result::value() called on an error result");
99+
}
93100
return std::get<0>(std::move(storage_));
94101
}
95102

96-
/// Access the error value. Requires `has_error() == true`.
97-
// NOLINTNEXTLINE(bugprone-exception-escape)
98-
E& error() & noexcept {
99-
assert(has_error());
103+
/// Access the error value.
104+
///
105+
/// @throws std::logic_error if `has_error() == false`.
106+
E& error() & {
107+
if (!has_error()) {
108+
throw std::logic_error("Result::error() called on a success result");
109+
}
100110
return std::get<1>(storage_);
101111
}
102112

103-
/// Access the error value. Requires `has_error() == true`.
104-
// NOLINTNEXTLINE(bugprone-exception-escape)
105-
const E& error() const& noexcept {
106-
assert(has_error());
113+
/// Access the error value.
114+
///
115+
/// @throws std::logic_error if `has_error() == false`.
116+
const E& error() const& {
117+
if (!has_error()) {
118+
throw std::logic_error("Result::error() called on a success result");
119+
}
107120
return std::get<1>(storage_);
108121
}
109122

110-
/// Move the error value out. Requires `has_error() == true`.
111-
// NOLINTNEXTLINE(bugprone-exception-escape)
112-
E&& error() && noexcept {
113-
assert(has_error());
123+
/// Move the error value out.
124+
///
125+
/// @throws std::logic_error if `has_error() == false`.
126+
E&& error() && {
127+
if (!has_error()) {
128+
throw std::logic_error("Result::error() called on a success result");
129+
}
114130
return std::get<1>(std::move(storage_));
115131
}
116132

117-
/// Move the error value out. Requires `has_error() == true`.
118-
// NOLINTNEXTLINE(bugprone-exception-escape)
119-
const E&& error() const&& noexcept {
120-
assert(has_error());
133+
/// Move the error value out.
134+
///
135+
/// @throws std::logic_error if `has_error() == false`.
136+
const E&& error() const&& {
137+
if (!has_error()) {
138+
throw std::logic_error("Result::error() called on a success result");
139+
}
121140
return std::get<1>(std::move(storage_));
122141
}
123142

@@ -152,30 +171,53 @@ class [[nodiscard]] Result<void, E> {
152171
/// Allows `if (result)` style success checks.
153172
explicit operator bool() const noexcept { return ok(); }
154173

155-
/// Validates success in debug builds. Mirrors the `value()` API shape.
156-
void value() const noexcept { assert(ok()); }
174+
/// Validates success. Mirrors the `value()` API shape on the primary
175+
/// template so generic code can use the same form for both.
176+
///
177+
/// @throws std::logic_error if `ok() == false`.
178+
void value() const {
179+
if (!ok()) {
180+
throw std::logic_error("Result::value() called on an error result");
181+
}
182+
}
157183

158-
/// Access the error value. Requires `has_error() == true`.
159-
E& error() & noexcept {
160-
assert(has_error());
184+
/// Access the error value.
185+
///
186+
/// @throws std::logic_error if `has_error() == false`.
187+
E& error() & {
188+
if (!error_.has_value()) {
189+
throw std::logic_error("Result::error() called on a success result");
190+
}
161191
return *error_;
162192
}
163193

164-
/// Access the error value. Requires `has_error() == true`.
165-
const E& error() const& noexcept {
166-
assert(has_error());
194+
/// Access the error value.
195+
///
196+
/// @throws std::logic_error if `has_error() == false`.
197+
const E& error() const& {
198+
if (!error_.has_value()) {
199+
throw std::logic_error("Result::error() called on a success result");
200+
}
167201
return *error_;
168202
}
169203

170-
/// Move the error value out. Requires `has_error() == true`.
171-
E&& error() && noexcept {
172-
assert(has_error());
204+
/// Move the error value out.
205+
///
206+
/// @throws std::logic_error if `has_error() == false`.
207+
E&& error() && {
208+
if (!error_.has_value()) {
209+
throw std::logic_error("Result::error() called on a success result");
210+
}
173211
return std::move(*error_);
174212
}
175213

176-
/// Move the error value out. Requires `has_error() == true`.
177-
const E&& error() const&& noexcept {
178-
assert(has_error());
214+
/// Move the error value out.
215+
///
216+
/// @throws std::logic_error if `has_error() == false`.
217+
const E&& error() const&& {
218+
if (!error_.has_value()) {
219+
throw std::logic_error("Result::error() called on a success result");
220+
}
179221
return std::move(*error_);
180222
}
181223

‎src/tests/unit/test_result.cpp‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <livekit/result.h>
2929

3030
#include <memory>
31+
#include <stdexcept>
3132
#include <string>
3233
#include <utility>
3334

@@ -58,7 +59,7 @@ TEST(ResultTest, SuccessHasErrorIsFalse) {
5859

5960
TEST(ResultTest, SuccessBoolConversionIsTrue) {
6061
auto r = Result<int, SimpleError>::success(42);
61-
EXPECT_TRUE(static_cast<bool>(r));
62+
EXPECT_TRUE(r);
6263
}
6364

6465
TEST(ResultTest, SuccessValueMatchesInput) {
@@ -105,7 +106,7 @@ TEST(ResultTest, FailureHasErrorIsTrue) {
105106

106107
TEST(ResultTest, FailureBoolConversionIsFalse) {
107108
auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
108-
EXPECT_FALSE(static_cast<bool>(r));
109+
EXPECT_FALSE(r);
109110
}
110111

111112
TEST(ResultTest, FailureErrorCodeMatchesInput) {
@@ -131,6 +132,40 @@ TEST(ResultTest, FailureStringError) {
131132
EXPECT_EQ(r.error(), "something went wrong");
132133
}
133134

135+
// ---------------------------------------------------------------------------
136+
// Result<T, E> — precondition violations throw std::logic_error
137+
// ---------------------------------------------------------------------------
138+
139+
TEST(ResultTest, ValueOnFailureThrowsLogicError) {
140+
auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
141+
EXPECT_THROW(r.value(), std::logic_error);
142+
}
143+
144+
TEST(ResultTest, ConstValueOnFailureThrowsLogicError) {
145+
const auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
146+
EXPECT_THROW(r.value(), std::logic_error);
147+
}
148+
149+
TEST(ResultTest, MoveValueOnFailureThrowsLogicError) {
150+
auto r = Result<int, SimpleError>::failure(SimpleError{1, "oops"});
151+
EXPECT_THROW(std::move(r).value(), std::logic_error);
152+
}
153+
154+
TEST(ResultTest, ErrorOnSuccessThrowsLogicError) {
155+
auto r = Result<int, SimpleError>::success(42);
156+
EXPECT_THROW(r.error(), std::logic_error);
157+
}
158+
159+
TEST(ResultTest, ConstErrorOnSuccessThrowsLogicError) {
160+
const auto r = Result<int, SimpleError>::success(42);
161+
EXPECT_THROW(r.error(), std::logic_error);
162+
}
163+
164+
TEST(ResultTest, MoveErrorOnSuccessThrowsLogicError) {
165+
auto r = Result<int, SimpleError>::success(42);
166+
EXPECT_THROW(std::move(r).error(), std::logic_error);
167+
}
168+
134169
// ---------------------------------------------------------------------------
135170
// Result<void, E> — success path
136171
// ---------------------------------------------------------------------------
@@ -147,7 +182,7 @@ TEST(ResultVoidTest, SuccessHasErrorIsFalse) {
147182

148183
TEST(ResultVoidTest, SuccessBoolConversionIsTrue) {
149184
auto r = Result<void, SimpleError>::success();
150-
EXPECT_TRUE(static_cast<bool>(r));
185+
EXPECT_TRUE(r);
151186
}
152187

153188
TEST(ResultVoidTest, SuccessValueIsCallable) {
@@ -171,7 +206,7 @@ TEST(ResultVoidTest, FailureHasErrorIsTrue) {
171206

172207
TEST(ResultVoidTest, FailureBoolConversionIsFalse) {
173208
auto r = Result<void, SimpleError>::failure(SimpleError{5, "void fail"});
174-
EXPECT_FALSE(static_cast<bool>(r));
209+
EXPECT_FALSE(r);
175210
}
176211

177212
TEST(ResultVoidTest, FailureErrorMatchesInput) {
@@ -186,6 +221,25 @@ TEST(ResultVoidTest, FailureMoveError) {
186221
EXPECT_EQ(msg, "void error");
187222
}
188223

224+
// ---------------------------------------------------------------------------
225+
// Result<void, E> — precondition violations throw std::logic_error
226+
// ---------------------------------------------------------------------------
227+
228+
TEST(ResultVoidTest, ValueOnFailureThrowsLogicError) {
229+
auto r = Result<void, SimpleError>::failure(SimpleError{1, "oops"});
230+
EXPECT_THROW(r.value(), std::logic_error);
231+
}
232+
233+
TEST(ResultVoidTest, ErrorOnSuccessThrowsLogicError) {
234+
auto r = Result<void, SimpleError>::success();
235+
EXPECT_THROW(r.error(), std::logic_error);
236+
}
237+
238+
TEST(ResultVoidTest, MoveErrorOnSuccessThrowsLogicError) {
239+
auto r = Result<void, SimpleError>::success();
240+
EXPECT_THROW(std::move(r).error(), std::logic_error);
241+
}
242+
189243
// ---------------------------------------------------------------------------
190244
// if-result idiom
191245
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)