Skip to content

Commit c20e8b4

Browse files
don't report errors when running against non-production services
1 parent 9f75877 commit c20e8b4

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/cli-kit/src/public/node/error-handler.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {ciPlatform, cloudEnvironment, isUnitTest, macAddress} from './context/lo
33
import {mockAndCaptureOutput} from './testing/output.js'
44
import * as error from './error.js'
55
import {hashString} from '../../public/node/crypto.js'
6+
import {settings} from '@oclif/core'
67
import {beforeEach, describe, expect, test, vi} from 'vitest'
78

89
const onNotify = vi.fn()
@@ -22,13 +23,22 @@ vi.mock('@bugsnag/js', () => {
2223
vi.mock('./cli.js')
2324
vi.mock('./context/local.js')
2425
vi.mock('../../public/node/crypto.js')
26+
vi.mock('@oclif/core', () => ({
27+
settings: {
28+
debug: false,
29+
},
30+
Interfaces: {},
31+
}))
2532

2633
beforeEach(() => {
2734
vi.mocked(ciPlatform).mockReturnValue({isCI: true, name: 'vitest', metadata: {}})
2835
vi.mocked(macAddress).mockResolvedValue('macAddress')
2936
vi.mocked(cloudEnvironment).mockReturnValue({platform: 'localhost', editor: false})
3037
vi.mocked(hashString).mockReturnValue('hashed-macaddress')
3138
vi.mocked(isUnitTest).mockReturnValue(true)
39+
onNotify.mockClear()
40+
delete process.env.SHOPIFY_SERVICE_ENV
41+
vi.mocked(settings).debug = false
3242
})
3343

3444
describe('errorHandler', async () => {
@@ -108,7 +118,43 @@ describe('bugsnag metadata', () => {
108118
})
109119
})
110120

111-
describe('send to Bugsnag', () => {
121+
describe('skips sending errors to Bugsnag', () => {
122+
test('when SHOPIFY_SERVICE_ENV is local', async () => {
123+
// Given
124+
process.env.SHOPIFY_SERVICE_ENV = 'local'
125+
const mockOutput = mockAndCaptureOutput()
126+
const toThrow = new Error('In test')
127+
128+
// When
129+
const res = await sendErrorToBugsnag(toThrow, 'unexpected_error')
130+
131+
// Then
132+
expect(res.reported).toEqual(false)
133+
expect(res.error).toEqual(toThrow)
134+
expect(res.unhandled).toBeUndefined()
135+
expect(onNotify).not.toHaveBeenCalled()
136+
expect(mockOutput.debug()).toMatch('Skipping Bugsnag report')
137+
})
138+
139+
test('when settings.debug is true', async () => {
140+
// Given
141+
vi.mocked(settings).debug = true
142+
const mockOutput = mockAndCaptureOutput()
143+
const toThrow = new Error('In test')
144+
145+
// When
146+
const res = await sendErrorToBugsnag(toThrow, 'unexpected_error')
147+
148+
// Then
149+
expect(res.reported).toEqual(false)
150+
expect(res.error).toEqual(toThrow)
151+
expect(res.unhandled).toBeUndefined()
152+
expect(onNotify).not.toHaveBeenCalled()
153+
expect(mockOutput.debug()).toMatch('Skipping Bugsnag report')
154+
})
155+
})
156+
157+
describe('sends errors to Bugsnag', () => {
112158
test('processes Error instances as unhandled', async () => {
113159
const toThrow = new Error('In test')
114160
const res = await sendErrorToBugsnag(toThrow, 'unexpected_error')

packages/cli-kit/src/public/node/error-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function sendErrorToBugsnag(
6363
exitMode: Omit<CommandExitMode, 'ok'>,
6464
): Promise<{reported: false; error: unknown; unhandled: unknown} | {error: Error; reported: true; unhandled: boolean}> {
6565
try {
66-
if (settings.debug) {
66+
if (process.env.SHOPIFY_SERVICE_ENV === 'local' || settings.debug) {
6767
outputDebug(`Skipping Bugsnag report`)
6868
return {reported: false, error, unhandled: undefined}
6969
}

0 commit comments

Comments
 (0)