-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-41988: [Go] Add FormatRecoveredError to consistently handle recove…
…ry with wrapped errors (#41989) ### Rationale for this change Part of #36186 (Fixes it, IMO) Fixes #41988. ### What changes are included in this PR? A new internal `FormatRecoveredError()` function adds consistency to errors generated from panic recovery. ### Are these changes tested? Yes. ### Are there any user-facing changes? Users will be able to unwrap errors thrown by allocators, for example. * GitHub Issue: #41988
- Loading branch information
Showing
13 changed files
with
138 additions
and
266 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
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,31 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 utils | ||
|
||
import "fmt" | ||
|
||
// FormatRecoveredError is used in cases where a panic/recover receives an | ||
// object which is potentially an error that could be wrapped, instead of | ||
// formatted, so that callers can see it. This may be useful, for example, | ||
// with custom Allocators which panic to signal failure; these panics will be | ||
// recovered as wrapped errors, letting the client distinguish them. | ||
func FormatRecoveredError(msg string, recovered any) error { | ||
if err, ok := recovered.(error); ok { | ||
return fmt.Errorf("%s: %w", msg, err) | ||
} | ||
return fmt.Errorf("%s: %v", msg, recovered) | ||
} |
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,62 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you 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 utils | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testError struct{} | ||
|
||
var _ error = testError{} | ||
|
||
func (testError) Error() string { | ||
return "test error" | ||
} | ||
|
||
func TestFormatRecoveredError(t *testing.T) { | ||
defer func() { | ||
thing := recover() | ||
assert.NotNil(t, thing) | ||
assert.Error(t, thing.(testError)) | ||
|
||
err := FormatRecoveredError("recovered thing", thing) | ||
|
||
assert.Equal(t, "recovered thing: test error", err.Error()) | ||
assert.True(t, errors.Is(err, testError{})) | ||
assert.Equal(t, "test error", errors.Unwrap(err).(testError).Error()) | ||
}() | ||
|
||
panic(testError{}) | ||
} | ||
|
||
func TestFormatRecoveredNonError(t *testing.T) { | ||
defer func() { | ||
thing := recover() | ||
assert.NotNil(t, thing) | ||
|
||
err := FormatRecoveredError("recovered thing", thing) | ||
|
||
assert.Equal(t, "recovered thing: just a message", err.Error()) | ||
assert.False(t, errors.Is(err, testError{})) | ||
}() | ||
|
||
panic("just a message") | ||
} |
Oops, something went wrong.