Skip to content

Commit bc5aaa1

Browse files
authored
Merge pull request #5292 from Shopify/js.show-error-message-when-no-logs-available
show error message in replay command when no logs available
2 parents 13fed76 + 61a1a10 commit bc5aaa1

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

.changeset/lemon-eggs-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/app': patch
3+
---
4+
5+
Improve error message in function replay command when log directory doesnt exist

packages/app/src/cli/services/function/replay.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {readFile} from '@shopify/cli-kit/node/fs'
1010
import {describe, expect, beforeAll, test, vi} from 'vitest'
1111
import {AbortError} from '@shopify/cli-kit/node/error'
1212
import {outputInfo} from '@shopify/cli-kit/node/output'
13-
import {readdirSync} from 'fs'
13+
import {getLogsDir} from '@shopify/cli-kit/node/logs'
14+
15+
import {existsSync, readdirSync} from 'fs'
1416

1517
vi.mock('fs')
1618
vi.mock('@shopify/cli-kit/node/fs')
@@ -111,6 +113,24 @@ describe('replay', () => {
111113

112114
test('throws error if no logs available', async () => {
113115
// Given
116+
mockFileOperations([])
117+
118+
// When/Then
119+
await expect(async () => {
120+
await replay({
121+
app: testAppLinked(),
122+
extension,
123+
stdout: false,
124+
path: 'test-path',
125+
json: true,
126+
watch: false,
127+
})
128+
}).rejects.toThrow(new AbortError(`No logs found in ${getLogsDir()}`))
129+
})
130+
131+
test('throws error if log directory does not exist', async () => {
132+
// Given
133+
vi.mocked(existsSync).mockReturnValue(false)
114134

115135
// When/Then
116136
await expect(async () => {
@@ -122,7 +142,7 @@ describe('replay', () => {
122142
json: true,
123143
watch: false,
124144
})
125-
}).rejects.toThrow()
145+
}).rejects.toThrow(new AbortError(`No logs found in ${getLogsDir()}`))
126146
})
127147

128148
test('delegates to renderReplay when watch is true', async () => {
@@ -285,6 +305,7 @@ function expectFunctionRun(functionExtension: ExtensionInstance<FunctionConfigTy
285305
}
286306

287307
function mockFileOperations(data: {run: FunctionRunData; path: string}[]) {
308+
vi.mocked(existsSync).mockReturnValue(true)
288309
vi.mocked(readdirSync).mockReturnValue([...data].reverse().map(({path}) => path) as any)
289310
vi.mocked(readFile).mockImplementation((path) => {
290311
const run = data.find((file) => path.endsWith(file.path))

packages/app/src/cli/services/function/replay.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {getLogsDir} from '@shopify/cli-kit/node/logs'
1212
import {AbortError} from '@shopify/cli-kit/node/error'
1313
import {AbortController} from '@shopify/cli-kit/node/abort'
1414

15-
import {readdirSync} from 'fs'
15+
import {existsSync, readdirSync} from 'fs'
1616

1717
const LOG_SELECTOR_LIMIT = 100
1818

@@ -124,7 +124,7 @@ async function findFunctionRun(
124124
functionHandle: string,
125125
identifier: string,
126126
): Promise<string | undefined> {
127-
const fileName = readdirSync(functionRunsDir).find((filename) => {
127+
const fileName = getAllFunctionRunFileNames(functionRunsDir).find((filename) => {
128128
const fileMetadata = parseLogFilename(filename)
129129
return (
130130
fileMetadata?.namespace === 'extensions' &&
@@ -147,7 +147,7 @@ async function getRunFromSelector(functionRunsDir: string, functionHandle: strin
147147
}
148148

149149
async function getFunctionRunData(functionRunsDir: string, functionHandle: string): Promise<FunctionRunData[]> {
150-
const allFunctionRunFileNames = readdirSync(functionRunsDir)
150+
const allFunctionRunFileNames = getAllFunctionRunFileNames(functionRunsDir)
151151
.filter((filename) => {
152152
// Expected format: 20240522_150641_827Z_extensions_my-function_abcdef.json
153153
const fileMetadata = parseLogFilename(filename)
@@ -191,3 +191,7 @@ async function getFunctionRunData(functionRunsDir: string, functionHandle: strin
191191
function getIdentifierFromFilename(fileName: string): string {
192192
return fileName.split('_').pop()!.substring(0, 6)
193193
}
194+
195+
function getAllFunctionRunFileNames(functionRunsDir: string): string[] {
196+
return existsSync(functionRunsDir) ? readdirSync(functionRunsDir) : []
197+
}

0 commit comments

Comments
 (0)