Upgrade to Pro — share decks privately, control downloads, hide ads and more …

my new error

my new error

sadnessOjisan

March 19, 2023
Tweet

More Decks by sadnessOjisan

Other Decks in Programming

Transcript

  1. My New Error… It is my new era. 1 Nikkei

    Inc. Yuta Ide(@sadnessOjisan) YAPC::Kyoto 2023
  2. 3

  3. 4

  4. ࢲͱ Try Catch • 2022/10/01 - 2022/12/31 ͷ̏ϲ݄ؒɺΞϥʔτ؂ࢹͷ࢓ࣄ ͚ͩΛ͢Δ •

    ࢲͷνʔϜ͸̏̌ݸ͍ۙϨϙδτϦ͕Քಇ͍ͯ͠ΔϚΠΫϩαʔϏε؀ڥͰɺ ֤αʔϏε͕ग़ྗ͢ΔΤϥʔϩά͕ Slack ʹ࿈ܞ͞ΕΔ • ΤϥʔϋϯυϦϯά͕ෆे෼ͳαʔϏε͔Βͷग़ྗʹ೰·͞ΕΔ 5
  5. Nikkei Tech Talk Ͱͷ঺հ • Nikkei Tech Talk ͸೔ܦͷΤϯδχΞ૊৫ ͷษڧձ

    • #3 ͷϑϩϯτΤϯυճͰΞϥʔτվળͷ औΓ૊ΈΛ঺հ 6 https://speakerdeck.com/sadnessojisan/jian-shi- senaakansi-wu-da-zhi-dakeniookamitutena
  6. Nikkei Tech Talk Ͱͷ঺հ • Nikkei Tech Talk ͸೔ܦͷΤϯδχΞ૊৫ ͷษڧձ

    • #3 ͷϑϩϯτΤϯυճͰΞϥʔτվળͷ औΓ૊ΈΛ঺հ 7 https://speakerdeck.com/sadnessojisan/jian-shi- senaakansi-wu-da-zhi-dakeniookamitutena ΦΦΧϛ
  7. ݸਓϒϩάͰͷ঺հ - My new error… - • ʮ͜͏͍͏ΤϥʔϋϯυϦϯάΛ͢Ε͹ ؂ࢹ͠΍͍͢ʯͱ͍ͬͨςΫχοΫͷ঺ հ

    • Result • Custom Error • Error Cause • Sentry 9 https://blog.ojisan.io/my-new-error/
  8. ݸਓϒϩάͰͷ঺հ - Sentry SDK ׬શཧղ - • Sentry SDKͷ࢓૊Έʹ͍ͭͯ •

    Sentry ͸ઃఆ߲໨͕ͱͯ΋ଟ͍͕ɺ Sentry ͷ࢓૊ΈΛ஌Ε͹ԿΛઃఆ͢΂͖ ͔͢͜͠ݟ͑ͯ͘Δ 10 https://blog.ojisan.io/sentry-sdk-kanzen-rikai/
  9. Ͳ͏͍͏πʔϧΛ࢖͑͹͍͍ͷ͔ • PagerDuty: Φϯίʔϧ • StatusCake: ֓ܗ؂ࢹ • Sentry: ϩΪϯάɺΞϥʔτ

    • GCP / AWS: උ͑෇͚ͷϩάج൫ • Datadog: ϩάج൫ɺαʔϏεލ͍ͩ౷߹ɺΫΤϦ͕͠΍͍͢ 18
  10. Ͳ͏͍͏πʔϧΛ࢖͑͹͍͍ͷ͔ • PagerDuty: Φϯίʔϧ • StatusCake: ֓ܗ؂ࢹ • Sentry: ϩΪϯάɺΞϥʔτ

    • GCP / AWS: උ͑෇͚ͷϩάج൫ • Datadog: ϩάج൫ɺαʔϏεލ͍ͩ౷߹ɺΫΤϦ͕͠΍͍͢ • Kibana: ݕࡧɺϏδϡΞϥΠζ 19
  11. πʔϧʹΑΔ৘ใ൙ཞ • πʔϧͷ໾ׂ͕ඃ͍ͬͯΔ • Datadog ΋ cloud monitoring ΋ΞϥʔτͰ͖Δ •

    ͱ͸͍͑ɺ໨తΛܾΊ͍ͯΔͷͰ͋Ε͹ݸผʹ͞·͟·ͳπʔϧΛಋೖ͢ Δ͜ͱࣗମ͸ྑ͍ͱ͞Ε͍ͯΔ • ͔͠͠ɺͦͷલఏͷڞ༗͕ෆे෼ͩͱ৘ใ൙ཞ͕ൃੜ͢Δ • ʮ͜͏͍͏ো֐͕ى͖ͨͱ͖͸ԿΛݟΕ͹͍͍Ͱ͔͢ʁʯ 20
  12. ΤϥʔϋϯυϦϯάΛ๨Εͳ͍ͨΊʹResultܕͷಋೖ • JS Ͱ͸ྫ֎ͷ؅ཧ͸ try catch Ͱߦ ͏ • ͔͠͠Ͳͷؔ਺͕ྫ֎Λ౤͛Δ͔

    Λ஌Βͳ͍ͱ try Λ෇͚Εͳ͍ • νʔϜ։ൃ΍ϥΠϒϥϦͷར༻ͳ Ͳɺࣗ෼͕࡞͍ͬͯͳ͍ίʔυΛ ݺͼग़͢ͱ͖ʹ͚ͭ๨Ε͕ൃੜ͢ Δ 24
  13. ΤϥʔϋϯυϦϯάΛ๨Εͳ͍ͨΊʹResultܕͷಋೖ • ੒ޭɾࣦഊΛ Result ͱ͍͏ܕͰද ݱ • type Result<T, E>

    = { _tag: “success”, val: T} | {_tag: “err”, val: E } • TS ؀ڥͰ͸ݺͼग़͠ଆʹ੒ޭ͔ࣦ ഊ͔ͷ֬ೝΛڧ੍ͤ͞ΒΕΔͷ ͰɺΤϥʔϋϯυϦϯάͷ͠๨Ε Λ๷͛Δ 25
  14. Result ܕʹৼΓ੾Δ͜ͱ͸ຊ౰ʹଥ౰ͳͷ͔ • ΦϒδΣΫτͷੜ੒ͱݕূ͸ਖ਼ৗܥʹ͓͍ͯ͸ʢແࢹͰ͖Δ΄ͲʹඍʑͨΔ ΋ͷ͚ͩͲʣແବͳίετͰ͸ͳ͍͔ • ഑ྻɺΦϒδΣΫτɺMapʹΞΫηεͨ͠ͱ͖ͷϋϯυϦϯάʹ΋ Result ͕ ग़ͯ͘ΔͷͰ͸ͳ͍͔

    • const a = [1]; const b = a[0]; ઈରʹ஋͕ೖ͍ͬͯΔ b Λ number ͱͯ͠࢖͍ ͨͯ͘΋ܕ্Ͱ͸ undefined, ྫ֎Λ౤͛ͨ͘ͳΒͳ͍Ͱ͔͢ʁ • ͦ΋ͦ΋ݴޠػೳͰͳ͍΋ͷʹཔΔͱຊདྷͷτϨʔε͕ෆਖ਼֬ʹͳΔͷͰ͸ ͳ͍͔ 26
  15. ϝϦοτʹ໨Λ޲͚ͯ Resultܕ ͷಋೖ΁ͱ౿Έ੾Δ • ೔ܦͷಛੑ্SSR͕ඞਢͰɺ෦෼తͳࢴ໘ੜ੒ʹ ࣦഊͨ͠ͱ͖ʹରԠ͠΍͍͢ • ৽ฉهࣄͷೖߘ͸ͱͯ΋ࣗ༝౓͕ߴ͘ɺΞϓϦ έʔγϣϯͷͲ͔͜Βྫ֎͕ඈΜͰ͘Δͷ͔༧ଌ Ͱ͖ͳ͍

    • ༗ࣝऀʢRustaceanʣʹΑΔϋϯζΦϯ΍߈ུΨΠ υͷڞ༗ • Fastly C@E ͷಋೖͰ Rust ʹ׳Εͳ͍ͱ͍͚ͳ͍ • ଞνʔϜʹScalaͱKotlinͷ೿ൊ͕͋ΓɺResultܕࣗ ମʹର͢Δ஌ݟ͸͋Δ 27 https://hack.nikkei.com/blog/advent20221201/
  16. ҰԠσϝϦοτʹ΋໨Λ޲͚Δͱ • ΦϒδΣΫτͷੜ੒ͱݕূ͸ਖ਼ৗܥʹ͓͍ͯ͸ʢແࢹͰ͖Δ΄ͲʹඍʑͨΔ΋ͷ͚ͩ ͲʣແବͳίετͰ͸ͳ͍͔ 
 => ແࢹɺύϑΥʔϚϯε͕໰୊ʹͳΔͳΒݦࡏԽ͔ͯ͠Βରࡦ͢Δ • ഑ྻɺΦϒδΣΫτɺMapʹΞΫηεͨ͠ͱ͖ͷϋϯυϦϯάʹ΋ Result

    ͕ग़ͯ͘Δͷ Ͱ͸ͳ͍͔ 
 => ։ൃऀ͕ઈରʹ҆શͱ஌͍ͬͯΔͷͰ͋Ε͹ྫ֎Λ౤͛Δɻ഑ྻʹ஋Λ٧Ίͨޙʹऔ Γग़ͯͦ͠ͷ஋ͷ࣮ࡏΛݕূʢܕݕࠪ௨ͨ͢Ίʹʣͨ͠ͱ͖ɺྫ֎্͕͕ΔΘ͚͕ͳ͍ • ͦ΋ͦ΋ݴޠػೳͰͳ͍΋ͷʹཔΔͱຊདྷͷτϨʔε͕ෆਖ਼֬ʹͳΔͷͰ͸ͳ͍͔ • ؆୯ͳίϯϏωʔλ͚ͩΛ࢖͏Α͏ʹ͠ɺ࠷ѱ࢖Θͣʹ۪௚ͳίʔυΛॻ͍ͯ΋ڐ༰ • functional programming ʹد͍ͤͨΘ͚Ͱ͸ͳ͍ 28
  17. ResultܕΛಋೖ͢Δʹ͸ίϯϏωʔλ΋ඞཁ • Resultͷ஋Λ࢖͏ͨΊʹ͸ຖճ Result Λ֎͢ඞཁ͕͋ΔɻϝϯυΫαΠ • map: Result ͷ val

    ʹؔ਺Λద༻Ͱ͖Δɻmap(Result<T, E>, T->U) ͸ Result<U , E> ͱͳΔɻmap(getUserResult, (user) => encUserId(user)); • flatMap: Result ͷ val ʹ Result Λฦؔ͢਺Λద༻Ͱ͖ΔɻResult<T, Result<T, E>> ͕ Result<T, E> ͱͳΔɻflatMap(getUserResult, (user) => getBoughtItemsByUser(user)); • Result Λฦؔ͢਺ಉ࢜ͷ߹੒͕༰қʹͳΓɺResultΛͨ͘͞Μ࢖ͬͯ΋Ϣʔ βʔͷॻ͖ຯ͸มΘΒͳ͍ɻ 29
  18. ίϯϏωʔλͷ࣮૷ΛϥΠϒϥϦʹ೚ͤΔ • ίϯϏωʔλΛࣗ෼ୡͰ༻ҙ͢Δͷ͸େมͳͷͰ option-t ͱ͍͏ϥΠϒϥϦ Λ࠾༻ • https://github.com/option-t/option-t • A

    toolkit of Nullable/Option/Result type implementation in ECMAScript. Their APIs are inspired by Rust's `Option<T>` and `Result<T, E>`. • Rust ͷυΩϡϝϯτͰֶ΂Δ͜ͱ͕େ͖ͳΞυόϯςʔδ 30
  19. Τϥʔʹ໊લΛׂΓৼΔ • SentryͰ͸ Issue ͷλΠτϧ͕ҰཡͰදࣔͰ ͖ɺͦͷλΠτϧͰूܭͰ͖Δ • λΠτϧͷཻ౓΍ωʔϛϯά͕େࣄ • λΠτϧ͸

    Error.prototype.name Ͱ੍ޚͰ͖Δ • new Error(‘msg’).name = “MyError” • ͱ͢Δͷ͸ΊΜͲ͍͘͞ͷͰɺͦͷ໊લΛ࣋ͬ ͨ Custom Error Λఆ͓ٛͯ͘͠ͷ͕ৗ౟ 31 ԿͷΤϥʔ͔෼͔Βͳ͍ʂ
  20. σόοά͠΍͍͢ϝλ৘ใΛϝοηʔδʹೖΕΔ • SentryͰ͸ Error.prototype.message ͷ಺༰͕ ϩάʹݱΕΔ • ΤϥʔΛ෼ੳ͢Δͱ͖ͷώϯτΛ message ʹ

    ೖΕ͓ͯ͘ • ݺͼग़͞ΕΔؔ਺ͷҾ਺ • ϢʔβʔτʔΫϯ΍ݸਓ৘ใͳͲ͸ग़ྗ͠ͳ ͍Α͏ʹ஫ҙ • SentryʹݶΒͣʮϩάʹग़ྗ͢΂͖͸Կ͔ʯ Λҙࣝ͢Δͱྑ͍ 32 ԿͷΤϥʔ͔෼͔Βͳ͍ʂ
  21. ϩάϨϕϧΛ࢖͍෼͚Δ • Sentry͸ log, warn, error ͱ͍ͬͨϩάϨϕϧͷ࢖͍෼͚͕Ͱ͖ΔɻϑΟϧλͯ͠ݕࡧ͠ ͨΓɺϨϕϧ͝ͱʹΞϥʔτΛઃఆͰ͖Δɻ • ͳΜͰ΋͔ΜͰ΋ΤϥʔʹͤͣɺϩάϨϕϧΛ࢖͍෼͚ΔͱΦΦΧϛগ೥໰୊ΛݮΒͤΔ

    • error: ҟৗ͕ى͖ͨͱ͖ʹ࢖͏ • warn: ݺ͹Εͯخ͘͠ͳ͍ॲཧ͕ݺ͹Εͨͱ͖ʹ࢖͏ɻdeprecated ͳ API Λݺ͹Εͨ ࣌΍retryͷ࣮ߦͳͲ • Info: ϨεϙϯεͳͲͰ֬ೝͰ͖ͳͯ͘໨ʹݟ͑ΔܗͰ֬ೝͰ͖ͳ͍͕ɺ࣮ߦ͞Εͨܗ ੻Λ࢒͢ͱ͖ʹ࢖͏ɻಛघͳΤϯυϙΠϯτͷݺͼग़͠΍application cache͕miss͠ ͨͱ͖ͳͲ 33
  22. ϊΠζͷ཈੍Λ͍ͨ͠ • Ξϥʔτ͸UI͔Β ignore Ͱ͖Δ • ͔͠͠ҟͳΔURL͔Βಉ͡Τϥʔ͕ग़ͨ৔߹ɺҟͳΔΤϥʔͱͯ͠ѻΘΕͯ ignore ͯ͠΋৽͘͠ಉ͡Τϥʔ͕௨஌͞ΕΔ •

    ͦ΋ͦ΋ Sentry ʹૹΒͳ͍Α͏ʹ͍ͨ͠ɻinit ͷ option Ͱ͋Δఔ౓੍ޚͰ͖ Δɻhttps://docs.sentry.io/platforms/javascript/configuration/filtering/ • ignoreErrors: send ͠ͳ͍ΤϥʔΛએݴͰ͖Δ • denyUrls: ֎෦ϞδϡʔϧʹΑΔΤϥʔΛsend͠ͳ͍ 36
  23. ιʔεϚοϓΛग़ྗ͢Δ • SentryͰ͸ source map Λૹ৴͢Δ͜ͱͰ minify ͞ΕͨίʔυͷΤϥʔΛɺਓ ͕ؒಡΊΔܗʹͯ͘͠ΕΔ •

    Sentry͕Ϗϧυπʔϧ޲͚ϓϥάΠϯΛఏڙ͓ͯ͠ΓɺͦΕΛར༻͢Δ͜ͱͰ sourcemap Λૹ৴Ͱ͖Δ • webpack, nextjs • sentry-cli ͷઃఆ͕ඞཁ • Source map Λૹ৴ޙɺͭ·ΓϏϧυޙσϓϩΠલͷλΠϛϯάͰιʔε ίʔυ͔Β sourcemap Λফ͢͜ͱΛਪ঑ 37
  24. Error Cause Λ࢖͏ • ྫ֎Λcatchͯ͠٧Ί௚͢ͱͦͷྫ֎ ৘ใ͕ࣦΘΕͯ͠·͏ • catch અͰΧελϜΤϥʔΛ࡞ͬͯ Result.Error

    Λ࡞Δͱ͖ʹɺݩͷΤ ϥʔ͕ࣦΘΕͯ͠·͏໰୊͕͋ͬͨ • Error.prototype.cause Λ࢖͏͜ͱ ͰɺݩͷΤϥʔΛ৽͍͠Τϥʔʹੵ ΊΔ 38
  25. Node.js ΛΞοϓσʔτ͢Δ • Error Cause ͸ ES2022, Node.js 16.9 ͔Βͷػೳ

    • ౰࣌ฐࣾͷଟ͘ͷ Node.js ͷόʔδϣϯ͸ 16.9ະຬ • 16, 18 ΁ͷΞοϓσʔτ࡞ۀΛνʔϜϝΠτ͕Ξϥʔτվળͱฒߦͯ͠ߦ͏ • gyp-error ͱᛀΊͬ͜͢Δຖ೔ 39