I've recently started delving into function programming and encountered some unexpected behavior. I'm facing an issue where a function containing a tryCatch isn't being called within a pipe function for fp-ts. Despite reaching the next line past the function with breakpoints, it seems like the tryCatch function or any subsequent chain or fold functions are not executed.
Moreover, when I isolate this function and log it to the console using validateRegistrationForm(user), instead of getting a return value, it displays a function. From what I've gathered online, it seems that tryCatch should return a TaskEither and not a function.
Could someone point out any syntax errors causing this problem and suggest how to resolve it?
Here is an excerpt of the function in the pipe where the tryCatch appears to be skipped:
const validateRegistrationForm = (user: User): TaskEither<Error, User> => tryCatch(
async () => {
const schema = Joi.object().keys({
userName: Joi.string().min(4).max(255).required(),
firstName: Joi.string().pattern(new RegExp(/^[a-z\d\-_\s]+$/i)).min(1).max(50).required(),
lastName: Joi.string().pattern(new RegExp(/^[a-z\d\-_\s]+$/i)).min(1).max(50).required(),
emailAddress: Joi.string().email().required(),
password: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
confirmPassword: Joi.alternatives().try(Joi.string(), Joi.number()).disallow(Joi.ref('password')).required()
});
let {error} = await schema.validateAsync(user);
if (error && error.length > 0)
throw new ValidationError(error.message);
return user;
},
(reason: any) => reason instanceof Error ? reason : new Error(reason.message)
);
Below is the function consisting of the pipe and chain:
const createUser = (user: User): TaskEither<Error, User> => {
return pipe(
user,
validateRegistrationForm,
chain((validContent: User) => {
const user: User = {
confirmPassword: validContent.confirmPassword,
firstName: validContent.firstName,
lastName: validContent.lastName,
emailAddress: validContent.emailAddress,
password: validContent.password,
userName: validContent.userName
};
return repository.saveUser(user);
})
);
};
Lastly, here's the code snippet calling the function with the pipe:
postUser: (req, res) => {
pipe(
req.body,
interactor.createUser,
fold(
(error) => async () => {
console.error(error);
res.status(500).send({ error: 'Failed to retrieve user' });
},
(user) => async () => {
if (user) {
res.status(200).send(user);
} else {
res.status(404).send({ error: 'User not found' });
}
}
)
);
}