Skip to content

Commit

Permalink
feat: add hook dumping HTTP response on error (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Oct 10, 2024
1 parent 077efc6 commit 202ef32
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/hooks/httpdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ var _ afterSuccessHook = (*HTTPDumpResponseHook)(nil)

// AfterSuccess dumps the response to stdout if enabled.
func (i *HTTPDumpResponseHook) AfterSuccess(hookCtx AfterSuccessContext, res *http.Response) (*http.Response, error) {
return i.dumpResponse(res)
}

var _ afterErrorHook = (*HTTPDumpResponseHook)(nil)

// AfterSuccess dumps the response to stdout if enabled.
func (i *HTTPDumpResponseHook) AfterError(hookCtx AfterErrorContext, res *http.Response, err error) (*http.Response, error) {
fmt.Printf("Error: %v\n", err)
return i.dumpResponse(res)
}

func (i *HTTPDumpResponseHook) dumpResponse(res *http.Response) (*http.Response, error) {
if !i.Enabled {
return res, nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/hooks/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ func initHooks(h *Hooks) {
h.registerAfterSuccessHook(&HTTPDumpResponseHook{
Enabled: os.Getenv("KONNECT_SDK_HTTP_DUMP_RESPONSE") == "true",
})
h.registerAfterErrorHook(&HTTPDumpResponseHook{
Enabled: os.Getenv("KONNECT_SDK_HTTP_DUMP_RESPONSE") == "true",
})
}

0 comments on commit 202ef32

Please sign in to comment.