| 30 | ) |
| 31 | |
| 32 | func ExampleError() { |
| 33 | // A module that provides a HTTP server depends on |
| 34 | // the $PORT environment variable. If the variable |
| 35 | // is unset, the module returns an fx.Error option. |
| 36 | newHTTPServer := func() fx.Option { |
| 37 | port := os.Getenv("PORT") |
| 38 | if port == "" { |
| 39 | return fx.Error(errors.New("$PORT is not set")) |
| 40 | } |
| 41 | return fx.Provide(&http.Server{ |
| 42 | Addr: fmt.Sprintf("127.0.0.1:%s", port), |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | app := fx.New( |
| 47 | fx.NopLogger, |
| 48 | newHTTPServer(), |
| 49 | fx.Invoke(func(s *http.Server) error { return s.ListenAndServe() }), |
| 50 | ) |
| 51 | |
| 52 | fmt.Println(app.Err()) |
| 53 | |
| 54 | // Output: |
| 55 | // $PORT is not set |
| 56 | } |