File
Blob: src/workerd/jsg/dom-exception-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 "dom-exception.h" |
| 6 | #include "jsg-test.h" |
| 7 | |
| 8 | namespace workerd::jsg::test { |
| 9 | namespace { |
| 10 | |
| 11 | V8System v8System; |
| 12 | |
| 13 | struct DOMExceptionContext: public Object, public ContextGlobal { |
| 14 | JSG_RESOURCE_TYPE(DOMExceptionContext) { |
| 15 | JSG_NESTED_TYPE(DOMException); |
| 16 | } |
| 17 | }; |
| 18 | JSG_DECLARE_ISOLATE_TYPE(DOMExceptionIsolate, DOMExceptionContext); |
| 19 | |
| 20 | KJ_TEST("DOMException's prototype is ErrorPrototype") { |
| 21 | Evaluator<DOMExceptionContext, DOMExceptionIsolate> e(v8System); |
| 22 | e.expectEval( |
| 23 | "Object.getPrototypeOf(DOMException.prototype) === Error.prototype", "boolean", "true"); |
| 24 | } |
| 25 | |
| 26 | KJ_TEST("DOMException has a stack property") { |
| 27 | Evaluator<DOMExceptionContext, DOMExceptionIsolate> e(v8System); |
| 28 | e.expectEval("function throwError() { throw new DOMException('test error') }\n" |
| 29 | "try { throwError() } catch (e) { e.stack }", |
| 30 | "string", |
| 31 | "Error: test error\n" |
| 32 | " at throwError (<anonymous>:1:31)\n" |
| 33 | " at <anonymous>:2:7"); |
| 34 | } |
| 35 | |
| 36 | KJ_TEST("DOMException has legacy code constants") { |
| 37 | Evaluator<DOMExceptionContext, DOMExceptionIsolate> e(v8System); |
| 38 | // Test a subset of error codes that commonly appear in web APIs. |
| 39 | e.expectEval("DOMException.INDEX_SIZE_ERR === 1", "boolean", "true"); |
| 40 | e.expectEval("DOMException.NOT_SUPPORTED_ERR === 9", "boolean", "true"); |
| 41 | e.expectEval("DOMException.SYNTAX_ERR === 12", "boolean", "true"); |
| 42 | e.expectEval("DOMException.INVALID_ACCESS_ERR === 15", "boolean", "true"); |
| 43 | e.expectEval("DOMException.TYPE_MISMATCH_ERR === 17", "boolean", "true"); |
| 44 | e.expectEval("DOMException.QUOTA_EXCEEDED_ERR === 22", "boolean", "true"); |
| 45 | e.expectEval("DOMException.DATA_CLONE_ERR === 25", "boolean", "true"); |
| 46 | } |
| 47 | |
| 48 | KJ_TEST("jsg::Lock domException") { |
| 49 | Evaluator<DOMExceptionContext, DOMExceptionIsolate> e(v8System); |
| 50 | e.getIsolate().runInLockScope([&](DOMExceptionIsolate::Lock& lock) { |
| 51 | JSG_WITHIN_CONTEXT_SCOPE(lock, |
| 52 | lock.template newContext<DOMExceptionContext>().getHandle(lock.v8Isolate), |
| 53 | [&](jsg::Lock& js) { |
| 54 | Ref<DOMException> exception = js.domException(kj::str("foo"), kj::str("bar")); |
| 55 | |
| 56 | // The DOMException object should have the JS handle created already. |
| 57 | auto handle = JsObject(KJ_ASSERT_NONNULL(exception.tryGetHandle(js))); |
| 58 | |
| 59 | // And it will have the stack. |
| 60 | KJ_ASSERT(handle.has(js, "stack"_kj)); |
| 61 | |
| 62 | KJ_ASSERT(exception->getName() == "foo"_kj); |
| 63 | KJ_ASSERT(exception->getMessage() == "bar"_kj); |
| 64 | }); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | } // namespace workerd::jsg::test |