Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit ea2a893

Browse files
committed
feat: improve logging messages
1 parent 97bb8bc commit ea2a893

File tree

10 files changed

+88
-126
lines changed

10 files changed

+88
-126
lines changed

src/designer/create-ui.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@ import { FileType, workspace } from 'vscode'
55
import type { URI } from 'vscode-uri'
66
import type { CommandDeps } from '../commands'
77
import { getTargetDocumentUri } from '../commands'
8-
import type { ExecError, StdErrError } from '../run'
98
import { run } from '../run'
109
import { getToolCommand$ } from '../tool-utils'
1110
import type { ErrorResult, SuccessResult } from '../types'
1211

13-
export async function createUi(
14-
{ extensionUri }: CommandDeps,
15-
...args: any[]
16-
): Promise<CreateUiResult> {
12+
export async function createUi({ extensionUri }: CommandDeps, ...args: any[]) {
1713
const targetDocumentUriResult = getTargetDocumentUri(...args)
1814

1915
if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult
@@ -43,14 +39,6 @@ export async function createUi(
4339
})
4440
}
4541

46-
type CreateUiResult =
47-
| SuccessResult<string>
48-
| ExecError
49-
| StdErrError
50-
| ErrorResult<'NotFound'>
51-
| ErrorResult<'Type'>
52-
| ErrorResult<'IO'>
53-
5442
async function getDirectoryPath(uri: URI): Promise<GetDirectoryPathResult> {
5543
let stat: FileStat
5644
try {

src/designer/edit-ui.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { firstValueFrom } from 'rxjs'
22
import type { CommandDeps } from '../commands'
33
import { getTargetDocumentUri } from '../commands'
4-
import type { ExecError, StdErrError } from '../run'
54
import { run } from '../run'
65
import { getToolCommand$ } from '../tool-utils'
7-
import type { ErrorResult, SuccessResult } from '../types'
86

9-
export async function editUi(
10-
{ extensionUri }: CommandDeps,
11-
...args: any[]
12-
): Promise<EditUiResult> {
7+
export async function editUi({ extensionUri }: CommandDeps, ...args: any[]) {
138
const targetDocumentUriResult = getTargetDocumentUri(...args)
149

1510
if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult
@@ -34,10 +29,3 @@ export async function editUi(
3429
],
3530
})
3631
}
37-
38-
type EditUiResult =
39-
| SuccessResult<string>
40-
| ExecError
41-
| StdErrError
42-
| ErrorResult<'NotFound'>
43-
| ErrorResult<'Type'>

src/extension.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type { ErrorResult, SuccessResult } from './types'
1111
import { registerUicLiveExecution$ } from './uic/uic-live-execution'
1212
import { toDisposable } from './utils'
1313

14+
const JSON_INDENT = 2
15+
1416
export async function activate({
1517
extensionUri,
1618
subscriptions,
@@ -33,7 +35,15 @@ export async function activate({
3335
onResultReceived(
3436
{
3537
kind: 'UnexpectedError',
36-
message: `Unexpected error: ${JSON.stringify(e)}`,
38+
message: `${JSON.stringify(e, null, JSON_INDENT)}`,
39+
},
40+
outputChannel,
41+
),
42+
complete: () =>
43+
onResultReceived(
44+
{
45+
kind: 'Success',
46+
value: 'An observable is completed',
3747
},
3848
outputChannel,
3949
),
@@ -46,9 +56,11 @@ export async function activate({
4656
catchError((err: unknown, caught$) => {
4757
return of({
4858
kind: 'UnexpectedError',
49-
message: `Unexpected error: ${JSON.stringify(
59+
message: `${JSON.stringify(
5060
err,
51-
)} (caught: ${JSON.stringify(caught$)})`,
61+
null,
62+
JSON_INDENT,
63+
)}\n(caught: ${JSON.stringify(caught$, null, JSON_INDENT)})`,
5264
} as const)
5365
}),
5466
)
@@ -60,20 +72,22 @@ export async function activate({
6072
}
6173

6274
function onResultReceived(result: Result, outputChannel: OutputChannel) {
63-
const indent = 2
6475
switch (result.kind) {
6576
case 'Success':
6677
return outputChannel.appendLine(
6778
prefixLogging({
68-
message: JSON.stringify(result.value, null, indent),
79+
message:
80+
typeof result.value === 'string'
81+
? result.value
82+
: JSON.stringify(result.value, null, JSON_INDENT),
6983
severity: 'INFO',
7084
}),
7185
)
7286
case 'ParseError':
7387
case 'TypeError':
7488
case 'NotFoundError':
7589
case 'UnexpectedError':
76-
return showError(result.message, outputChannel)
90+
return showError(`${result.kind}: ${result.message}`, outputChannel)
7791
case 'ExecError':
7892
return showError(
7993
`${result.stderr}\n${result.stdout}\n${result.error.message ?? ''}`,

src/python.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function resolveScriptCommand$({
2222
...result.value,
2323
path.join(extensionUri.fsPath, 'python', 'scripts', `${tool}.py`),
2424
],
25-
}
25+
} as const
2626
}),
2727
)
2828
}
@@ -37,9 +37,7 @@ export type ResolveScriptCommandResult =
3737
| SuccessResult<CommandArgs>
3838
| ErrorResult<'NotFound'>
3939

40-
function getPythonInterpreterPath$(
41-
resource: URI | undefined,
42-
): Observable<GetPythonInterpreterPathResult> {
40+
function getPythonInterpreterPath$(resource: URI | undefined) {
4341
return defer(async () => getPythonExtensionApi()).pipe(
4442
concatMap(result => {
4543
if (result.kind !== 'Success') return of(result)
@@ -61,7 +59,7 @@ type GetPythonInterpreterPathResult =
6159
| SuccessResult<CommandArgs>
6260
| ErrorResult<'NotFound'>
6361

64-
async function getPythonExtensionApi(): Promise<GetPythonExtensionApiResult> {
62+
async function getPythonExtensionApi() {
6563
const pythonExtensionApi = await extensions
6664
.getExtension<PythonExtensionApi>('ms-python.python')
6765
?.activate()
@@ -70,42 +68,31 @@ async function getPythonExtensionApi(): Promise<GetPythonExtensionApiResult> {
7068
return {
7169
kind: 'NotFoundError',
7270
message: 'Python extension not found.',
73-
}
71+
} as const
7472

7573
await pythonExtensionApi.ready
7674

77-
return { kind: 'Success', value: pythonExtensionApi }
75+
return { kind: 'Success', value: pythonExtensionApi } as const
7876
}
7977

80-
type GetPythonExtensionApiResult =
81-
| SuccessResult<PythonExtensionApi>
82-
| ErrorResult<'NotFound'>
83-
84-
function getPythonExecCommand({
85-
api,
86-
resource,
87-
}: GetPythonExecCommandArgs): GetPythonExecCommandResult {
78+
function getPythonExecCommand({ api, resource }: GetPythonExecCommandArgs) {
8879
const command = api.settings.getExecutionDetails(resource).execCommand
8980

9081
if (isNil(command) || command.length === 0)
9182
return {
9283
kind: 'NotFoundError',
9384
message:
9485
'Python interpreter cannot could not be retrieved from the Python extension.',
95-
}
86+
} as const
9687

97-
return { kind: 'Success', value: [...command] }
88+
return { kind: 'Success', value: [...command] } as const
9889
}
9990

10091
type GetPythonExecCommandArgs = {
10192
readonly api: PythonExtensionApi
10293
readonly resource: URI | undefined
10394
}
10495

105-
type GetPythonExecCommandResult =
106-
| SuccessResult<string[]>
107-
| ErrorResult<'NotFound'>
108-
10996
// Excerpt from: https://github.com/microsoft/vscode-python/blob/344c912a1c15d07eb9b14bf749c7529a7fa0877b/src/client/apiTypes.ts
11097
type PythonExtensionApi = {
11198
ready: Promise<void>

src/qmlls/client.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import {
2020
} from 'vscode-languageclient/node'
2121
import type { URI } from 'vscode-uri'
2222
import { getEnabledFromConfig$ } from '../configurations'
23-
import type { CommandArgs, ExecError, StdErrError } from '../run'
23+
import type { CommandArgs } from '../run'
2424
import { run, wrapAndJoinCommandArgsWithQuotes } from '../run'
2525
import { getToolCommand$ } from '../tool-utils'
26-
import type { ErrorResult, SuccessResult } from '../types'
2726

2827
export function registerQmlLanguageServer$({
2928
extensionUri,
3029
outputChannel,
31-
}: RegisterQmlLanguageServerArgs): Observable<RegisterQmlLanguageServerResult> {
30+
}: RegisterQmlLanguageServerArgs) {
3231
const client$ = new ReplaySubject<LanguageClient | undefined>(1)
3332

3433
const createClient$ = getToolCommand$({
@@ -67,9 +66,9 @@ export function registerQmlLanguageServer$({
6766
await client?.start()
6867
return {
6968
kind: 'Success',
70-
value: `qmlls is enabled with latest config: ${JSON.stringify(
71-
result.value,
72-
)}`,
69+
value: `qmlls is running with the command: '${wrapAndJoinCommandArgsWithQuotes(
70+
[...result.value.command, ...result.value.options],
71+
)}'`,
7372
} as const
7473
}),
7574
),
@@ -83,7 +82,7 @@ export function registerQmlLanguageServer$({
8382
if (!enabled)
8483
return defer(async () => {
8584
client$.next(undefined)
86-
return { kind: 'Success', value: 'qmlls is disabled' } as const
85+
return { kind: 'Success', value: 'qmlls has been stopped' } as const
8786
})
8887

8988
return createClient$
@@ -96,12 +95,6 @@ type RegisterQmlLanguageServerArgs = {
9695
readonly outputChannel: OutputChannel
9796
}
9897

99-
type RegisterQmlLanguageServerResult =
100-
| ErrorResult<'NotFound'>
101-
| ExecError
102-
| StdErrError
103-
| SuccessResult<string>
104-
10598
async function checkQmllsExists(command: CommandArgs) {
10699
return run({ command: [...command, '--help'] })
107100
}

src/rcc/compile-resource.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { firstValueFrom } from 'rxjs'
22
import type { CommandDeps } from '../commands'
33
import { getTargetDocumentUri } from '../commands'
4-
import type { ExecError, StdErrError } from '../run'
54
import { run } from '../run'
65
import { getToolCommand$ } from '../tool-utils'
7-
import type { ErrorResult, SuccessResult } from '../types'
86

97
export async function compileResource(
108
{ extensionUri }: CommandDeps,
119
...args: any[]
12-
): Promise<CompileResourceResult> {
10+
) {
1311
const targetDocumentUriResult = getTargetDocumentUri(...args)
1412

1513
if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult
@@ -34,10 +32,3 @@ export async function compileResource(
3432
],
3533
})
3634
}
37-
38-
type CompileResourceResult =
39-
| SuccessResult<string>
40-
| ExecError
41-
| StdErrError
42-
| ErrorResult<'NotFound'>
43-
| ErrorResult<'Type'>

src/run.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,54 @@ import { exec } from 'node:child_process'
55
import type { SuccessResult } from './types'
66
import { notNil } from './utils'
77

8-
export async function run({ command, cwd }: RunArgs): Promise<RunResult> {
8+
export async function run({ command, cwd }: RunArgs) {
9+
const commandString = wrapAndJoinCommandArgsWithQuotes(command)
10+
911
return new Promise<RunResult>(resolve => {
10-
exec(
11-
wrapAndJoinCommandArgsWithQuotes(command),
12-
{ cwd },
13-
(error, stdout, stderr) => {
14-
if (notNil(error)) resolve({ kind: 'ExecError', error, stdout, stderr })
15-
if (stderr.length !== 0)
16-
resolve({ kind: 'StdErrError', stdout, stderr })
17-
resolve({ kind: 'Success', value: stdout })
18-
},
19-
)
12+
exec(commandString, { cwd }, (error, stdout, stderr) => {
13+
if (notNil(error))
14+
resolve({
15+
kind: 'ExecError',
16+
command: commandString,
17+
error,
18+
stdout,
19+
stderr,
20+
})
21+
if (stderr.length !== 0)
22+
resolve({ kind: 'StdErrError', command: commandString, stdout, stderr })
23+
resolve({
24+
kind: 'Success',
25+
value: {
26+
stdout,
27+
command: commandString,
28+
},
29+
})
30+
})
2031
})
2132
}
2233

2334
type RunArgs = { readonly command: CommandArgs; readonly cwd?: string }
2435

2536
export type CommandArgs = readonly string[]
2637

27-
export type RunResult = SuccessResult<string> | ExecError | StdErrError
38+
export type RunResult = SuccessResult<RunSuccessValue> | ExecError | StdErrError
39+
40+
type RunSuccessValue = {
41+
readonly stdout: string
42+
readonly command: string
43+
}
2844

2945
export type ExecError = {
3046
readonly kind: 'ExecError'
47+
readonly command: string
3148
readonly error: Partial<ExecException>
3249
readonly stdout: string
3350
readonly stderr: string
3451
}
3552

3653
export type StdErrError = {
3754
readonly kind: 'StdErrError'
55+
readonly command: string
3856
readonly stdout: string
3957
readonly stderr: string
4058
}

src/test/suite/run.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ suite('run', () => {
1818
suite(
1919
'when execute cat command to read exist file with spaces in filename',
2020
() => {
21-
setup(async () => {
22-
const filePath = path.resolve(
23-
TEST_ASSETS_PATH,
24-
'filename with spaces.txt',
25-
)
26-
27-
if (process.platform === 'win32')
28-
result = await run({ command: ['type', filePath] })
29-
else result = await run({ command: ['cat', filePath] })
30-
})
21+
const filePath = path.resolve(
22+
TEST_ASSETS_PATH,
23+
'filename with spaces.txt',
24+
)
25+
26+
const command =
27+
process.platform === 'win32' ? ['type', filePath] : ['cat', filePath]
28+
29+
setup(async () => (result = await run({ command })))
3130

3231
test('should return Success with contents', async () => {
33-
const expected: RunResult = { kind: 'Success', value: 'hello' }
32+
const expected: RunResult = {
33+
kind: 'Success',
34+
value: {
35+
stdout: 'hello',
36+
command: wrapAndJoinCommandArgsWithQuotes(command),
37+
},
38+
}
3439
assert.deepStrictEqual(result, expected)
3540
})
3641
},

0 commit comments

Comments
 (0)