Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move tryFunctions to core and include tests
Browse files Browse the repository at this point in the history
Hweinstock committed Jan 24, 2025
1 parent 7d05c68 commit c7b97e2
Showing 5 changed files with 49 additions and 19 deletions.
17 changes: 0 additions & 17 deletions packages/core/src/amazonq/lsp/util.ts
Original file line number Diff line number Diff line change
@@ -26,20 +26,3 @@ export async function lspSetupStage<T>(
return result
})
}

/**
* Try Functions in the order presented and return the first returned result. If none return, throw the final error.
* @param functions non-empty list of functions to try.
* @returns
*/
export async function tryFunctions<Result>(functions: (() => Promise<Result>)[]): Promise<Result> {
let currentError: Error = new Error('No functions provided')
for (const func of functions) {
try {
return await func()
} catch (e) {
currentError = e as Error
}
}
throw currentError
}
3 changes: 2 additions & 1 deletion packages/core/src/shared/lsp/lspResolver.ts
Original file line number Diff line number Diff line change
@@ -13,7 +13,8 @@ import { TargetContent, logger, LspResult, LspVersion, Manifest } from './types'
import { getApplicationSupportFolder } from '../vscode/env'
import { createHash } from '../crypto'
import request from '../request'
import { lspSetupStage, tryFunctions } from '../../amazonq/lsp/util'
import { lspSetupStage } from '../../amazonq/lsp/util'
import { tryFunctions } from '../utilities/tsUtils'

export class LanguageServerResolver {
constructor(
3 changes: 2 additions & 1 deletion packages/core/src/shared/lsp/manifestResolver.ts
Original file line number Diff line number Diff line change
@@ -9,7 +9,8 @@ import { RetryableResourceFetcher } from '../resourcefetcher/httpResourceFetcher
import { Timeout } from '../utilities/timeoutUtils'
import globals from '../extensionGlobals'
import { Manifest } from './types'
import { lspSetupStage, tryFunctions } from '../../amazonq/lsp/util'
import { lspSetupStage } from '../../amazonq/lsp/util'
import { tryFunctions } from '../utilities/tsUtils'

const logger = getLogger('lsp')

17 changes: 17 additions & 0 deletions packages/core/src/shared/utilities/tsUtils.ts
Original file line number Diff line number Diff line change
@@ -94,6 +94,23 @@ export function createFactoryFunction<T extends new (...args: any[]) => any>(cto
return (...args) => new ctor(...args)
}

/**
* Try Functions in the order presented and return the first returned result. If none return, throw the final error.
* @param functions non-empty list of functions to try.
* @returns
*/
export async function tryFunctions<Result>(functions: (() => Promise<Result>)[]): Promise<Result> {
let currentError: Error = new Error('No functions provided')
for (const func of functions) {
try {
return await func()
} catch (e) {
currentError = e as Error
}
}
throw currentError
}

type NoSymbols<T> = { [Property in keyof T]: Property extends symbol ? never : Property }[keyof T]
export type InterfaceNoSymbol<T> = Pick<T, NoSymbols<T>>
/**
28 changes: 28 additions & 0 deletions packages/core/src/test/shared/utilities/tsUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import { tryFunctions } from '../../../shared/utilities/tsUtils'
import assert from 'assert'

describe('tryFunctions', function () {
it('should return the result of the first function that returns', async function () {
const f1 = () => Promise.reject('f1')
const f2 = () => Promise.resolve('f2')
const f3 = () => Promise.reject('f3')

assert.strictEqual(await tryFunctions([f1, f2, f3]), 'f2')
})

it('if all reject, then should throw final error', async function () {
const f1 = () => Promise.reject('f1')
const f2 = () => Promise.reject('f2')
const f3 = () => Promise.reject('f3')

await assert.rejects(
async () => await tryFunctions([f1, f2, f3]),
(e) => e === 'f3'
)
})
})

0 comments on commit c7b97e2

Please sign in to comment.