Skip to content

Commit 441457e

Browse files
use service function to check if local services being used
1 parent c20e8b4 commit 441457e

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

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: 5 additions & 3 deletions
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 {isLocalEnvironment} from '../../private/node/context/service.js'
67
import {settings} from '@oclif/core'
78
import {beforeEach, describe, expect, test, vi} from 'vitest'
89

@@ -23,6 +24,7 @@ vi.mock('@bugsnag/js', () => {
2324
vi.mock('./cli.js')
2425
vi.mock('./context/local.js')
2526
vi.mock('../../public/node/crypto.js')
27+
vi.mock('../../private/node/context/service.js')
2628
vi.mock('@oclif/core', () => ({
2729
settings: {
2830
debug: false,
@@ -37,8 +39,8 @@ beforeEach(() => {
3739
vi.mocked(hashString).mockReturnValue('hashed-macaddress')
3840
vi.mocked(isUnitTest).mockReturnValue(true)
3941
onNotify.mockClear()
40-
delete process.env.SHOPIFY_SERVICE_ENV
4142
vi.mocked(settings).debug = false
43+
vi.mocked(isLocalEnvironment).mockReturnValue(false)
4244
})
4345

4446
describe('errorHandler', async () => {
@@ -119,9 +121,9 @@ describe('bugsnag metadata', () => {
119121
})
120122

121123
describe('skips sending errors to Bugsnag', () => {
122-
test('when SHOPIFY_SERVICE_ENV is local', async () => {
124+
test('when using local services', async () => {
123125
// Given
124-
process.env.SHOPIFY_SERVICE_ENV = 'local'
126+
vi.mocked(isLocalEnvironment).mockReturnValue(true)
125127
const mockOutput = mockAndCaptureOutput()
126128
const toThrow = new Error('In test')
127129

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 (process.env.SHOPIFY_SERVICE_ENV === 'local' || 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)