Skip to content
Draft
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
23 changes: 15 additions & 8 deletions packages/build/src/error/monitor/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import Bugsnag from '@bugsnag/js'
import memoizeOne from 'memoize-one'

import type { ResolvedFlags } from '../../core/normalize_flags.js'
import { Logs, log } from '../../log/logger.js'
import { type Logs, getSystemLogger, log } from '../../log/logger.js'
import { ROOT_PACKAGE_JSON } from '../../utils/json.js'
import type { SystemLogger } from '../../plugins_core/types.js'

const projectRoot = fileURLToPath(new URL('../../..', import.meta.url))

// Start a client to monitor errors
export const startErrorMonitor = function (config: { flags: ResolvedFlags; logs?: Logs; bugsnagKey?: string }) {
const {
flags: { mode },
flags: { debug, mode, systemLogFile },
logs,
bugsnagKey,
} = config
Expand All @@ -23,22 +24,28 @@ export const startErrorMonitor = function (config: { flags: ResolvedFlags; logs?

const isTest = isBugsnagTest(bugsnagKey)
const releaseStage = getReleaseStage(mode)
const logger = getLogger(logs, isTest)
const logger = getLogger(getSystemLogger(logs, debug, systemLogFile), isTest)
try {
const errorMonitor = startBugsnag({
apiKey: bugsnagKey,
apiKey: 'aabbccddaabbccddaabbccddaabbccdd',
appVersion: `${ROOT_PACKAGE_JSON.name} ${ROOT_PACKAGE_JSON.version}`,
appType: ROOT_PACKAGE_JSON.name,
releaseStage,
logger,
projectRoot,
endpoints: {
notify: 'http://dry-run:9999/notify', // nothing listening β†’ fast failure
sessions: 'http://dry-run:9999/sessions',
},
})

// Allows knowing the percentage of failed builds per release
if (!isTest) {
errorMonitor.startSession()
}

errorMonitor.notify(new Error('just testing stuff'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can clean up the changes made for testing like this one, this looks good to me


return errorMonitor
// Failsafe
} catch (error) {
Expand All @@ -57,7 +64,7 @@ const BUGSNAG_TEST_KEY = '00000000000000000000000000000000'
// several times.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const startBugsnag = memoizeOne(Bugsnag.start.bind(Bugsnag), () => true)
const startBugsnag = memoizeOne(Bugsnag.start.bind(Bugsnag), () => true) as typeof Bugsnag.default.start

// Based the release stage on the `mode`
const getReleaseStage = function (mode = DEFAULT_RELEASE_STAGE) {
Expand All @@ -70,9 +77,9 @@ const DEFAULT_RELEASE_STAGE = 'unknown'
// We also want to use our own `log` utility, unprefixed.
// In tests, we don't print Bugsnag because it sometimes randomly fails to
// send sessions, which prints warning messags in test snapshots.
const getLogger = function (logs, isTest) {
const logFunc = isTest ? noop : log.bind(null, logs)
return { debug: noop, info: noop, warn: logFunc, error: logFunc }
const getLogger = function (systemLogger: SystemLogger, isTest) {
const logFunc = isTest ? noop : log.bind(null, systemLogger)
return { debug: noop, info: noop, warn: logFunc, error: logFunc } satisfies Bugsnag.Logger
}

const noop = function () {
Expand Down
Loading