Top Banner
DON’T USE TRY-CATCH Tetsuharu OHZEKI aka @saneyuki_s April 5, 2016. 東京node学園 #20
23

Don’t use try catch in JavaScript

Apr 14, 2017

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Don’t use try catch in JavaScript

DON’T USE TRY-CATCH

Tetsuharu OHZEKI aka @saneyuki_s

April 5, 2016. 東京node学園 #20

Page 2: Don’t use try catch in JavaScript

ABOUT ME

• Tetsuharu OHZEKI

• twitter: saneyuki_s

• Open Source Contributor

• Mozilla Committer

• Firefox, Servo (as reviewer)

• ReactiveX/RxJS

• And some others…

• whatwg html, etc

• Working at VOYAGE GROUP, inc.

• My main work is to develop an advertising management system with using

TypeScript, babel compiler, React, and RxJS.

Page 3: Don’t use try catch in JavaScript

DO YOU USE TRY-CATCH?

Page 4: Don’t use try catch in JavaScript

DO YOU THROW AN ERROR?

Page 5: Don’t use try catch in JavaScript

TWO TYPES OF “ERROR”

•Just a Bug• NullPointerException, TypeError, calling undefined methods, not found a

required file, etc...

•Recoverable Errors• Network timeout, JSON parse errors, etc…

Page 6: Don’t use try catch in JavaScript

CAN YOU RECOVER A BUGIN A RUNNING APPLICATION?

No• A bug is just a programming mistake.

• Don’t recover them.

• So “let it crash”, “abandonment”, “fail-fast” immediately.

• Even if you success recovering from their fatal,there’re some serious conformity problem in your heap.

Page 7: Don’t use try catch in JavaScript

Promise/Observable .catch() are any type in TypeScript definitions. This is a hole for type

restriction.

interface Promise<T> {

catch(onrejected?: (reason: any) => T | PromiseLike<T>):

Promise<T>;

catch(onrejected?: (reason: any) => void): Promise<T>;

}

interface Observable<T> {

catch<R>(selector: (err: any, caught: Observable<T>) =>

Observable<R>): Observable<R>;

}

Page 8: Don’t use try catch in JavaScript

CAN WE USE TRY-CATCH FOR RECOVERABLE?

• I don’t think so excluding some exceptions…

• (of course, we have to handle errors from builtin

object, dom, libraries, ete with try-catch)

• “error throw-ablity” should be restricted by a

function signature.

• should force an error handling for a caller!

Page 9: Don’t use try catch in JavaScript

function parseJSON(obj) {

try {

const value = JSON.parse(obj);

return value;

}

catch (e) {

return null;

}

}

Page 10: Don’t use try catch in JavaScript

HOW DO WE REPRESENT“ERROR THROWABLE” TYPE?

Page 11: Don’t use try catch in JavaScript

RETURN A “RESULT TYPE” THAT IS LIKE A ITERATOR’S RESULT.

interface ErrorContainableResult<T, E> {

ok: boolean;

value: T;

error: E;

}

Page 12: Don’t use try catch in JavaScript

const { ok, value, error } = doSome();

if (ok) {

// handle value

}

else {

// handle error

}

Page 13: Don’t use try catch in JavaScript

doSomePromise().then((result) => {

const { ok, value, error } = result;

if (ok) {

// handle value

}

else {

// handle error

}

}, (unrecover) => crash(unrecover)).;

Page 14: Don’t use try catch in JavaScript

doSomeRxObservable().subscribe((result) => {

const { ok, value, error, } = result;

// blah, blah, blah

}, (unrecover) => crash(unrecover));

Page 15: Don’t use try catch in JavaScript

process.on(‘uncaughtException’, (reason) => {

// dump error report

process.exit();

});

throw new Error(‘Hello! This is fatal bug’);

Page 16: Don’t use try catch in JavaScript

INTRODUCE RESULT<T, E>

Page 17: Don’t use try catch in JavaScript

RESULT<T, E>

• Result<T, E> == Either<A, B>

• You can use it from npm:option-t• APIs are inspired by Rustlang’s std::result

• There are some instance methods

• This provides “utility” object & methods.

• You can same things with destructuring assignment.

Page 18: Don’t use try catch in JavaScript

const result = getSomeResult();

if ( result.isOk() ) {

// success

}

else {

// error

}

Page 19: Don’t use try catch in JavaScript

const result = getSomeResult();

const value: string = result

.map((v: number) => String(v)).unwrap();

const first = getSomeResult();

const second = first.andThen(first => {

return getSecondResult(first);

});

Page 20: Don’t use try catch in JavaScript

type Result<T, E> = Ok<T, E> | Err<T, E>;

class Ok<T, E> implements ResultMethods<T, E>;

class Err<T, E> implements ResultMethods<T, E>;

interface ResultMethods<T, E> {

ok(): Option<T>;

err(): Option<E>;

isOk(): boolean;

isErr(): boolean;

map<U>(op: MapFn<T, U>): Result<U, E>;

mapErr<F>(op: MapFn<E, F>): Result<T, F>;

andThen<U>(op: FlatmapOkFn<T, U, E>): Result<U, E>;

orElse<F>(op: FlatmapErrFn<T, E, F>): Result<T, F>;

unwrap(): T;

unwrapErr(): E;

}

Page 21: Don’t use try catch in JavaScript

SEEHTTPS://DOC.RUST-LANG.ORG/BOOK/ERROR-

HANDLING.HTML

Page 22: Don’t use try catch in JavaScript

DRAWBACKS IN JAVASCRIPT

• Need a extra object allocation cost

• In the future, JavaScript runtime may make the cost more cheap.

• Don’t use them if you require computation performance

extremely.

• Result<T, E> type needs static type analytics for more

convenient/secure programming (but plain JavaScript is not so).

• Let’s use with TypeScript or Flowtype

Page 23: Don’t use try catch in JavaScript

•DO NOT TRY TO RECOVER A BUG

• Don’t suppress a bug. FIX it.

•Don’t use try-catch

• Excluding: Runtime provided functions (include JSON.parse, DOM),ssand 3rd

party libraries.

•Tell “Error-Throwable” via function signature.