Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Fix "error: no errors" #312

@shimonp21

Description

@shimonp21

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions