{"record":{"id":"0e883e162e121201","repo":"denoland/deno","slug":"illegal-invocation-0e883e","errorCode":null,"errorMessage":"Illegal invocation","messagePattern":"Illegal invocation","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/02_event.js","lineNumber":1716,"sourceCode":"  }\n  const event = new ErrorEvent(\"error\", {\n    cancelable: true,\n    message,\n    filename,\n    lineno,\n    colno,\n    error,\n  });\n  // Avoid recursing `reportException()` via error handlers more than once.\n  if (reportExceptionStackedCalls > 1 || globalThis_.dispatchEvent(event)) {\n    core.reportUnhandledException(error);\n  }\n  reportExceptionStackedCalls--;\n}\n\nfunction checkThis(thisArg) {\n  if (thisArg !== null && thisArg !== undefined && thisArg !== globalThis_) {\n    throw new TypeError(\"Illegal invocation\");\n  }\n}\n\n// https://html.spec.whatwg.org/#dom-reporterror\nfunction reportError(error) {\n  checkThis(this);\n  const prefix = \"Failed to execute 'reportError'\";\n  webidl.requiredArguments(arguments.length, 1, prefix);\n  reportException(error);\n}\n\ninternals.defineEventHandler = defineEventHandler;\ninternals.setEventTargetData = setEventTargetData;\n\nreturn {\n  CloseEvent,\n  CustomEvent,\n  defineEventHandler,","sourceCodeStart":1698,"sourceCodeEnd":1734,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/02_event.js#L1698-L1734","documentation":"reportError is a bare global function, and checkThis (ext/web/02_event.js:1712-1716) throws TypeError 'Illegal invocation' when it runs with a this that is neither null, undefined, nor globalThis. This is the standard WebIDL guard for global functions that must not be invoked as methods of another object. It fires when reportError is assigned as a property of an object or class and called through it, or invoked via call()/apply() with a non-global receiver.","triggerScenarios":"const o = { report: reportError }; o.report(err); reportError.call({}, err); class Logger { re = reportError; fire(e) { this.re(e); } } — any invocation where this binds to a non-global object.","commonSituations":"Wrapping reportError in logger objects, DI containers, or class methods; spy/mock frameworks that capture method references and re-invoke them with a receiver; code that enumerates globals and re-attaches them as object members.","solutions":["Call reportError as a bare global: reportError(err) or globalThis.reportError(err).","When aliasing it, wrap in an arrow function ((e) => reportError(e)) or bind it: const report = reportError.bind(globalThis).","Search for assignments like obj.x = reportError and .call(/.apply wrappers around it."],"exampleFix":"// before\nconst logger = { report: reportError };\nlogger.report(err); // TypeError: Illegal invocation\n\n// after\nconst logger = { report: (e) => reportError(e) };\nlogger.report(err);","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"const safeReport = (e) => {\n  try {\n    return obj.report(e);\n  } catch (err) {\n    if (err instanceof TypeError && err.message === 'Illegal invocation') {\n      return globalThis.reportError(e);\n    }\n    throw err;\n  }\n};","preventionTips":["Invoke reportError only as a bare global call","Alias it as an arrow function: const report = (e) => reportError(e)","Bind when capturing: reportError.bind(globalThis)","Avoid attaching global functions to objects/classes as methods"],"tags":["reporterror","this-binding","web-api"],"backgroundTag":"illegal-invocation","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}