-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add fatal errors * add tests * fix linting issues * update lint rule (removes warning) * change type and guard against nil * fix ci and change type names again * use is to assert * change type and constructor again * fix errname lint error * fix IsFatalError method * remove unnecessary check * make type unexported
- Loading branch information
Showing
8 changed files
with
138 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright © 2024 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cerrors | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// fatalError is an error type that will differentiate these from other errors that could be retried. | ||
type fatalError struct { | ||
Err error | ||
} | ||
|
||
// NewFatalError creates a new fatalError. | ||
func NewFatalError(err error) *fatalError { | ||
return &fatalError{Err: err} | ||
} | ||
|
||
// Unwrap returns the wrapped error. | ||
func (f *fatalError) Unwrap() error { | ||
return f.Err | ||
} | ||
|
||
// Error returns the error message. | ||
func (f *fatalError) Error() string { | ||
if f.Err == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("fatal error: %v", f.Err) | ||
} | ||
|
||
// IsFatalError checks if the error is a fatalError. | ||
func IsFatalError(err error) bool { | ||
var fatalErr *fatalError | ||
return As(err, &fatalErr) | ||
} |
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,86 @@ | ||
// Copyright © 2024 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cerrors_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/conduitio/conduit/pkg/foundation/cerrors" | ||
"github.com/matryer/is" | ||
) | ||
|
||
func TestNewFatalError(t *testing.T) { | ||
is := is.New(t) | ||
|
||
err := cerrors.New("test error") | ||
fatalErr := cerrors.NewFatalError(err) | ||
wantErr := fmt.Sprintf("fatal error: %v", err) | ||
|
||
is.Equal(fatalErr.Error(), wantErr) | ||
} | ||
|
||
func TestIsFatalError(t *testing.T) { | ||
is := is.New(t) | ||
err := cerrors.New("test error") | ||
|
||
testCases := []struct { | ||
name string | ||
err error | ||
want bool | ||
}{ | ||
{ | ||
name: "when it's a fatalError", | ||
err: cerrors.NewFatalError(err), | ||
want: true, | ||
}, | ||
{ | ||
name: "when it's wrapped in", | ||
err: fmt.Errorf("something went wrong: %w", cerrors.NewFatalError(cerrors.New("fatal error"))), | ||
want: true, | ||
}, | ||
{ | ||
name: "when it's not a fatalError", | ||
err: err, | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := cerrors.IsFatalError(tc.err) | ||
is.Equal(got, tc.want) | ||
}) | ||
} | ||
} | ||
|
||
func TestUnwrap(t *testing.T) { | ||
is := is.New(t) | ||
|
||
err := cerrors.New("test error") | ||
fatalErr := cerrors.NewFatalError(err) | ||
|
||
is.Equal(cerrors.Unwrap(fatalErr), err) | ||
} | ||
|
||
func TestFatalError(t *testing.T) { | ||
is := is.New(t) | ||
|
||
err := cerrors.New("test error") | ||
fatalErr := cerrors.NewFatalError(err) | ||
wantErr := fmt.Sprintf("fatal error: %v", err) | ||
|
||
is.Equal(fatalErr.Error(), wantErr) | ||
} |
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