Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include original input when mapping errors #168

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/build-npm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ex. scripts/build_npm.ts
import { build, emptyDir } from 'https://deno.land/x/dnt@0.40.0/mod.ts'
import pkg from '../deno.json' assert { type: 'json' }
import pkg from '../deno.json' with { type: 'json' }

await emptyDir('./npm')

Expand Down
13 changes: 8 additions & 5 deletions src/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,30 @@ function catchFailure<
}

/**
* Creates a new function that will apply a transformation over the list of Errors of a Failure from a given function. When the given function succeeds, its result is returned without changes.
* Creates a new function that will apply a transformation over the list of Errors of a Failure from a given function. When the given function succeeds, its result is returned without changes. The mapper receives the original input.
*
* @example
*
* ```ts
* import { mapErrors } from 'composable-functions'
*
* const increment = ({ id }: { id: number }) => id + 1
* const incrementWithErrorSummary = mapErrors(increment, (result) => ({
* errors: [{ message: 'Errors count: ' + result.errors.length }],
* const incrementWithErrorSummary = mapErrors(increment, (result, {id}) => ({
* errors: [{ message: 'Errors count: ' + result.errors.length + ' for id: ' + String(id) }],
* }))
* ```
*/
function mapErrors<Fn extends Function>(
fn: Fn,
mapper: (err: Error[]) => Error[] | Promise<Error[]>,
mapper: (
err: Error[],
...originalInput: Parameters<Extract<Fn, Internal.AnyFn>>
) => Error[] | Promise<Error[]>,
): Fn extends Internal.AnyFn ? Composable<Fn> : never {
const callable = (async (...args) => {
const res = await composable(fn)(...args)
if (res.success) return success(res.data)
const mapped = await composable(mapper)(res.errors)
const mapped = await composable(mapper)(res.errors, ...(args as never))
if (mapped.success) {
return failure(mapped.data)
} else {
Expand Down
15 changes: 15 additions & 0 deletions src/tests/map-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ describe('mapErrors', () => {
assertEquals(res.errors[0].message, 'a is 1!!!')
})

it('maps using the original input', async () => {
const fn = mapErrors(faultyAdd, (_, a, b) => {
throw new Error('result would be ' + (a + b))
})
const res = await fn(1, 2)

type _FN = Expect<
Equal<typeof fn, Composable<(a: number, b: number) => number>>
>
type _R = Expect<Equal<typeof res, Result<number>>>

assertEquals(res.success, false)
assertEquals(res.errors[0].message, 'result would be 3')
})

it('accepts an async mapper', async () => {
const fn = mapErrors(
faultyAdd,
Expand Down