-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the web platform standard reportError API (#1979)
- Loading branch information
1 parent
3b3c4c7
commit 8828214
Showing
16 changed files
with
272 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "events.h" | ||
|
||
namespace workerd::api { | ||
|
||
ErrorEvent::ErrorEvent(kj::String type, ErrorEventInit init) | ||
: Event(kj::mv(type)), init(kj::mv(init)) {} | ||
|
||
jsg::Ref<ErrorEvent> ErrorEvent::constructor( | ||
jsg::Lock& js, | ||
kj::String type, | ||
jsg::Optional<ErrorEventInit> init) { | ||
return jsg::alloc<ErrorEvent>(kj::mv(type), kj::mv(init).orDefault({})); | ||
} | ||
|
||
kj::StringPtr ErrorEvent::getFilename() { | ||
return init.filename.orDefault(nullptr); | ||
} | ||
|
||
kj::StringPtr ErrorEvent::getMessage() { | ||
return init.message.orDefault(nullptr); | ||
} | ||
|
||
int ErrorEvent::getLineno() { | ||
return init.lineno.orDefault(0); | ||
} | ||
|
||
int ErrorEvent::getColno() { | ||
return init.colno.orDefault(0); | ||
} | ||
|
||
jsg::JsValue ErrorEvent::getError(jsg::Lock& js) { | ||
KJ_IF_SOME(error, init.error) { | ||
return error.getHandle(js); | ||
} else { | ||
return js.undefined(); | ||
} | ||
} | ||
|
||
void ErrorEvent::visitForMemoryInfo(jsg::MemoryTracker& tracker) const { | ||
tracker.trackField("message", init.message); | ||
tracker.trackField("filename", init.filename); | ||
tracker.trackField("error", init.error); | ||
} | ||
|
||
void ErrorEvent::visitForGc(jsg::GcVisitor& visitor) { | ||
visitor.visit(init.error); | ||
} | ||
|
||
} // namespace workerd::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <workerd/jsg/jsg.h> | ||
#include "basics.h" | ||
|
||
namespace workerd::api { | ||
|
||
class ErrorEvent: public Event { | ||
public: | ||
struct ErrorEventInit { | ||
jsg::Optional<kj::String> message; | ||
jsg::Optional<kj::String> filename; | ||
jsg::Optional<int32_t> lineno; | ||
jsg::Optional<int32_t> colno; | ||
jsg::Optional<jsg::JsRef<jsg::JsValue>> error; | ||
JSG_STRUCT(message, filename, lineno, colno, error); | ||
}; | ||
|
||
ErrorEvent(kj::String type, ErrorEventInit init); | ||
|
||
static jsg::Ref<ErrorEvent> constructor( | ||
jsg::Lock& js, | ||
kj::String type, | ||
jsg::Optional<ErrorEventInit> init); | ||
|
||
kj::StringPtr getFilename(); | ||
kj::StringPtr getMessage(); | ||
int getLineno(); | ||
int getColno(); | ||
jsg::JsValue getError(jsg::Lock& js); | ||
|
||
JSG_RESOURCE_TYPE(ErrorEvent) { | ||
JSG_INHERIT(Event); | ||
|
||
JSG_READONLY_PROTOTYPE_PROPERTY(filename, getFilename); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(message, getMessage); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(lineno, getLineno); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(colno, getColno); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(error, getError); | ||
|
||
JSG_TS_ROOT(); | ||
} | ||
|
||
void visitForMemoryInfo(jsg::MemoryTracker& tracker) const; | ||
|
||
private: | ||
ErrorEventInit init; | ||
|
||
void visitForGc(jsg::GcVisitor& visitor); | ||
}; | ||
|
||
#define EW_EVENTS_ISOLATE_TYPES \ | ||
api::ErrorEvent, \ | ||
api::ErrorEvent::ErrorEventInit | ||
|
||
} // namespace workerd::api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { mock } from 'node:test'; | ||
import { strictEqual, throws } from 'node:assert'; | ||
|
||
const boom = new Error('boom'); | ||
|
||
const handler = mock.fn((event) => { | ||
if (event.error instanceof Error) { | ||
strictEqual(event.message, 'Uncaught Error: boom'); | ||
strictEqual(event.colno, 13); | ||
strictEqual(event.lineno, 4); | ||
strictEqual(event.filename, 'worker'); | ||
strictEqual(event.error, boom); | ||
} else { | ||
strictEqual(event.message, 'Uncaught boom'); | ||
strictEqual(event.colno, 0); | ||
strictEqual(event.lineno, 25); | ||
strictEqual(event.filename, 'worker'); | ||
strictEqual(event.error, 'boom'); | ||
} | ||
return true; | ||
}); | ||
|
||
addEventListener('error', handler); | ||
|
||
reportError('boom'); | ||
|
||
throws(() => reportError(), { | ||
message: "Failed to execute 'reportError' on 'ServiceWorkerGlobalScope': " + | ||
"parameter 1 is not of type 'JsValue'." | ||
}); | ||
|
||
export const reportErrorTest = { | ||
test() { | ||
// TODO(soon): We are limited in what we can test here because we cannot | ||
// inspect the log output and workerd does not implement that WorkerTracer | ||
// used for collecting data for tail workers. The best we can currently do | ||
// is make sure the basic API is working and that the mock fn was called. | ||
reportError(boom); | ||
strictEqual(handler.mock.calls.length, 2); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ | ||
( name = "reporterror-test", | ||
worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "reporterror-test.js") | ||
], | ||
compatibilityDate = "2023-01-15", | ||
compatibilityFlags = ["nodejs_compat"], | ||
) | ||
), | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.