Skip to content

Commit

Permalink
refactor: update mapError to take in Err and spit out Err
Browse files Browse the repository at this point in the history
  • Loading branch information
braden-w committed Dec 24, 2024
1 parent fa19c4c commit 35dbcdd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-ducks-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@epicenterhq/result": patch
---

Update mapError to take in Err and spit out Err
8 changes: 4 additions & 4 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export function trySync<T, E>({
mapErr,
}: {
try: () => T extends Promise<unknown> ? never : T;
mapErr: (error: unknown) => E;
mapErr: (error: Err<unknown>) => Err<E>;
}): Result<T, E> {
try {
const data = operation();
return Ok(data);
} catch (error) {
return Err(mapErr(error));
return mapErr(Err(error));
}
}

Expand All @@ -43,12 +43,12 @@ export async function tryAsync<T, E>({
mapErr,
}: {
try: () => Promise<T>;
mapErr: (error: unknown) => E;
mapErr: (error: Err<unknown>) => Err<E>;
}): Promise<Result<T, E>> {
try {
const data = await operation();
return Ok(data);
} catch (error) {
return Err(mapErr(error));
return mapErr(Err(error));
}
}
4 changes: 2 additions & 2 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type ServiceErrorFns<ServiceErrorProperties> = {
Err: (props: ServiceErrorProperties) => Err<ServiceErrorProperties>;
trySync: <T>(opts: {
try: () => T extends Promise<unknown> ? never : T;
mapErr: (error: unknown) => ServiceErrorProperties;
mapErr: (error: Err<unknown>) => Err<ServiceErrorProperties>;
}) => Result<T, ServiceErrorProperties>;
tryAsync: <T>(opts: {
try: () => Promise<T>;
mapErr: (error: unknown) => ServiceErrorProperties;
mapErr: (error: Err<unknown>) => Err<ServiceErrorProperties>;
}) => Promise<Result<T, ServiceErrorProperties>>;
};

Expand Down

0 comments on commit 35dbcdd

Please sign in to comment.