Skip to content

Commit

Permalink
Merge pull request #16 from tigh-latte/main
Browse files Browse the repository at this point in the history
Feature: `Conflict`
  • Loading branch information
mergify[bot] committed Feb 19, 2024
2 parents 6a3e3af + fb39af9 commit cd5ea13
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
34 changes: 34 additions & 0 deletions errs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,37 @@ func NewErrTooManyRequestsf(code, detail string, a ...interface{}) ErrTooManyReq
func (e ErrTooManyRequests) TooManyRequests() bool {
return true
}

// ErrConflict can be returned if you reach a condition
// where the system is in conflict with a user's request.
type ErrConflict struct {
ErrClient
}

// Conflict we understand the request, it is valid,
// but we are unable to process this request due to a conflict.
func (e ErrConflict) Conflict() bool {
return true
}

// NewErrConflict will create and return a new Conflict error.
// You can supply a code which can be set in your application to identify
// a particular error in code such as C001.
// Detail can be supplied to give more context to the error, ie
// "entity already exists".
func NewErrConflict(code, detail string) ErrConflict {
c := newErrClient(code, detail)
c.title = "Conflict"
return ErrConflict{
ErrClient: c,
}
}

// NewErrConflictf will create and return a new Conflict error.
// You can supply a code which can be set in your application to identify
// a particular error in code such as C001.
// Detail can be supplied to give more context to the error, ie
// "entity already exists".
func NewErrConflictf(code, detail string, a ...interface{}) ErrConflict {
return NewErrConflict(code, fmt.Sprintf(detail, a...))
}
4 changes: 4 additions & 0 deletions errs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func Test_FmtString(t *testing.T) {
err: NewErrTooManyRequestsf("test", "test %s", "format"),
expErr: errors.New("Too many requests: test format"),
},
"err conflict": {
err: NewErrConflictf("test", "test %s", "format"),
expErr: errors.New("Conflict: test format"),
},
}

for name, test := range tests {
Expand Down
12 changes: 12 additions & 0 deletions lathos.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,15 @@ func IsTooManyRequests(err error) bool {
var t TooManyRequests
return errors.As(err, &t)
}

// Conflict when implemented will indicate that the request cannot be completed
// due to a conflict with the current state of the resource.
type Conflict interface {
Conflict() bool
}

// IsConflict will check if this is a conflict error.
func IsConflict(err error) bool {
var t Conflict
return errors.As(err, &t)
}

0 comments on commit cd5ea13

Please sign in to comment.