Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Pitfalls of Cancellation
Search
Masatoshi Kubode
December 22, 2020
Programming
1.9k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
The Pitfalls of Cancellation
Masatoshi Kubode
December 22, 2020
More Decks by Masatoshi Kubode
See All by Masatoshi Kubode
3リポジトリーを2ヶ月でモノレポ化した話 / How I turned 3 repositories into a monorepo in 2 months
kubode
0
220
ウォンテッドリーの「ココロオドル」モバイル開発 / Wantedly's "kokoro odoru" mobile development
kubode
2
1.3k
Firebase Dynamic Linksの代替手段を自作する / Create your own Firebase Dynamic Links alternative
kubode
0
720
技術を根付かせる / How to make technology take root
kubode
1
500
WantedlyでのKotlin Multiplatformの導入と課題 / Kotlin Multiplatform Implementation and Challenges at Wantedly
kubode
0
640
Google Play Consoleデベロッパー アカウントの確認 / Verifying your Play Console developer account
kubode
1
1.4k
Make your Android app into Multiplatform app
kubode
0
230
ウォンテッドリーにおけるモバイルアプリ開発 / iOSDC Japan 2024 Sponsor Session
kubode
1
1.5k
Jetpack ComposeのBottomSheetとの戦い / Fight with BottomSheet of Jetpack Compose
kubode
0
1.1k
Other Decks in Programming
See All in Programming
Oxcを導入して開発体験が向上した話
yug1224
4
310
OSもどきOS
arkw
0
560
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
6.6k
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
780
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
4
910
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
350
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
540
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
dRuby over BLE
makicamel
2
340
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
540
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Practical Orchestrator
shlominoach
191
11k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
580
Fireside Chat
paigeccino
42
3.9k
Balancing Empowerment & Direction
lara
6
1.2k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
170
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
Documentation Writing (for coders)
carmenintech
77
5.4k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
290
Side Projects
sachag
455
43k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Transcript
©2020 Wantedly, Inc. The Pitfalls of Cancellation Presentation Subtitle 2020/12/22
Masatoshi Kubode https://unsplash.com/photos/J8sh37XZ8ww
©2020 Wantedly, Inc. ٱอग़խढ़ 8BOUFEMZ 8BOUFEMZ7JTJUͷ"OESPJEΞϓϦΛ։ൃ͍ͯ͠·͢ɻ ,PUMJO .BHJDUIF(BUIFSJOH UIJTUP4USJOH 5XJUUFS
!TXJ[@BSE (JU)VC !LVCPEF
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc. $PSPVUJOFTͰى͖ͨΫϥογϡͷ
©2020 Wantedly, Inc. ͋ΔͷΫϥογϡϨϙʔτ
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc.
©2020 Wantedly, Inc. ֘ίʔυ class FetchDiscoverPostsUseCaseImpl( private val discoverRepository: DiscoverRepository,
) : FetchDiscoverPostsUseCase { override suspend operator fun invoke(sectionId: DiscoverSectionId) { try { return discoverRepository.fetchDiscoverPosts(sectionId) } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { throw FetchDiscoverProjectsUseCase.Error( "Failed to fetch discover posts for section: $sectionId”, e ) } } } ←ίϐϖͰTypoͯ͠Δ Repositoryͷྫ֎Λϥοϓͯ͠ Rethrowͯ͠Δ͚ͩͳͷʹ ͳͥ?
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc. 4UBDL5SBDFΛΑ͘ݟΔ
©2020 Wantedly, Inc. ͋ΔͷΫϥογϡϨϙʔτ JobCancellationException͕ݪҼ
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc. +PC$BODFMMBUJPO&YDFQUJPOͱ
©2020 Wantedly, Inc. JobCancellationException https://atoulme.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job-cancellation-exception/index.html Job.cancel()͞Εͨͱ͖ʹ suspend͍ͯ͠Δ͔ؔΒThrow͞ΕΔ CoroutinesͷγεςϜతͳྫ֎
©2020 Wantedly, Inc. ֘ίʔυ class FetchDiscoverPostsUseCaseImpl( private val discoverRepository: DiscoverRepository,
) : FetchDiscoverPostsUseCase { override suspend operator fun invoke(sectionId: DiscoverSectionId) { try { return discoverRepository.fetchDiscoverPosts(sectionId) } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { throw FetchDiscoverProjectsUseCase.Error( "Failed to fetch discover posts for section: $sectionId”, e ) } } } ͭ·ΓɺCoroutinesͷγεςϜతͳྫ֎Λ ϥοϓͯ͠͠·͔ͬͨΒΫϥογϡͨ͠
©2020 Wantedly, Inc. JobCancellationExceptionৄղ ͱͱJavaͷฒྻॲཧAPIͰΘΕ͍ͯͨྫ֎
©2020 Wantedly, Inc. Javaͷੈք java.util.concurrent.Future#get() try { future.get(); } catch
(ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } CancellationExceptionRuntimeException ͳͷͰ໌ࣔతͳcatchෆཁ CancellationΛcatchͪ͠Ό͏͜ͱ͕ى͖ͮΒ͍
©2020 Wantedly, Inc. Kotlinͷੈք Kotlinʹݕࠪྫ֎͕ͳ͍ catch(Throwable)͕ͪ͠ CoroutinesͰࠓճͷΑ͏ͳʹܨ͕Γ͍͢ cancel()ͯ͠ͳ͍͔ͱ͍͏ςετ;ͭ͏ॻ͔ͳ͍͠ɺ ݱঢ়ϓϩάϥϚ͕ؾΛ͚ͭΔ͔͠ͳ͍ʁ
©2020 Wantedly, Inc. Ͳ͏͖͔͢ launch { try { codeThatCanThrowExceptions() //
// } catch (e: Throwable) { // } catch (e: Exception) { // } catch (e: RuntimeException) { // } catch (e: IllegalStateException) { // } catch (e: CancellationException) { } catch (e: SpecificException) { // throw MyException(e) } } // OR launch { try { codeThatCanThrowExceptions() } catch (e: Throwable) { throw e as? CancellationException ?: MyException(e) } }
©2020 Wantedly, Inc. w ,PUMJOʹݕࠪྫ֎͕ͳ͍ͷͰࡶʹ͕͕ͪͩ͠ɺ $PSPVUJOFTͰ5PP(FOFSJDͳDBUDIةݥ w DBUDI͔ͨͬͨ͠Β$BODFMMBUJPOSFUISPX w ͘͠$PSPVUJOF&YDFQUJPO)BOEMFSΛ͏
w -JOUແࢹ͠ͳ͍ ·ͱΊ
ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc.