-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
slightly improved abortion error ui handling
- Loading branch information
Showing
8 changed files
with
91 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters