File
Blob: src/workerd/io/promise-wrapper-test.c++
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #include <workerd/io/promise-wrapper.h> |
| 6 | #include <workerd/jsg/jsg-test.h> |
| 7 | #include <workerd/jsg/jsg.h> |
| 8 | #include <workerd/jsg/setup.h> |
| 9 | |
| 10 | #include <kj/test.h> |
| 11 | |
| 12 | namespace workerd::jsg::test { // workerd |
| 13 | namespace { |
| 14 | |
| 15 | jsg::V8System v8System; |
| 16 | |
| 17 | struct CaptureThrowContext: public jsg::Object, public ContextGlobal { |
| 18 | kj::Promise<int> test1() { |
| 19 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 20 | } |
| 21 | |
| 22 | kj::Promise<void> test2() { |
| 23 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 24 | } |
| 25 | |
| 26 | int test3() { |
| 27 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 28 | } |
| 29 | |
| 30 | kj::Promise<void> test4(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 31 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 32 | } |
| 33 | |
| 34 | int test5(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 35 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 36 | } |
| 37 | |
| 38 | v8::Local<v8::Promise> test6() { |
| 39 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 40 | } |
| 41 | |
| 42 | v8::Local<v8::Promise> test7(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 43 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 44 | } |
| 45 | |
| 46 | template <typename T> |
| 47 | v8::Local<v8::Value> testT(jsg::Lock& js, const jsg::TypeHandler<Function<T()>>& handler) { |
| 48 | return handler.wrap(js, [](Lock&) -> T { JSG_FAIL_REQUIRE(TypeError, "boom"); }); |
| 49 | } |
| 50 | |
| 51 | static kj::Promise<void> staticTest1() { |
| 52 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 53 | } |
| 54 | |
| 55 | static kj::Promise<void> staticTest2(const v8::FunctionCallbackInfo<v8::Value>&) { |
| 56 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 57 | } |
| 58 | |
| 59 | static kj::Promise<void> staticTest3(jsg::Lock& js) { |
| 60 | // Tests that JsExceptionThrown is handled properly. |
| 61 | jsg::throwTypeError(js.v8Isolate, "boom"_kj); |
| 62 | } |
| 63 | |
| 64 | kj::Promise<void> getTest() { |
| 65 | JSG_FAIL_REQUIRE(TypeError, "boom"); |
| 66 | } |
| 67 | |
| 68 | JSG_RESOURCE_TYPE(CaptureThrowContext) { |
| 69 | JSG_METHOD(test1); |
| 70 | JSG_METHOD(test2); |
| 71 | JSG_METHOD(test3); |
| 72 | JSG_METHOD(test4); |
| 73 | JSG_METHOD(test5); |
| 74 | JSG_METHOD(test6); |
| 75 | JSG_METHOD(test7); |
| 76 | JSG_READONLY_PROTOTYPE_PROPERTY(test8, template testT<kj::Promise<void>>); |
| 77 | JSG_STATIC_METHOD(staticTest1); |
| 78 | JSG_STATIC_METHOD(staticTest2); |
| 79 | JSG_STATIC_METHOD(staticTest3); |
| 80 | JSG_READONLY_PROTOTYPE_PROPERTY(test, getTest); |
| 81 | } |
| 82 | }; |
| 83 | JSG_DECLARE_ISOLATE_TYPE( |
| 84 | CaptureThrowIsolate, CaptureThrowContext, jsg::TypeWrapperExtension<workerd::PromiseWrapper>); |
| 85 | |
| 86 | KJ_TEST("Async functions capture sync errors with flag") { |
| 87 | Evaluator<CaptureThrowContext, CaptureThrowIsolate> e(v8System); |
| 88 | e.setCaptureThrowsAsRejections(true); |
| 89 | e.expectEval("test1()", "object", "[object Promise]"); |
| 90 | e.expectEval("test2()", "object", "[object Promise]"); |
| 91 | e.expectEval("test3()", "throws", "TypeError: boom"); |
| 92 | e.expectEval("test4()", "object", "[object Promise]"); |
| 93 | e.expectEval("test5()", "throws", "TypeError: boom"); |
| 94 | e.expectEval("test6()", "object", "[object Promise]"); |
| 95 | e.expectEval("test7()", "object", "[object Promise]"); |
| 96 | e.expectEval("test8()", "object", "[object Promise]"); |
| 97 | e.expectEval("CaptureThrowContext.staticTest1()", "object", "[object Promise]"); |
| 98 | e.expectEval("CaptureThrowContext.staticTest2()", "object", "[object Promise]"); |
| 99 | e.expectEval("CaptureThrowContext.staticTest3()", "object", "[object Promise]"); |
| 100 | e.expectEval("test", "object", "[object Promise]"); |
| 101 | } |
| 102 | |
| 103 | KJ_TEST("Async functions do not capture sync errors without flag") { |
| 104 | Evaluator<CaptureThrowContext, CaptureThrowIsolate> e(v8System); |
| 105 | e.setCaptureThrowsAsRejections(false); |
| 106 | e.expectEval("test1()", "throws", "TypeError: boom"); |
| 107 | e.expectEval("test2()", "throws", "TypeError: boom"); |
| 108 | e.expectEval("test3()", "throws", "TypeError: boom"); |
| 109 | e.expectEval("test4()", "throws", "TypeError: boom"); |
| 110 | e.expectEval("test5()", "throws", "TypeError: boom"); |
| 111 | e.expectEval("test6()", "throws", "TypeError: boom"); |
| 112 | e.expectEval("test7()", "throws", "TypeError: boom"); |
| 113 | e.expectEval("test8()", "throws", "TypeError: boom"); |
| 114 | e.expectEval("CaptureThrowContext.staticTest1()", "throws", "TypeError: boom"); |
| 115 | e.expectEval("CaptureThrowContext.staticTest2()", "throws", "TypeError: boom"); |
| 116 | e.expectEval("CaptureThrowContext.staticTest3()", "throws", "TypeError: boom"); |
| 117 | e.expectEval("test", "throws", "TypeError: boom"); |
| 118 | } |
| 119 | |
| 120 | } // namespace |
| 121 | } // namespace workerd::jsg::test |