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 */
4141template <typename T, typename E>
4242class [[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
0 commit comments