저 역시 FP 형태는 아직 미숙하여 FP 형태의 code 들이 다소 이상할 수 있습니다. 피드백 주시면 반영하겠습니다. 😀
const main = (userId) => {
try{
const user = getUser(userId)
const parsedUser = parseUser(user)
const transformUser = transformUser(parseUser)
}catch(error){
console.log(error.message)
}
}
import * as Either from 'fp-ts/Either';
import { pipe } from 'fp-ts/lib/function';
type UserType = {
userId: Number;
name: String;
};
type ParseUserType = UserType & {
age: Number;
};
const getUser = (userId: number) => {
try {
const user = { userId, name: 'name' };
return Either.right(user);
} catch (error) {
return Either.left(error);
}
};
const parseUser = (user: UserType) => {
try {
const parseUser = { userId: user.userId, name: user.name, age: 20 };
return Either.right(parseUser);
} catch (error) {
return Either.left(error);
}
};
const transformUser = (parsedUser: ParseUserType) => {
try {
const transformedUser = { user: 'transformed', userId: parsedUser.userId };
return Either.right(transformedUser);
} catch (error: any) {
return Either.left(error);
}
};
export const main = (userId: number) => {
return pipe(
userId,
getUser,
Either.chain(parseUser),
Either.chain(transformUser),
Either.getOrElseW(() => 'error occur')
);
};
// error handling 전
export const someFunc = (user: { user: string; userId: Number }) => {
return Either.right(user);
};
// error handling 추가 후
export const someFunc = (user: { user: string; userId: Number }) => {
if (user.userId == 0) {
return Either.left(new Error('not proper user id'));
}
return Either.right(user);
};
describe('example about monad', () => {
it('case 1, add error handling', () => {
pipe(
1234,
getUser,
Either.chain(parseUser),
Either.chain(transformUser),
Either.chain(someFunc),
Either.getOrElseW(() => 'error occur'),
console.log
);
});
});
export const parseUser = (user: UserType) => {
try {
if (user.userId == 1) {
return E.left(new Error('blocked user id'));
}
const parseUser = { userId: user.userId, name: user.name, age: 20 };
return E.right(parseUser);
} catch (error: any) {
return E.left(error);
}
};
describe('example about monad', () => {
it('case 2.1, after parsedUser, send alarm, terminate logic', () => {
pipe(
1, // error 발생 하기 위한 block user Id
getUser,
Either.chain(parseUser),
Either.match(
(error: any) => {
console.log(`blocked user signed, ${error}`);
return Either.left(error);
},
(data) => {
return Either.right(data);
}
),
Either.chain(transformUser),
Either.getOrElseW((error) => `error occur ${error}`),
console.log
);
});
});
console.log
blocked user signed, Error: blocked user id
console.log
error occur Error: blocked user id
describe('example about monad', () => {
it('case 2.1, after parsedUser, send alarm, terminate logic', () => {
pipe(
1, // error 발생 하기 위한 block user Id
getUser,
Either.chain(parseUser),
Either.match(
(error: any) => {
console.log(`blocked user signed, ${error}`);
return Either.right({ userId: 1, name: 'default', age: 20 }); // left 가 아니라 정상 처리를 위한 right 를 넣었다.
},
(data) => {
return Either.right(data);
}
),
Either.chain(transformUser),
Either.getOrElseW((error) => `error occur ${error}`),
console.log
);
});
});
console.log
blocked user signed, Error: blocked user id
console.log
{ user: 'transformed', userId: 1 }
const res = someFunction()
if(typeof res == error){
return ...
}
return ...