Last active
September 16, 2019 06:10
-
-
Save FongX777/af5e19e024a34f14be0ac287bcc34b7b to your computer and use it in GitHub Desktop.
Error Handling in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @returns {Left|Right} | |
*/ | |
function sumPositiveNums(nums) { | |
if (nums.some(num => !Number.isInteger(num))) { | |
return Left.of({ code: 501, msg: 'All numbers should be integer' }); | |
} | |
if (nums.some(num => num <= 0)) { | |
return Left.of({ code: 502, msg: 'All numbers should be positive'}); | |
} | |
return Right.of(nums.reduce((sum, num) => sum + num, 0)); | |
} | |
const result = sumPositiveNums([1,2,3]); | |
if (result.isLeft()) { | |
const { code, msg } = result.value; | |
switch(code) { | |
case 501: | |
// handle error | |
case 502: | |
// handle error | |
} | |
} else { | |
console.log(result.value); | |
} | |
sumPositiveNums([1,2]).map(v => v * 2).map(v => v * -1); // val = -6 | |
sumPositiveNums([-1,2]).map(v => v * 2); // val = {code: 502, msg: "All numbers should be positive"} | |
class Left { | |
constructor(x) { | |
this.value = x; | |
} | |
static of (x) { | |
return new Left(x); | |
} | |
map(f) { | |
return this; | |
} | |
isLeft() { | |
return true; | |
} | |
isRight() { | |
return false; | |
} | |
} | |
class Right { | |
constructor(x) { | |
this.value = x; | |
} | |
static of (x) { | |
return new Right(x); | |
} | |
map(f) { | |
return Right.of(f(this.value)); | |
} | |
isLeft() { | |
return false; | |
} | |
isRight() { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Left<L, A> { | |
readonly value: L; | |
constructor(value: L) { | |
this.value = value; | |
} | |
isLeft(): this is Left<L, A> { | |
return true; | |
} | |
isRight(): this is Right<L, A> { | |
return false; | |
} | |
} | |
export const left = <L, A>(l: L): Either<L, A> => { | |
return new Left(l); | |
}; | |
export class Right<L, A> { | |
readonly value: A; | |
constructor(value: A) { | |
this.value = value; | |
} | |
isLeft(): this is Left<L, A> { | |
return false; | |
} | |
isRight(): this is Right<L, A> { | |
return true; | |
} | |
} | |
export const right = <L, A>(a: A): Either<L, A> => { | |
return new Right<L, A>(a); | |
}; | |
export type Either<L, A> = Left<L, A> | Right<L, A>; | |
interface User { | |
id: string; | |
name: string; | |
email: string; | |
} | |
interface NameError { | |
name: string; | |
message: string; | |
} | |
interface EmailError { | |
email: string; | |
message: string; | |
} | |
export const createUser = (params: { | |
name: string; | |
email: string; | |
}): Either<NameError | EmailError, User> => { | |
if (params.name.length < 5) { | |
return left({ name: params.name, message: 'name length should > 5' }); | |
} | |
if (!params.email.includes('@')) { | |
return left({ email: params.email, message: 'email not valid' }); | |
} | |
const user: User = { id: '1', ...params }; | |
return right(user); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @returns {[string|undefined, number|undefined]} | |
*/ | |
function sumPositiveNums(nums) { | |
if (nums.some(num => !Number.isInteger(num))) { | |
return ['All numbers should be integer']; | |
} | |
if (nums.some(num => num <= 0)) { | |
return ['All numbers should be positive']; | |
} | |
return [undefined, nums.reduce((sum, num) => sum + num, 0)]; | |
} | |
const [err, sum] = sumPositiveNums([1,2,3]); | |
if (err) { | |
switch(err) { | |
case 'All numbers should be integer': | |
// handle error | |
case 'All numbers should be positive': | |
// handle error | |
} | |
} | |
console.log(sum); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @returns {number|null} | |
*/ | |
function sumPositiveInts(nums) { | |
if (nums.some(num => !Number.isInteger(num))) { | |
return null | |
} | |
if (nums.some(num => num <= 0)) { | |
return null; | |
} | |
return nums.reduce((sum, num) => sum + num, 0); | |
} | |
const sum = sumPositiveNums([1,2,3]); | |
if (sum === null) { | |
// handle error (which one?) | |
} else { | |
// success | |
console.log(sum); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @returns {number} | |
*/ | |
function sumPositiveNums(nums) { | |
if (nums.some(num => !Number.isInteger(num))) { | |
throw new Error('All numbers should be integer'); | |
} | |
if (nums.some(num => num <= 0)) { | |
throw new Error('All numbers should be positive') | |
} | |
return nums.reduce((sum, num) => sum + num, 0); | |
} | |
try { | |
const sum = sumPositiveNums([1,2,3]); | |
console.log(sum); | |
} catch(error) { | |
switch(error.message) { | |
// very fragile | |
case 'All numbers should be integer': | |
// handle error | |
case 'All numbers should be positive': | |
// handle error | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment