labelerrとsuberrを作った話 / created labelerr and suberr

1b7098127bb4872f5fa10415d88479b7?s=47 nametake
December 18, 2018

labelerrとsuberrを作った話 / created labelerr and suberr

1b7098127bb4872f5fa10415d88479b7?s=128

nametake

December 18, 2018
Tweet

Transcript

  1. labelerrͱ
 suberrΛ࡞ͬͨ࿩ @nametake

  2. ࣗݾ঺հ

  3. Shogo Nameki @nametake Go, Vim

  4. Error handling

  5. Έͳ͞Μ

  6. GoͷError handling ޷͖Ͱ͔͢ʁ

  7. ΋ͪΖΜ ޷͖Ͱ͢ΑͶʁ

  8. ࢲ΋޷͖Ͱ͢

  9. ޷͖ͳΜͰ͕͢……

  10. StackTraceͱ͔ ErrorͷݪҼͱ͔ ແͯ͘ࠔΔ

  11. ͦΜͳ͋ͳͨʹʂ

  12. pkg/errors

  13. Stack Trace͕ Errorʹͭ͘ʂ

  14. errors.CauseͰ ݪҼΛऔΓग़ͤΔʂ

  15. ͜Ε͑͋͞Ε͹ ࠔͬͨ͜ͱ͸ ى͜Βͳ͍ʂʂʂ

  16. Θ͚Ͱ΋ͳ͔ͬͨ

  17. ྫ͑͹

  18. WebΞϓϦͰͷ Error Handling

  19. 4xx 5xx

  20. 4xx͸Ϣʔβʔͷ ૢ࡞ʹΑͬͯ͸ ൃੜ͢Δ

  21. 4xxͱ5xx͸ ҧ͏΋ͷͱͯ͠ औΓѻ͍͍ͨ

  22. Go͸಺෦తʹ͸ Ͳͬͪ΋Error (e.g. NotFound)

  23. ൃੜݯͰ͸ 4xxͱ͔5xxͱ͔͸ ҙࣝ͠ͳ͍

  24. errors.CauseͰ͸ ۠ผͰ͖ͳ͍

  25. ΄͔ʹ΋

  26. None
  27. None
  28. ͲΜͳઃܭͰ΋

  29. ϏδωεϩδοΫ (DomainϩδοΫ)

  30. Domain Error

  31. Errorͷதͷ Domain ErrorΛ औΓग़͍ͨ͠ (e.g. middleware)

  32. ઌʹผͷError͕ ൃੜ͍ͯͨ͠৔߹ errors.Cause͸ ࢖͑ͳ͍

  33. ͦͷ݁Ռ

  34. if err := f(); err != nil { return errors.Wrap(

    ErrDomain, err.Error(), ) }
  35. if err := f(); err != nil { return errors.Wrap(

    ErrDomain, err.Error(), ) }
  36. if err := f(); err != nil { return errors.Wrap(

    ErrDomain, err.Error(), ) }
  37. if err := f(); err != nil { return errors.Wrap(

    ErrDomain, err.Error(), ) }
  38. Stack Trace΋ ফ͑Δ͠ඇৗʹअѱ

  39. Ͳ͏ʹ͔͍ͨ͠

  40. ࡞ͬͨ

  41. github.com/nametake/labelerr github.com/nametake/suberr

  42. github.com/ nametake/labelerr

  43. Wrap(err error, label string) error Label(err error) string

  44. Wrap(err error, label string) error Label(err error) string

  45. Wrap(err error, label string) error Label(err error) string

  46. Wrap(err error, label string) error Label(err error) string

  47. Wrap(err error, label string) error Label(err error) string

  48. Wrap(err error, label string) error Label(err error) string

  49. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  50. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  51. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  52. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  53. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  54. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  55. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  56. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err)) third: second: warning: first: cause
  57. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  58. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err)) warning
  59. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err)) warning
  60. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err))
  61. err := errors.New("cause") err = errors.Wrap(err, "first") err = labelerr.Wrap(err,

    "warning") err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(labelerr.Label(err)) fmt.Println(errors.Cause(err)) cause
  62. cause first wrap LABEL ERROR second wrap

  63. cause first wrap LABEL ERROR second wrap

  64. github.com/ nametake/suberr

  65. Add(main error, sub error) error SubCause(err error) error

  66. Add(main error, sub error) error SubCause(err error) error

  67. Add(main error, sub error) error SubCause(err error) error

  68. Add(main error, sub error) error SubCause(err error) error

  69. Add(main error, sub error) error SubCause(err error) error

  70. Add(main error, sub error) error SubCause(err error) error

  71. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  72. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  73. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  74. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  75. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  76. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  77. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  78. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err)) third: second: sub: first: cause
  79. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  80. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err)) sub
  81. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err)) sub
  82. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err))
  83. err := errors.New("cause") err = errors.Wrap(err, "first") err = suberr.Add(err,

    errors.New("sub")) err = errors.Wrap(err, "second") err = errors.Wrap(err, "third") fmt.Println(err) fmt.Println(suberr.SubCause(err)) fmt.Println(errors.Cause(err)) cause
  84. cause first wrap second wrap third wrap

  85. cause first wrap second wrap third wrap SUB ERROR

  86. ಺෦Ͱpkg/errorsΛ ࢖͍ͬͯΔͷͰ Stack Trace΋࢒Δ

  87. Ҿ਺΋ฦΓ஋΋ Primitiveͳܕ͔͠ ࢖͍ͬͯͳ͍

  88. FB଴ͬͯ·͢ʂ