Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert @bugsnag/plugin-window-unhandled-rejection to TypeScript #2222

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/lib/iserror.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function isError(maybeError: unknown): maybeError is Error
15 changes: 14 additions & 1 deletion packages/plugin-window-unhandled-rejection/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/plugin-window-unhandled-rejection",
"version": "8.0.0",
"main": "unhandled-rejection.js",
"main": "dist/unhandled-rejection.js",
"types": "dist/types/unhandled-rejection.d.ts",
"exports": {
".": {
"types": "./dist/types/unhandled-rejection.d.ts",
"default": "./dist/unhandled-rejection.js",
"import": "./dist/unhandled-rejection.mjs"
}
},
"description": "@bugsnag/js plugin to report unhandled promise rejections in browsers",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -21,5 +29,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/*"
}
}
25 changes: 25 additions & 0 deletions packages/plugin-window-unhandled-rejection/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import createRollupConfig from '../../.rollup/index.mjs'

export default createRollupConfig({
input: 'src/unhandled-rejection.ts',
output: [
{
dir: `dist`,
entryFileNames: '[name].js',
format: 'cjs',
preserveModules: true,
generatedCode: {
preset: 'es2015',
}
},
{
dir: `dist`,
entryFileNames: '[name].mjs',
format: 'esm',
preserveModules: true,
generatedCode: {
preset: 'es2015',
}
}
]
})
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
const map = require('@bugsnag/core/lib/es-utils/map')
const isError = require('@bugsnag/core/lib/iserror')
import { Plugin, Stackframe } from '@bugsnag/core'
import map from '@bugsnag/core/lib/es-utils/map'
import isError from '@bugsnag/core/lib/iserror'

type Listener = (evt: PromiseRejectionEvent) => void

let _listener: Listener | null

let _listener
/*
* Automatically notifies Bugsnag when window.onunhandledrejection is called
*/
module.exports = (win = window) => {
const plugin = {
export default (win = window): Plugin => {
const plugin: Plugin = {
load: (client) => {
// @ts-expect-error _config is private API
if (!client._config.autoDetectErrors || !client._config.enabledErrorTypes.unhandledRejections) return
const listener = evt => {
const listener = (evt: PromiseRejectionEvent) => {
let error = evt.reason
let isBluebird = false

// accessing properties on evt.detail can throw errors (see #394)
try {
// @ts-expect-error detail does not exist on type PromiseRejectionEvent
if (evt.detail && evt.detail.reason) {
// @ts-expect-error detail does not exist on type PromiseRejectionEvent
error = evt.detail.reason
isBluebird = true
}
Expand All @@ -25,6 +32,7 @@ module.exports = (win = window) => {
severity: 'error',
unhandled: true,
severityReason: { type: 'unhandledPromiseRejection' }
// @ts-expect-error _logger is private API
}, 'unhandledrejection handler', 1, client._logger)

if (isBluebird) {
Expand All @@ -37,6 +45,7 @@ module.exports = (win = window) => {
[Object.prototype.toString.call(event.originalError)]: {
name: event.originalError.name,
message: event.originalError.message,
// @ts-expect-error Property 'code' does not exist on type 'Error'
code: event.originalError.code
}
})
Expand All @@ -46,21 +55,24 @@ module.exports = (win = window) => {
if ('addEventListener' in win) {
win.addEventListener('unhandledrejection', listener)
} else {
// @ts-expect-error onunhandledrejection does not exist on type never
win.onunhandledrejection = (reason, promise) => {
// @ts-expect-error detail does not exist on type PromiseRejectionEvent
listener({ detail: { reason, promise } })
}
}
_listener = listener
}
}

// @ts-expect-error cannot find name 'process'
if (process.env.NODE_ENV !== 'production') {
plugin.destroy = (win = window) => {
if (_listener) {
if ('addEventListener' in win) {
win.removeEventListener('unhandledrejection', _listener)
} else {
win.onunhandledrejection = null
(win as Window).onunhandledrejection = null
}
}
_listener = null
Expand Down Expand Up @@ -88,7 +100,7 @@ module.exports = (win = window) => {
//
// Bluebird pads method names with spaces so trim that too…
// https://github.com/petkaantonov/bluebird/blob/b7f21399816d02f979fe434585334ce901dcaf44/src/debuggability.js#L568-L571
const fixBluebirdStacktrace = (error) => (frame) => {
const fixBluebirdStacktrace = (error: PromiseRejectionEvent['reason']) => (frame: Stackframe) => {
if (frame.file === error.toString()) return
if (frame.method) {
frame.method = frame.method.replace(/^\s+/, '')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable jest/no-commented-out-tests */
import plugin from '../'
import plugin from '../src/unhandled-rejection'

import Client from '@bugsnag/core/client'

Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-window-unhandled-rejection/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 @@ -81,7 +81,6 @@
"packages/plugin-restify",
"packages/node",
"packages/plugin-client-ip",
"packages/plugin-window-unhandled-rejection",
"packages/plugin-strip-query-string",
"packages/plugin-strip-project-root",
"packages/plugin-interaction-breadcrumbs",
Expand Down
Loading