Describe the bug
An interesting bug that stems from the fact that diag.Diagnostics implements the error interface (i.e. func Error() string {:
both diag.BaseError and diag.Diagnostics (i.e. a slice of diagnostics []diag.Diagnostics ) implement the error interface:
type error interface {
Error() string
}
This means that, if a function returns diag.Diagnostics , but thinks it returns an error, it will be implicitly converted. And thus, even though the function returned nil, the caller will actually see error: no errors.
To reproduce:
func Test_DiagAndErrors(t *testing.T) {
var err error
err = functionReturningDiags0()
if err != nil {
fmt.Printf("error: %vֿ\n", err)
}
err = functionReturningDiagsNil()
if err != nil {
fmt.Printf("error: %vֿ\n", err)
}
}
func functionReturningDiagsNil() diag.Diagnostics {
return nil
}
func functionReturningDiags0() diag.Diagnostics {
return diag.Diagnostics{}
}
Output:
error: no errorsֿ
error: no errorsֿ
Insights:
A customer complained about error: no errors in discord a couple of weeks ago (in relation to policies).
implicit conversion is bad! The code for converting Diagnostics to errors makes me believe that this is a side effect that the writer did not intend.
|
func (diags Diagnostics) Error() string { |
My proposal would be to rename Diagnostics.Error() to Diagnostics.ToError() to avoid implementing the error interface and allowing this implicit conversion.
Expected Behavior
Steps to Reproduce
Possible Solution
No response
Provider and CloudQuery version
0.24.7
Additional Context
No response
Describe the bug
An interesting bug that stems from the fact that
diag.Diagnosticsimplements theerrorinterface (i.e.func Error() string {:both
diag.BaseErroranddiag.Diagnostics(i.e. a slice of diagnostics[]diag.Diagnostics) implement the error interface:This means that, if a function returns
diag.Diagnostics, but thinks it returns anerror, it will be implicitly converted. And thus, even though the function returned nil, the caller will actually seeerror: no errors.To reproduce:
Output:
Insights:
A customer complained about
error: no errorsin discord a couple of weeks ago (in relation to policies).implicit conversion is bad! The code for converting
Diagnosticsto errors makes me believe that this is a side effect that the writer did not intend.cq-provider-sdk/provider/diag/diagnostics.go
Line 18 in 22a7afb
My proposal would be to rename Diagnostics.Error() to Diagnostics.ToError() to avoid implementing the error interface and allowing this implicit conversion.
Expected Behavior
Steps to Reproduce
Possible Solution
No response
Provider and CloudQuery version
0.24.7
Additional Context
No response