Skip to content

Commit 33c7d11

Browse files
authored
Merge pull request #6478 from cloudflare/maizatskyi/2026-04-01-exception-copy
make exception copy explicit or completely avoid it
2 parents 9bdd85c + 927d7f5 commit 33c7d11

5 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/workerd/api/streams/identity-transform-stream.c++

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ class IdentityTransformStreamImpl final: public kj::Refcounted,
107107
if (amount > l) {
108108
auto exception = JSG_KJ_EXCEPTION(
109109
FAILED, TypeError, "Attempt to write too many bytes through a FixedLengthStream.");
110-
cancel(exception);
110+
cancel(kj::cp(exception));
111111
return kj::mv(exception);
112112
} else if (amount == 0 && l != 0) {
113113
auto exception = JSG_KJ_EXCEPTION(FAILED, TypeError,
114114
"FixedLengthStream did not see all expected bytes before close().");
115-
cancel(exception);
115+
cancel(kj::cp(exception));
116116
return kj::mv(exception);
117117
}
118118
l -= amount;

src/workerd/api/streams/writable-sink-adapter-test.c++

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ KJ_TEST("Basic abort() operation") {
202202

203203
KJ_ASSERT(adapter->isClosed() == false, "Adapter should not be closed after abort()");
204204
KJ_ASSERT(adapter->isClosing() == false, "Adapter should not be closing after abort()");
205-
auto exception =
205+
auto& exception =
206206
KJ_ASSERT_NONNULL(adapter->isErrored(), "Adapter should be in errored state after abort()");
207207
KJ_ASSERT(exception.getDescription().contains("Abort reason"),
208208
"Adapter should be in errored state after abort()");
@@ -223,7 +223,7 @@ KJ_TEST("Basic abort() operation") {
223223
"End after abort() call should be rejected");
224224

225225
adapter->abort(env.js, env.js.str("Abort reason 2"_kj));
226-
auto exception2 = KJ_ASSERT_NONNULL(
226+
auto& exception2 = KJ_ASSERT_NONNULL(
227227
adapter->isErrored(), "Adapter should still be in errored state after second abort()");
228228
KJ_ASSERT(exception2.getDescription().contains("Abort reason 2"),
229229
"Adapter should reflect reason from second abort() call");
@@ -248,7 +248,7 @@ KJ_TEST("Abort from closing state supersedes close") {
248248

249249
KJ_ASSERT(adapter->isClosed() == false, "Adapter should not be closed after abort()");
250250
KJ_ASSERT(adapter->isClosing() == false, "Adapter should not be closing after abort()");
251-
auto exception =
251+
auto& exception =
252252
KJ_ASSERT_NONNULL(adapter->isErrored(), "Adapter should be in errored state after abort()");
253253
KJ_ASSERT(exception.getDescription().contains("Abort reason"),
254254
"Adapter should be in errored state after abort()");

src/workerd/jsg/exception.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,29 @@ namespace workerd::jsg {
8383
do { \
8484
try { \
8585
KJ_REQUIRE(cond, jsErrorType ": Cloudflare internal error."); \
86-
} catch (const kj::Exception& e) { \
86+
} catch (kj::Exception & e) { \
8787
KJ_LOG(ERROR, e, ##__VA_ARGS__); \
88-
throw e; \
88+
throw kj::mv(e); \
8989
} \
9090
} while (0)
9191

9292
#define _JSG_INTERNAL_REQUIRE_NONNULL(value, jsErrorType, ...) \
9393
([&]() -> decltype(auto) { \
9494
try { \
9595
return KJ_REQUIRE_NONNULL(value, jsErrorType ": Cloudflare internal error."); \
96-
} catch (const kj::Exception& e) { \
96+
} catch (kj::Exception & e) { \
9797
KJ_LOG(ERROR, e, ##__VA_ARGS__); \
98-
throw e; \
98+
throw kj::mv(e); \
9999
} \
100100
}())
101101

102102
#define _JSG_INTERNAL_FAIL_REQUIRE(jsErrorType, ...) \
103103
do { \
104104
try { \
105105
KJ_FAIL_REQUIRE(jsErrorType ": Cloudflare internal error."); \
106-
} catch (const kj::Exception& e) { \
106+
} catch (kj::Exception & e) { \
107107
KJ_LOG(ERROR, e, ##__VA_ARGS__); \
108-
throw e; \
108+
throw kj::mv(e); \
109109
} \
110110
} while (0)
111111

src/workerd/tests/test-fixture.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ void TestFixture::runInIoContext(kj::Function<kj::Promise<void>(const Environmen
454454
});
455455
} catch (kj::Exception& e) {
456456
if (!ignoreDescription(e.getDescription())) {
457-
throw e;
457+
throw kj::mv(e);
458458
}
459459
}
460460
}

src/workerd/util/test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace workerd {
1414
#define WD_EXPECT_THROW(expException, code, ...) \
1515
do { \
1616
/* NOLINTNEXTLINE(performance-unnecessary-copy-initialization) */ \
17-
auto expExcObj = expException; \
17+
auto expExcObj = kj::cp(expException); \
1818
KJ_IF_SOME(e, ::kj::runCatchingExceptions([&]() { (void)({ code; }); })) { \
1919
KJ_EXPECT(e.getType() == expExcObj.getType(), "code threw wrong exception type: " #code, e, \
2020
##__VA_ARGS__); \

0 commit comments

Comments
 (0)