-
Notifications
You must be signed in to change notification settings - Fork 653
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MIGRATION ISSUE: replacement for awserr.New() when creating errors for unit tests #2857
Comments
This doesn't actually satisfy Double-check that you're satisfying the interface w/ var _ smithy.APIError = (*mockApiError)(nil) You want something like type mockError struct {
code, msg string
fault smithy.ErrorFault
}
func(m *mockError) ErrorCode() string { return m.code }
func(m *mockError) ErrorMessage() string { return m.msg }
func(m *mockError) ErrorFault() smithy.ErrorFault { return m.fault }
func(m *mockError) Error() string { return "..." }
var _ smithy.APIError = (*mockError)(nil) You can also just use the concrete |
Thank you for the reply. I had not made any effort to satisfy the interface, only matched the struct fields. So the path forward seems to be to satisfy the interface. When I use var raee types.ResourceAlreadyExistsException
mockCloudWatchLogs.EXPECT().CreateLogGroup(context.TODO(), gomock.Any()).Return(
&cloudwatchlogs.CreateLogGroupOutput{},
smithy.GenericAPIError{
Code: raee.ErrorCode(),
Message: raee.ErrorMessage(),
Fault: raee.ErrorFault(),
},
) I get this error:
Am I using it incorrectly? |
Correct, this is the case across the board in Go. "Is this an X" checks at runtime are idiomatically done via checking if something satisfies an interface. (that's what errors.As is doing under the hood) I think it needs to be |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Pre-Migration Checklist
Go Version Used
Go 1.23
Describe the Migration Issue
Seeking the upgrade path for
awserr.New()
with respect to creating errors which can be unwrapped byerrors.As()
as recommended by the migration guide.A simple, clear scenario is a function which creates a CloudWatch log group as needed. During certain well-understood race conditions the log group very well may exist by the time the code calls
.CreateLogGroup()
. I need to swallow the specific error condition when the log group already exists, and pass-through the error on all other error types. In order to unit test this function, I need to generate an error which emulates as perfectly as possible the actual error received at runtime.In Go SDK v1 this was easy:
awserr.New()
. The migration guide's section on error handling as well as the Go SDK v2 error handling guide say that the Smithy-Go error wrapping is intended to completely replace theawserr
package. I can find guidance on how to check these errors but unfortunately haven't found any yet on how to create or emulate them.Code Comparison
Function snippet following the migration guide for error unwrapping and taking distinct action for a particular error code:
also tried the simpler case to the CWL type directly, with effectively the same results:
V1 test code which has worked fine:
V2 test code returning a custom struct which ought to satisfy Smithy-Go, but unfortunately not working:
also tried logging an SDK v2 error in an isolated environment, and copying-pasting its error string to
errors.New()
:Observed Differences/Errors
In both of the above V2 variations, the code yields the
log.Error("did not satisfy...")
instead of thelog.Info("swallowing error")
as expected. Ultimately I'm no longer able to fully unit test this function with V2 now thatawserr.New()
has gone away.Additional Context
No response
The text was updated successfully, but these errors were encountered: