Skip to content

Commit 39738c3

Browse files
committed
chore: Fix error handling in example
1 parent 1046bfb commit 39738c3

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

example/backend/src/lib/error-handler.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ export interface ErrorResponse {
1010
};
1111
}
1212

13-
export const SerializeError = Effect.catchAll<
14-
unknown,
15-
ErrorResponse,
16-
never,
17-
never
18-
>((error) => {
13+
const formatError = (error: unknown) => {
1914
if (error instanceof ParseError) {
2015
const failures = ParseResult.ArrayFormatter.formatErrorSync(error).map(
2116
(failure) => ({ path: failure.path.join('.'), message: failure.message })
@@ -32,4 +27,15 @@ export const SerializeError = Effect.catchAll<
3227
return Effect.succeed({
3328
error: { message: 'Internal error', tag: 'UnknownError' },
3429
});
35-
});
30+
};
31+
32+
export const SerializeError = Effect.catchAll<
33+
unknown,
34+
ErrorResponse,
35+
never,
36+
never
37+
>((error) => formatError(error));
38+
39+
export const ErrorHandler = Effect.catchAllDefect((error) =>
40+
formatError(error)
41+
);

example/backend/src/lib/on-call.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { onCallEffect } from '@effect-firebase/admin';
22
import { Effect, Option } from 'effect';
33
import { runtime } from './runtime.js';
44
import { OnExampleCall, PostRepository } from '@example/shared';
5+
import { SerializeError, ErrorHandler } from './error-handler.js';
56

67
/**
78
* Example using onCallEffect with schemas.
@@ -20,5 +21,5 @@ export const onExampleCall = onCallEffect(
2021
const posts = yield* PostRepository;
2122
const post = yield* posts.getById(input.id);
2223
return Option.getOrThrow(post);
23-
})
24+
}).pipe(SerializeError, ErrorHandler)
2425
);

example/backend/src/lib/on-request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Effect, Option } from 'effect';
22
import { onRequestEffect } from '@effect-firebase/admin';
33
import { OnExampleRequest, PostId, PostRepository } from '@example/shared';
44
import { runtime } from './runtime.js';
5+
import { SerializeError, ErrorHandler } from './error-handler.js';
56

67
/**
78
* Example using onRequestEffect with schemas.
@@ -21,5 +22,5 @@ export const onExampleRequest = onRequestEffect(
2122
const id = PostId.make(body.id);
2223
const post = yield* posts.getById(id);
2324
return Option.getOrThrow(post);
24-
})
25+
}).pipe(SerializeError, ErrorHandler)
2526
);

0 commit comments

Comments
 (0)