MCPcopy Index your code
hub / github.com/uber-go/fx / ExampleError

Function ExampleError

error_example_test.go:32–56  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30)
31
32func 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}

Callers

nothing calls this directly

Calls 5

ErrorFunction · 0.92
ProvideFunction · 0.92
NewFunction · 0.92
InvokeFunction · 0.92
ErrMethod · 0.45

Tested by

no test coverage detected