diff --git a/lib/modules/datasource/rubygems/common.ts b/lib/modules/datasource/rubygems/common.ts index 28a434f3d56c05c1e0836e4d6463899edb89139d..5dd38e90df81d5c7ee86515464a1816fb18280fb 100644 --- a/lib/modules/datasource/rubygems/common.ts +++ b/lib/modules/datasource/rubygems/common.ts @@ -39,6 +39,6 @@ export function getV1Releases( return http.getJsonSafe(versionsUrl, GemVersions).transform((releaseResult) => getV1Metadata(http, registryUrl, packageName) .transform((metadata) => assignMetadata(releaseResult, metadata)) - .unwrap(releaseResult) + .unwrapOrElse(releaseResult) ); } diff --git a/lib/modules/manager/poetry/extract.ts b/lib/modules/manager/poetry/extract.ts index b36d1a550ee2d4f04dbeda8017a68cf6d68ef1cd..b86135590e6b29813c87c632ff87519dc61f62c8 100644 --- a/lib/modules/manager/poetry/extract.ts +++ b/lib/modules/manager/poetry/extract.ts @@ -183,7 +183,7 @@ export async function extractPackageFile( const lockfileMapping = Result.parse( Lockfile.transform(({ lock }) => lock), lockContents - ).unwrap({}); + ).unwrapOrElse({}); const deps = [ ...extractFromDependenciesSection( diff --git a/lib/modules/manager/poetry/update-locked.ts b/lib/modules/manager/poetry/update-locked.ts index 775a08c02a225f66760d61fb879d24c8e55053e1..7ed61b03700c838c80384e006a6fc5d0243b74e7 100644 --- a/lib/modules/manager/poetry/update-locked.ts +++ b/lib/modules/manager/poetry/update-locked.ts @@ -20,5 +20,5 @@ export function updateLockedDependency( ? { status: 'already-updated' } : { status: 'unsupported' } ) - .unwrap({ status: 'unsupported' }); + .unwrapOrElse({ status: 'unsupported' }); } diff --git a/lib/util/result.spec.ts b/lib/util/result.spec.ts index 5d108ff1190a6e634de9d6f21104b4b2063f8ead..c55d6b0619ece886ccc8556539d79c2d63fb0f8d 100644 --- a/lib/util/result.spec.ts +++ b/lib/util/result.spec.ts @@ -120,15 +120,26 @@ describe('util/result', () => { it('skips fallback for successful value', () => { const res: Result<number> = Result.ok(42); - expect(res.unwrap(-1)).toBe(42); + expect(res.unwrapOrElse(-1)).toBe(42); }); it('uses fallback for error value', () => { const res: Result<number, string> = Result.err('oops'); - expect(res.unwrap(42)).toBe(42); + expect(res.unwrapOrElse(42)).toBe(42); }); - it('throws error uncaught in the failed transform', () => { + it('unwrapOrElse throws uncaught transform error', () => { + const res = Result.ok(42); + expect(() => + res + .transform(() => { + throw 'oops'; + }) + .unwrapOrElse(0) + ).toThrow('oops'); + }); + + it('unwrap throws uncaught transform error', () => { const res = Result.ok(42); expect(() => res @@ -345,12 +356,12 @@ describe('util/result', () => { it('skips fallback for successful AsyncResult', async () => { const res = Result.wrap(Promise.resolve(42)); - await expect(res.unwrap(0)).resolves.toBe(42); + await expect(res.unwrapOrElse(0)).resolves.toBe(42); }); it('uses fallback for error AsyncResult', async () => { const res = Result.wrap(Promise.reject('oops')); - await expect(res.unwrap(42)).resolves.toBe(42); + await expect(res.unwrapOrElse(42)).resolves.toBe(42); }); it('returns ok-value for unwrapOrThrow', async () => { diff --git a/lib/util/result.ts b/lib/util/result.ts index 59132bb35779ad71d63742c1006623861f357a60..aa71a560ab2b70f563b605f297e108ec60d153b4 100644 --- a/lib/util/result.ts +++ b/lib/util/result.ts @@ -306,38 +306,49 @@ export class Result<T extends Val, E extends Val = Error> { /** * Returns a discriminated union for type-safe consumption of the result. - * When `fallback` is provided, the error is discarded and value is returned directly. * When error was uncaught during transformation, it's being re-thrown here. * * ```ts * - * // DESTRUCTURING * const { val, err } = Result.ok('foo').unwrap(); * expect(val).toBe('foo'); * expect(err).toBeUndefined(); * - * // FALLBACK - * const value = Result.err('bar').unwrap('foo'); - * expect(val).toBe('foo'); - * * ``` */ - unwrap(): Res<T, E>; - unwrap(fallback: T): T; - unwrap(fallback?: T): Res<T, E> | T { + unwrap(): Res<T, E> { if (this.res.ok) { - return fallback === undefined ? this.res : this.res.val; + return this.res; } - if (fallback !== undefined) { - return fallback; + if (this.res._uncaught) { + throw this.res.err; + } + + return this.res; + } + + /** + * Returns a success value or a fallback value. + * When error was uncaught during transformation, it's being re-thrown here. + * + * ```ts + * + * const value = Result.err('bar').unwrapOrElse('foo'); + * expect(val).toBe('foo'); + * + * ``` + */ + unwrapOrElse(fallback: T): T { + if (this.res.ok) { + return this.res.val; } if (this.res._uncaught) { throw this.res.err; } - return this.res; + return fallback; } /** @@ -625,28 +636,32 @@ export class AsyncResult<T extends Val, E extends Val> /** * Returns a discriminated union for type-safe consumption of the result. - * When `fallback` is provided, the error is discarded and value is returned directly. * * ```ts * - * // DESTRUCTURING * const { val, err } = await Result.wrap(readFile('foo.txt')).unwrap(); * expect(val).toBe('foo'); * expect(err).toBeUndefined(); * - * // FALLBACK - * const val = await Result.wrap(readFile('foo.txt')).unwrap('bar'); + * ``` + */ + unwrap(): Promise<Res<T, E>> { + return this.asyncResult.then<Res<T, E>>((res) => res.unwrap()); + } + + /** + * Returns a success value or a fallback value. + * + * ```ts + * + * const val = await Result.wrap(readFile('foo.txt')).unwrapOrElse('bar'); * expect(val).toBe('bar'); * expect(err).toBeUndefined(); * * ``` */ - unwrap(): Promise<Res<T, E>>; - unwrap(fallback: T): Promise<T>; - unwrap(fallback?: T): Promise<Res<T, E>> | Promise<T> { - return fallback === undefined - ? this.asyncResult.then<Res<T, E>>((res) => res.unwrap()) - : this.asyncResult.then<T>((res) => res.unwrap(fallback)); + unwrapOrElse(fallback: T): Promise<T> { + return this.asyncResult.then<T>((res) => res.unwrapOrElse(fallback)); } /**