@@ -23,6 +23,8 @@ package fx
2323import (
2424 "context"
2525 "time"
26+
27+ "go.uber.org/multierr"
2628)
2729
2830// Shutdowner provides a method that can manually trigger the shutdown of the
@@ -34,17 +36,11 @@ type Shutdowner interface {
3436}
3537
3638// ShutdownOption provides a way to configure properties of the shutdown
37- // process. Currently, no options have been implemented.
39+ // process.
3840type ShutdownOption interface {
3941 apply (* shutdowner )
4042}
4143
42- type exitCodeOption int
43-
44- func (code exitCodeOption ) apply (s * shutdowner ) {
45- s .exitCode = int (code )
46- }
47-
4844var _ ShutdownOption = exitCodeOption (0 )
4945
5046// ExitCode is a [ShutdownOption] that may be passed to the Shutdown method of the
@@ -71,6 +67,45 @@ func ShutdownTimeout(timeout time.Duration) ShutdownOption {
7167 return shutdownTimeoutOption (timeout )
7268}
7369
70+ type shutdownErrorOption []error
71+
72+ func (errs shutdownErrorOption ) apply (s * shutdowner ) {
73+ s .app .err = multierr .Append (s .app .err , multierr .Combine (errs ... ))
74+ }
75+
76+ // ShutdownError registers any number of errors with the application shutdown.
77+ // If more than one error is given, the errors are combined into a
78+ // single error. Similar to invocations, errors are applied in order.
79+ //
80+ // You can use these errors, for example, to decide what to do after the app shutdown.
81+ //
82+ // customErr := errors.New("something went wrong")
83+ // app := fx.New(
84+ // ...
85+ // fx.Provide(func(s fx.Shutdowner, a A) B {
86+ // s.Shutdown(fx.ShutdownError(customErr))
87+ // }),
88+ // ...
89+ // )
90+ // err := app.Start(context.Background())
91+ // if err != nil {
92+ // panic(err)
93+ // }
94+ // defer app.Stop(context.Background())
95+ //
96+ // if err := app.Err(); errors.Is(err, customErr) {
97+ // // custom logic here
98+ // }
99+ func ShutdownError (errs ... error ) ShutdownOption {
100+ return shutdownErrorOption (errs )
101+ }
102+
103+ type exitCodeOption int
104+
105+ func (code exitCodeOption ) apply (s * shutdowner ) {
106+ s .exitCode = int (code )
107+ }
108+
74109type shutdowner struct {
75110 app * App
76111 exitCode int
0 commit comments