Skip to content

Commit

Permalink
refactor: ♻️ convert plugin-window-onerror to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Oct 15, 2024
1 parent a8d5637 commit 5818712
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
10 changes: 8 additions & 2 deletions packages/plugin-window-onerror/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@bugsnag/plugin-window-onerror",
"version": "8.0.0",
"main": "onerror.js",
"main": "dist/onerror.js",
"types": "dist/types/onerror.d.ts",
"description": "@bugsnag/js plugin to report unhandled exceptions in browsers",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -12,7 +13,7 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"author": "Bugsnag",
"license": "MIT",
Expand All @@ -21,5 +22,10 @@
},
"peerDependencies": {
"@bugsnag/core": "^8.0.0"
},
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
}
}
5 changes: 5 additions & 0 deletions packages/plugin-window-onerror/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createRollupConfig from '../../.rollup/index.mjs'

export default createRollupConfig({
input: 'src/onerror.ts'
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
* Automatically notifies Bugsnag when window.onerror is called
*/

module.exports = (win = window, component = 'window onerror') => ({
load: (client) => {
import type { Client, Stackframe } from 'packages/core/types'

export default (win = window, component = 'window onerror') => ({
load: (client: Client) => {
// @ts-expect-error _config is private API
if (!client._config.autoDetectErrors) return
if (!client._config.enabledErrorTypes.unhandledExceptions) return
function onerror (messageOrEvent, url, lineNo, charNo, error) {
// @ts-expect-error _config is private API
if (!client._config.enabledErrorTypes?.unhandledExceptions) return
function onerror (messageOrEvent: string | Event, url?: string, lineNo?: number, charNo?: number, error?: Error) {
// Ignore errors with no info due to CORS settings
if (lineNo === 0 && /Script error\.?/.test(messageOrEvent)) {
if (lineNo === 0 && /Script error\.?/.test(messageOrEvent.toString())) {
// @ts-expect-error _logger is private API
client._logger.warn('Ignoring cross-domain or eval script error. See docs: https://tinyurl.com/yy3rn63z')
} else {
// any error sent to window.onerror is unhandled and has severity=error
Expand Down Expand Up @@ -41,11 +46,13 @@ module.exports = (win = window, component = 'window onerror') => ({
const name = messageOrEvent.type ? `Event: ${messageOrEvent.type}` : 'Error'
// attempt to find a message from one of the conventional properties, but
// default to empty string (the event will fill it with a placeholder)
// @ts-expect-error TODO: messageOrEvent has no message or detail property
const message = messageOrEvent.message || messageOrEvent.detail || ''

event = client.Event.create({ name, message }, true, handledState, component, 1)

// provide the original thing onerror received – not our error-like object we passed to _notify
// @ts-expect-error originalError is readonly
event.originalError = messageOrEvent

// include the raw input as metadata – it might contain more info than we extracted
Expand All @@ -60,7 +67,7 @@ module.exports = (win = window, component = 'window onerror') => ({
client._notify(event)
}

if (typeof prevOnError === 'function') prevOnError.apply(this, arguments)
if (typeof prevOnError === 'function') prevOnError.apply(win, [messageOrEvent, url, lineNo, charNo, error])
}

const prevOnError = win.onerror
Expand All @@ -71,18 +78,20 @@ module.exports = (win = window, component = 'window onerror') => ({
// Sometimes the stacktrace has less information than was passed to window.onerror.
// This function will augment the first stackframe with any useful info that was
// received as arguments to the onerror callback.
const decorateStack = (stack, url, lineNo, charNo) => {
if (!stack[0]) stack.push({})
const decorateStack = (stack: Stackframe[], url?: string, lineNo?: number, charNo?: number) => {
if (!stack[0]) stack.push({ file: '' })
const culprit = stack[0]
if (!culprit.file && typeof url === 'string') culprit.file = url
if (!culprit.lineNumber && isActualNumber(lineNo)) culprit.lineNumber = lineNo
if (!culprit.columnNumber) {
if (isActualNumber(charNo)) {
culprit.columnNumber = charNo
// @ts-expect-error event.errorCharacter does not exist on type 'Event' (deprecated)
} else if (window.event && isActualNumber(window.event.errorCharacter)) {
// @ts-expect-error event.errorCharacter does not exist on type 'Event' (deprecated)
culprit.columnNumber = window.event.errorCharacter
}
}
}

const isActualNumber = (n) => typeof n === 'number' && String.call(n) !== 'NaN'
const isActualNumber = (n: unknown): n is number => typeof n === 'number' && String.call(n) !== 'NaN'
2 changes: 1 addition & 1 deletion packages/plugin-window-onerror/test/onerror.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jest/no-commented-out-tests */

import plugin from '../'
import plugin from '../src/onerror'

import Client, { EventDeliveryPayload } from '@bugsnag/core/client'

Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-window-onerror/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"target": "ES2020"
}
}

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"packages/node",
"packages/plugin-client-ip",
"packages/plugin-window-unhandled-rejection",
"packages/plugin-window-onerror",
"packages/plugin-strip-query-string",
"packages/plugin-strip-project-root",
"packages/plugin-interaction-breadcrumbs",
Expand Down

0 comments on commit 5818712

Please sign in to comment.