Handling Parsing Errors in TypeScript Code

hi there, i need some help, i don't really understand this part.

this is my code,

const RegisterPayloadSchema = Schema.Struct({
    username: Schema.String,
    email: Schema.String,
    password: Schema.String,
});

export type RegisterPayload = Schema.Schema.Type<typeof RegisterPayloadSchema>;

export const parseRegisterPayload = Schema.decodeUnknown(RegisterPayloadSchema);

const register = (payload: RegisterPayload) => {
    const validated_payload = parseRegisterPayload(payload).pipe(
        Effect.tap((res) => Effect.log("validated payload :", res)),
    )

    const hashed_password = Effect.runPromise(
        pipe(
            validated_payload.password,
            hash_password,
            Effect.tap((res) => Effect.log(`hashed_password : ${res}`)),
            Effect.catchAll(() => {
                new Response("hashing error", { status: 500 })
                return Effect.fail("Send http response with hashing error")
            })
        )
    )

    //TODO: implement register
};

how do i can handle the error when parsing the payload?

i've try using this but still got an error.

    const validated_payload = parseRegisterPayload(payload).pipe(
        Effect.tap((res) => Effect.log("validated payload :", res)),
        Effect.catchAll(
            () => new Response("invalid payload", { status: 400 }) // the error in here..
        ),
    )

   //the validated payload is Effect<unknown, unknown, unknown>
Was this page helpful?