Skip to content

Commit ba8eff9

Browse files
Merge pull request #6360 from Shopify/09-05-don_t_report_errors_when_running_against_non-production_services
don't report errors when running against non-production services
2 parents 9f75877 + a1829e5 commit ba8eff9

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

packages/app/src/cli/api/graphql/app-management/generated/specifications.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type FetchSpecificationsQuery = {
1616
uidStrategy:
1717
| {appModuleLimit: number; isClientProvided: boolean}
1818
| {appModuleLimit: number; isClientProvided: boolean}
19+
| {appModuleLimit: number; isClientProvided: boolean}
1920
validationSchema?: {jsonSchema: string} | null
2021
}[]
2122
}

packages/cli-kit/src/private/node/context/service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ export function serviceEnvironment(env = process.env): Environment {
2424
return Environment.Production
2525
}
2626
}
27+
28+
/**
29+
* Returns true if the environment is local.
30+
*
31+
* @param env - Environment variables.
32+
* @returns True if the environment is local.
33+
*/
34+
export function isLocalEnvironment(env = process.env): boolean {
35+
return serviceEnvironment(env) === Environment.Local
36+
}

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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 {isLocalEnvironment} from '../../private/node/context/service.js'
7+
import {settings} from '@oclif/core'
68
import {beforeEach, describe, expect, test, vi} from 'vitest'
79

810
const onNotify = vi.fn()
@@ -22,13 +24,23 @@ vi.mock('@bugsnag/js', () => {
2224
vi.mock('./cli.js')
2325
vi.mock('./context/local.js')
2426
vi.mock('../../public/node/crypto.js')
27+
vi.mock('../../private/node/context/service.js')
28+
vi.mock('@oclif/core', () => ({
29+
settings: {
30+
debug: false,
31+
},
32+
Interfaces: {},
33+
}))
2534

2635
beforeEach(() => {
2736
vi.mocked(ciPlatform).mockReturnValue({isCI: true, name: 'vitest', metadata: {}})
2837
vi.mocked(macAddress).mockResolvedValue('macAddress')
2938
vi.mocked(cloudEnvironment).mockReturnValue({platform: 'localhost', editor: false})
3039
vi.mocked(hashString).mockReturnValue('hashed-macaddress')
3140
vi.mocked(isUnitTest).mockReturnValue(true)
41+
onNotify.mockClear()
42+
vi.mocked(settings).debug = false
43+
vi.mocked(isLocalEnvironment).mockReturnValue(false)
3244
})
3345

3446
describe('errorHandler', async () => {
@@ -108,7 +120,43 @@ describe('bugsnag metadata', () => {
108120
})
109121
})
110122

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
handler,
1111
cleanSingleStackTracePath,
1212
} from './error.js'
13+
import {isLocalEnvironment} from '../../private/node/context/service.js'
1314
import {getEnvironmentData} from '../../private/node/analytics.js'
1415
import {outputDebug, outputInfo} from '../../public/node/output.js'
1516
import {bugsnagApiKey, reportingRateLimit} from '../../private/node/constants.js'
@@ -63,7 +64,7 @@ export async function sendErrorToBugsnag(
6364
exitMode: Omit<CommandExitMode, 'ok'>,
6465
): Promise<{reported: false; error: unknown; unhandled: unknown} | {error: Error; reported: true; unhandled: boolean}> {
6566
try {
66-
if (settings.debug) {
67+
if (isLocalEnvironment() || settings.debug) {
6768
outputDebug(`Skipping Bugsnag report`)
6869
return {reported: false, error, unhandled: undefined}
6970
}

0 commit comments

Comments
 (0)