Skip to content

Commit c7b97e2

Browse files
committed
refactor: move tryFunctions to core and include tests
1 parent 7d05c68 commit c7b97e2

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

packages/core/src/amazonq/lsp/util.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,3 @@ export async function lspSetupStage<T>(
2626
return result
2727
})
2828
}
29-
30-
/**
31-
* Try Functions in the order presented and return the first returned result. If none return, throw the final error.
32-
* @param functions non-empty list of functions to try.
33-
* @returns
34-
*/
35-
export async function tryFunctions<Result>(functions: (() => Promise<Result>)[]): Promise<Result> {
36-
let currentError: Error = new Error('No functions provided')
37-
for (const func of functions) {
38-
try {
39-
return await func()
40-
} catch (e) {
41-
currentError = e as Error
42-
}
43-
}
44-
throw currentError
45-
}

packages/core/src/shared/lsp/lspResolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { TargetContent, logger, LspResult, LspVersion, Manifest } from './types'
1313
import { getApplicationSupportFolder } from '../vscode/env'
1414
import { createHash } from '../crypto'
1515
import request from '../request'
16-
import { lspSetupStage, tryFunctions } from '../../amazonq/lsp/util'
16+
import { lspSetupStage } from '../../amazonq/lsp/util'
17+
import { tryFunctions } from '../utilities/tsUtils'
1718

1819
export class LanguageServerResolver {
1920
constructor(

packages/core/src/shared/lsp/manifestResolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { RetryableResourceFetcher } from '../resourcefetcher/httpResourceFetcher
99
import { Timeout } from '../utilities/timeoutUtils'
1010
import globals from '../extensionGlobals'
1111
import { Manifest } from './types'
12-
import { lspSetupStage, tryFunctions } from '../../amazonq/lsp/util'
12+
import { lspSetupStage } from '../../amazonq/lsp/util'
13+
import { tryFunctions } from '../utilities/tsUtils'
1314

1415
const logger = getLogger('lsp')
1516

packages/core/src/shared/utilities/tsUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ export function createFactoryFunction<T extends new (...args: any[]) => any>(cto
9494
return (...args) => new ctor(...args)
9595
}
9696

97+
/**
98+
* Try Functions in the order presented and return the first returned result. If none return, throw the final error.
99+
* @param functions non-empty list of functions to try.
100+
* @returns
101+
*/
102+
export async function tryFunctions<Result>(functions: (() => Promise<Result>)[]): Promise<Result> {
103+
let currentError: Error = new Error('No functions provided')
104+
for (const func of functions) {
105+
try {
106+
return await func()
107+
} catch (e) {
108+
currentError = e as Error
109+
}
110+
}
111+
throw currentError
112+
}
113+
97114
type NoSymbols<T> = { [Property in keyof T]: Property extends symbol ? never : Property }[keyof T]
98115
export type InterfaceNoSymbol<T> = Pick<T, NoSymbols<T>>
99116
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { tryFunctions } from '../../../shared/utilities/tsUtils'
7+
import assert from 'assert'
8+
9+
describe('tryFunctions', function () {
10+
it('should return the result of the first function that returns', async function () {
11+
const f1 = () => Promise.reject('f1')
12+
const f2 = () => Promise.resolve('f2')
13+
const f3 = () => Promise.reject('f3')
14+
15+
assert.strictEqual(await tryFunctions([f1, f2, f3]), 'f2')
16+
})
17+
18+
it('if all reject, then should throw final error', async function () {
19+
const f1 = () => Promise.reject('f1')
20+
const f2 = () => Promise.reject('f2')
21+
const f3 = () => Promise.reject('f3')
22+
23+
await assert.rejects(
24+
async () => await tryFunctions([f1, f2, f3]),
25+
(e) => e === 'f3'
26+
)
27+
})
28+
})

0 commit comments

Comments
 (0)