Skip to content

Commit

Permalink
Merge pull request #12 from CrowdStrike/chore/note_on_osexit
Browse files Browse the repository at this point in the history
chore: update docs with a note about os.Exit
  • Loading branch information
jsteenb2 committed Nov 13, 2023
2 parents 331e596 + 0f8ccf4 commit ba7fc09
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,45 @@ func TestHandlerIntegration(t *testing.T) {

```

### A note on `os.Exit`

Please refrain from using `os.Exit`. When an error is encountered, we want to return a message
to the caller. Otherwise, it'll `os.Exit` and all stakeholders will have no idea what to make
of it. Instead, use something like the following in `fdk.Run`:

```go
// sdk.go

package fdk

import (
"context"
)

// Run is the meat and potatoes. This is the entrypoint for everything.
func Run[T Cfg](ctx context.Context, newHandlerFn func(_ context.Context, cfg T) Handler) {
// ... trim

cfg, loadErr := readCfg[T](ctx)
if loadErr != nil {
if loadErr.err != nil {
// these being specific to the author's eyes only
logger.Error("failed to load config", "err", loadErr.err)
}
// here we return a useful error to the caller of the function
run(ctx, logger, ErrHandler(loadErr.apiErr))
return
}

h := newHandlerFn(ctx, cfg)

run(ctx, logger, h)

return
}

```

---

<p align="center"><img src="https://raw.githubusercontent.com/CrowdStrike/falconpy/main/docs/asset/cs-logo-footer.png"><BR/><img width="250px" src="https://raw.githubusercontent.com/CrowdStrike/falconpy/main/docs/asset/adversary-red-eyes.png"></P>
Expand Down
12 changes: 5 additions & 7 deletions sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (r Response) StatusCode() int {

// Run is the meat and potatoes. This is the entrypoint for everything.
func Run[T Cfg](ctx context.Context, newHandlerFn func(_ context.Context, cfg T) Handler) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true}))
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{}))

defer func() {
if err := recover(); err != nil {
Expand All @@ -160,12 +160,10 @@ func Run[T Cfg](ctx context.Context, newHandlerFn func(_ context.Context, cfg T)

cfg, loadErr := readCfg[T](ctx)
if loadErr != nil {
run(ctx, logger, HandlerFn(func(ctx context.Context, r Request) Response {
if loadErr.err != nil {
logger.Error("failed to load config", "err", loadErr.err)
}
return Response{Errors: []APIError{loadErr.apiErr}}
}))
if loadErr.err != nil {
logger.Error("failed to load config", "err", loadErr.err)
}
run(ctx, logger, ErrHandler(loadErr.apiErr))
return
}

Expand Down

0 comments on commit ba7fc09

Please sign in to comment.