|
| 1 | +// Copyright (c) 2024 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 | +// Tests for Worker::Lock::getExportedHandler() error behaviour, specifically |
| 6 | +// the isDynamicDispatch path which surfaces user-configuration mistakes as JSG |
| 7 | +// TypeErrors rather than internal log-only errors. |
| 8 | + |
| 9 | +#include <workerd/api/global-scope.h> |
| 10 | +#include <workerd/io/frankenvalue.h> |
| 11 | +#include <workerd/io/worker.h> |
| 12 | +#include <workerd/tests/test-fixture.h> |
| 13 | + |
| 14 | +#include <kj/test.h> |
| 15 | + |
| 16 | +namespace workerd { |
| 17 | +namespace { |
| 18 | + |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | +// isDynamicDispatch = true: both error cases must throw a JSG TypeError. |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | + |
| 23 | +KJ_TEST("getExportedHandler: DO class via dynamic dispatch throws JSG TypeError") { |
| 24 | + TestFixture fixture({ |
| 25 | + .mainModuleSource = R"( |
| 26 | + import { DurableObject } from "cloudflare:workers"; |
| 27 | + export class SomeActor extends DurableObject {} |
| 28 | + export default { async fetch(req) { return new Response("ok"); } } |
| 29 | + )"_kj, |
| 30 | + }); |
| 31 | + |
| 32 | + fixture.runInIoContext([&](const TestFixture::Environment& env) { |
| 33 | + KJ_EXPECT_THROW_MESSAGE("refers to a Durable Object class", |
| 34 | + env.lock.getExportedHandler("SomeActor"_kj, kj::none, Frankenvalue{}, kj::none, true)); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +KJ_TEST("getExportedHandler: missing entrypoint via dynamic dispatch throws JSG TypeError") { |
| 39 | + TestFixture fixture({ |
| 40 | + .mainModuleSource = R"( |
| 41 | + export default { async fetch(req) { return new Response("ok"); } } |
| 42 | + )"_kj, |
| 43 | + }); |
| 44 | + |
| 45 | + fixture.runInIoContext([&](const TestFixture::Environment& env) { |
| 46 | + KJ_EXPECT_THROW_MESSAGE("was not found in this worker", |
| 47 | + env.lock.getExportedHandler("nonExistent"_kj, kj::none, Frankenvalue{}, kj::none, true)); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +// --------------------------------------------------------------------------- |
| 52 | +// isDynamicDispatch = false: both error cases must NOT throw a JSG TypeError |
| 53 | +// (they log and then throw a non-JSG internal error via KJ_FAIL_ASSERT). |
| 54 | +// --------------------------------------------------------------------------- |
| 55 | + |
| 56 | +KJ_TEST("getExportedHandler: DO class via static dispatch throws internal error") { |
| 57 | + TestFixture fixture({ |
| 58 | + .mainModuleSource = R"( |
| 59 | + import { DurableObject } from "cloudflare:workers"; |
| 60 | + export class SomeActor extends DurableObject {} |
| 61 | + export default { async fetch(req) { return new Response("ok"); } } |
| 62 | + )"_kj, |
| 63 | + }); |
| 64 | + |
| 65 | + fixture.runInIoContext([&](const TestFixture::Environment& env) { |
| 66 | + // LOG_ERROR_PERIODICALLY fires, then KJ_FAIL_ASSERT throws. |
| 67 | + // No JSG TypeError — the error is treated as internal. |
| 68 | + KJ_EXPECT_LOG(ERROR, "worker has no such named entrypoint"); |
| 69 | + KJ_EXPECT_THROW_MESSAGE("Unable to get exported handler", |
| 70 | + env.lock.getExportedHandler("nonExistent"_kj, kj::none, Frankenvalue{}, kj::none, false)); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | +} // namespace workerd |
0 commit comments