Skip to content

Commit

Permalink
slightly improved abortion error ui handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n committed Feb 16, 2024
1 parent 5ac11db commit bcafd91
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 103 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func handlePanics(exitFn func(int)) {
log.Fatal(err)
exitFn(1)
}
if err, ok := o.(cli.ExecutionAbortedError); ok {
if err, ok := o.(cli.AbortionError); ok {
log.Error(err)
exitFn(0)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
40 changes: 40 additions & 0 deletions internal/cli/abor_on_error_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cli

import (
"fmt"

"github.com/sha1n/bert/api"
)

// AbortionError a marker type for fatal user errors.
// This type of errors is treated differently when user feedback is provided.
type AbortionError struct {
message string
}

func (e AbortionError) Error() string {
return e.message
}

// NewAbortionError creates a new abortion error with the specified message.
func NewAbortionError(id api.ID, err error) AbortionError {
return AbortionError{
message: fmt.Sprintf("'%s' reported an error. %s", id, err),
}
}

type abortOnErrorListener struct {
api.Listener
}

// NewAbortOnErrorListener creates a new listener that logs abortion events.
func NewAbortOnErrorListener(delegate api.Listener) api.Listener {
return &abortOnErrorListener{Listener: delegate}
}

// OnError logs an error message with the specified ID and error details
func (l abortOnErrorListener) OnError(id api.ID, err error) {
defer panic(NewAbortionError(id, err))
l.Listener.OnError(id, err)
l.Listener.OnScenarioEnd(id)
}
47 changes: 47 additions & 0 deletions internal/cli/abor_on_error_listener_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"errors"
"testing"

"github.com/sha1n/bert/api"
"github.com/stretchr/testify/mock"
)

func TestAbortOnErrorListener_OnError(t *testing.T) {
mockID := api.ID("mockID")
mockError := errors.New("mock error")
mockListener := new(MockListener)
mockListener.On("OnError", mockID, mockError).Once()
mockListener.On("OnScenarioEnd", mockID).Once()
abortOnErrorListener := NewAbortOnErrorListener(mockListener)

defer func() {
actual := recover()
if actual == nil {
t.Errorf("The code did not panic")
}

_, ok := actual.(AbortionError)
if !ok {
t.Errorf("Unexpected panic type: %T", actual)
}

mockListener.AssertExpectations(t)
}()

abortOnErrorListener.OnError(mockID, mockError)
}

type MockListener struct {
api.Listener
mock.Mock
}

func (m *MockListener) OnScenarioEnd(id string) {
m.Called(id)
}

func (m *MockListener) OnError(id api.ID, err error) {
m.Called(id, err)
}
38 changes: 0 additions & 38 deletions internal/cli/abortion_listener.go

This file was deleted.

63 changes: 0 additions & 63 deletions internal/cli/abortion_listener_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/cli/main_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func resolveExecutionListener(cmd *cobra.Command, spec api.BenchmarkSpec, ctx ap
}

if spec.FailFast {
listener = NewFailFastListener(listener)
listener = NewAbortOnErrorListener(listener)
}

return listener
Expand Down

0 comments on commit bcafd91

Please sign in to comment.