mirror of https://github.com/grafana/grafana
Unified Storage: Return an already exists error (#102857)
* Unified Storage: Return an already exists error
When inserting a resource that already exists (i.e. race condition), we can safely catch UNIQUE constraint violations
and transform them into a `k8s.io/apimachinery/pkg/api/errors` error that stands the test of `errors.IsAlreadyExists`.
* feat: clarify existing conflict error
* chore: make update-workspace
* feat: make new package for backend error
* fix: assign dependency owner
* feat: use dialect for checking error type
* chore: go generate
* revert: to 5af369166d
pull/102070/head^2
parent
9f7df8b788
commit
f7b9f1ce69
@ -0,0 +1,21 @@ |
||||
package backend |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors" |
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
||||
) |
||||
|
||||
// Errors that may be returned by the storage backend, to be converted by the resource layer.
|
||||
// These are defined here to let all storage backends use the same error types without depending on one another.
|
||||
var ( |
||||
ErrResourceAlreadyExists error = &apierrors.StatusError{ |
||||
ErrStatus: metav1.Status{ |
||||
Status: metav1.StatusFailure, |
||||
Reason: metav1.StatusReasonAlreadyExists, |
||||
Message: "the resource already exists", |
||||
Code: http.StatusConflict, |
||||
}, |
||||
} |
||||
) |
||||
@ -0,0 +1,14 @@ |
||||
package backend |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
apierrors "k8s.io/apimachinery/pkg/api/errors" |
||||
) |
||||
|
||||
func TestErrResourceAlreadyExistsIsRecognisable(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
require.True(t, apierrors.IsAlreadyExists(ErrResourceAlreadyExists), "ErrResourceAlreadyExists should be recognised as an AlreadyExists error") |
||||
} |
||||
Loading…
Reference in new issue