-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
treat etag errors across all state stores
- Loading branch information
Showing
11 changed files
with
175 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// ------------------------------------------------------------ | ||
|
||
package state | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type ETagErrorKind string | ||
|
||
const ( | ||
mismatchPrefix = "possible etag mismatch. error from state store" | ||
invalidPrefix = "invalid etag value" | ||
|
||
ETagInvalid ETagErrorKind = "invalid" | ||
ETagMismatch ETagErrorKind = "mismatch" | ||
) | ||
|
||
// ETagError is a custom error type for etag exceptions | ||
type ETagError struct { | ||
err error | ||
kind ETagErrorKind | ||
} | ||
|
||
func (e *ETagError) Error() string { | ||
var prefix string | ||
|
||
switch e.kind { | ||
case ETagInvalid: | ||
prefix = invalidPrefix | ||
case ETagMismatch: | ||
prefix = mismatchPrefix | ||
} | ||
|
||
if e.err != nil { | ||
return fmt.Sprintf("%s: %s", prefix, e.err) | ||
} | ||
|
||
return errors.New(prefix).Error() | ||
} | ||
|
||
// NewETagError returns an ETagError wrapping an existing context error | ||
func NewETagError(kind ETagErrorKind, err error) *ETagError { | ||
return &ETagError{ | ||
err: err, | ||
kind: kind, | ||
} | ||
} |
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,41 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// ------------------------------------------------------------ | ||
|
||
package state | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestETagError(t *testing.T) { | ||
t.Run("invalid with context error", func(t *testing.T) { | ||
cerr := errors.New("error1") | ||
err := NewETagError(ETagInvalid, cerr) | ||
|
||
assert.Equal(t, invalidPrefix+": error1", err.Error()) | ||
}) | ||
|
||
t.Run("invalid without context error", func(t *testing.T) { | ||
err := NewETagError(ETagInvalid, nil) | ||
|
||
assert.Equal(t, invalidPrefix, err.Error()) | ||
}) | ||
|
||
t.Run("mismatch with context error", func(t *testing.T) { | ||
cerr := errors.New("error1") | ||
err := NewETagError(ETagMismatch, cerr) | ||
|
||
assert.Equal(t, mismatchPrefix+": error1", err.Error()) | ||
}) | ||
|
||
t.Run("mismatch without context error", func(t *testing.T) { | ||
err := NewETagError(ETagMismatch, nil) | ||
|
||
assert.Equal(t, mismatchPrefix, err.Error()) | ||
}) | ||
} |
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