Skip to content

Commit

Permalink
fix(golangci-lint): log errors and panic if unrecoverable
Browse files Browse the repository at this point in the history
  • Loading branch information
boreyuk committed Jul 20, 2023
1 parent 1c98c51 commit 9c06e39
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 22 deletions.
5 changes: 4 additions & 1 deletion consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe
func (p *httpMockProvider) reset() {
p.mockserver.CleanupMockServer(p.config.Port)
p.config.Port = 0
_ = p.configure()
err := p.configure()
if err != nil {
log.Println("[ERROR] failed to configure the mock server")
}
}

// TODO: improve / pretty print this to make it really easy to understand the problems
Expand Down
12 changes: 10 additions & 2 deletions consumer/http_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ func (i *V4InteractionWithPluginRequestBuilder) Headers(headers matchers.Headers

// PluginContents configures a plugin. This may be called once per plugin registered.
func (i *V4InteractionWithPluginRequestBuilder) PluginContents(contentType string, contents string) *V4InteractionWithPluginRequestBuilder {
_ = i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents)
err := i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents)
if err != nil {
log.Println("[ERROR] failed to get plugin content for interaction:", err)
panic(err)
}

return i
}
Expand Down Expand Up @@ -539,7 +543,11 @@ func (i *V4InteractionWithPluginResponseBuilder) Headers(headers matchers.Header

// PluginContents configures a plugin. This may be called once per plugin registered.
func (i *V4InteractionWithPluginResponseBuilder) PluginContents(contentType string, contents string) *V4InteractionWithPluginResponseBuilder {
_ = i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_RESPONSE, contentType, contents)
err := i.interaction.interaction.WithPluginInteractionContents(native.INTERACTION_PART_RESPONSE, contentType, contents)
if err != nil {
log.Println("[ERROR] failed to get plugin content for interaction:", err)
panic(err)
}

return i
}
Expand Down
6 changes: 5 additions & 1 deletion installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func NewInstaller(opts ...installerConfig) (*Installer, error) {
i := &Installer{downloader: &defaultDownloader{}, fs: afero.NewOsFs(), hasher: &defaultHasher{}, config: &configuration{}}

for _, opt := range opts {
_ = opt(i)
err := opt(i)
if err != nil {
log.Println("[ERROR] failure when configuring installer:", err)
return nil, err
}
}

if _, ok := supportedOSes[runtime.GOOS]; !ok {
Expand Down
7 changes: 5 additions & 2 deletions internal/native/message_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,11 @@ func (m *MessageServer) MockServerMismatchedRequests(port int) []MismatchedReque
return []MismatchedRequest{}
}

// TODO: return no requests if error on unmarshal ?
_ = json.Unmarshal([]byte(C.GoString(mismatches)), &res)
err := json.Unmarshal([]byte(C.GoString(mismatches)), &res)
if err != nil {
log.Println("[ERROR] failed to unmarshal mismatches response, returning empty list of mismatches")
return []MismatchedRequest{}
}

return res
}
Expand Down
17 changes: 13 additions & 4 deletions internal/native/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,16 @@ func Init(logLevel string) {

if os.Getenv("PACT_LOG_PATH") != "" {
log.Println("[DEBUG] initialised native log to log to file:", os.Getenv("PACT_LOG_PATH"))
_ = logToFile(os.Getenv("PACT_LOG_PATH"), l)
err := logToFile(os.Getenv("PACT_LOG_PATH"), l)
if err != nil {
log.Println("[ERROR] failed to log to file:", err)
}
} else {
log.Println("[DEBUG] initialised native log to log to stdout")
_ = logToStdout(l)
err := logToStdout(l)
if err != nil {
log.Println("[ERROR] failed to log to stdout:", err)
}
}
}
}
Expand Down Expand Up @@ -356,8 +362,11 @@ func (m *MockServer) MockServerMismatchedRequests(port int) []MismatchedRequest
return []MismatchedRequest{}
}

_ = json.Unmarshal([]byte(C.GoString(mismatches)), &res)

err := json.Unmarshal([]byte(C.GoString(mismatches)), &res)
if err != nil {
log.Println("[ERROR] failed to unmarshal mismatches response, returning empty list of mismatches")
return []MismatchedRequest{}
}
return res
}

Expand Down
14 changes: 11 additions & 3 deletions message/v4/asynchronous_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ type UnconfiguredAsynchronousMessageBuilder struct {
rootBuilder *AsynchronousMessageBuilder
}

// AddMessage creates a new asynchronous consumer expectation
// UsingPlugin enables a plugin for use in the current test case
func (m *UnconfiguredAsynchronousMessageBuilder) UsingPlugin(config PluginConfig) *AsynchronousMessageWithPlugin {
_ = m.rootBuilder.pact.messageserver.UsingPlugin(config.Plugin, config.Version)
err := m.rootBuilder.pact.messageserver.UsingPlugin(config.Plugin, config.Version)
if err != nil {
log.Println("[ERROR] failed to add plugin:", err)
panic(err)
}

return &AsynchronousMessageWithPlugin{
rootBuilder: m.rootBuilder,
Expand All @@ -74,7 +78,11 @@ type AsynchronousMessageWithPlugin struct {
}

func (s *AsynchronousMessageWithPlugin) WithContents(contents string, contentType string) *AsynchronousMessageWithPluginContents {
_ = s.rootBuilder.messageHandle.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents)
err := s.rootBuilder.messageHandle.WithPluginInteractionContents(native.INTERACTION_PART_REQUEST, contentType, contents)
if err != nil {
log.Println("[ERROR] failed to get plugin content from message handle:", err)
panic(err)
}

return &AsynchronousMessageWithPluginContents{
rootBuilder: s.rootBuilder,
Expand Down
12 changes: 10 additions & 2 deletions message/v4/synchronous_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ type UnconfiguredSynchronousMessageBuilder struct {

// UsingPlugin enables a plugin for use in the current test case
func (m *UnconfiguredSynchronousMessageBuilder) UsingPlugin(config PluginConfig) *SynchronousMessageWithPlugin {
_ = m.pact.mockserver.UsingPlugin(config.Plugin, config.Version)
err := m.pact.mockserver.UsingPlugin(config.Plugin, config.Version)
if err != nil {
log.Println("[ERROR] failed to add plugin:", err)
panic(err)
}

return &SynchronousMessageWithPlugin{
pact: m.pact,
Expand All @@ -73,7 +77,11 @@ func (m *UnconfiguredSynchronousMessageBuilder) UsingPlugin(config PluginConfig)

// UsingPlugin enables a plugin for use in the current test case
func (m *SynchronousMessageWithPlugin) UsingPlugin(config PluginConfig) *SynchronousMessageWithPlugin {
_ = m.pact.mockserver.UsingPlugin(config.Plugin, config.Version)
err := m.pact.mockserver.UsingPlugin(config.Plugin, config.Version)
if err != nil {
log.Println("[ERROR] failed to add plugin:", err)
panic(err)
}

return m
}
Expand Down
5 changes: 4 additions & 1 deletion message/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func CreateMessageHandler(messageHandlers Handlers) proxy.Middleware {
}

w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
_, err = w.Write(body)
if err != nil {
log.Println("[ERROR] failed to write body response:", err)
}

return
}
Expand Down
10 changes: 7 additions & 3 deletions provider/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,18 @@ func getStateFromRequest(r *http.Request) (stateHandlerAction, error) {
var state stateHandlerAction
buf := new(strings.Builder)
tr := io.TeeReader(r.Body, buf)
// TODO: return err if unable to read ?
_, _ = io.ReadAll(tr)

_, err := io.ReadAll(tr)
if err != nil {
log.Println("[ERROR] getStateFromRequest unable to read request body:", err)
return stateHandlerAction{}, err
}

// Body is consumed above, need to put it back after ;P
r.Body = ioutil.NopCloser(strings.NewReader(buf.String()))
log.Println("[TRACE] getStateFromRequest received raw input", buf.String())

err := json.Unmarshal([]byte(buf.String()), &state)
err = json.Unmarshal([]byte(buf.String()), &state)
log.Println("[TRACE] getStateFromRequest parsed input (without params)", state)

if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ func HTTPReverseProxy(options Options) (int, error) {

log.Println("[DEBUG] starting reverse proxy on port", port)
go func() {
_ = http.ListenAndServe(fmt.Sprintf(":%d", port), wrapper(proxy))
err := http.ListenAndServe(fmt.Sprintf(":%d", port), wrapper(proxy))
if err != nil {
log.Println("[ERROR] error when starting reverse proxy server:", err)
panic(err)
}
}()

return port, nil
Expand Down
13 changes: 11 additions & 2 deletions utils/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ package utils
import (
"bytes"
"encoding/json"
"log"
)

// Format a JSON document to make comparison easier.
func FormatJSONString(object string) string {
var out bytes.Buffer
_ = json.Indent(&out, []byte(object), "", "\t")
err := json.Indent(&out, []byte(object), "", "\t")
if err != nil {
log.Println("[ERROR] failed to format string:", err)
return ""
}
return out.String()
}

// Format a JSON document for creating Pact files.
func FormatJSONObject(object interface{}) string {
out, _ := json.Marshal(object)
out, err := json.Marshal(object)
if err != nil {
log.Println("[ERROR] failed to encode string to json:", err)
return ""
}
return FormatJSONString(string(out))
}

Expand Down

0 comments on commit 9c06e39

Please sign in to comment.