-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a099980
commit 2f444ea
Showing
18 changed files
with
156 additions
and
212 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
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 |
---|---|---|
@@ -1,54 +1,43 @@ | ||
package storage | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/lib/pq" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
ErrConfigurationNotFound = errors.New("configuration not found") | ||
ErrNotFound = errors.New("not found") | ||
ErrStoreNotInitialized = errors.New("store not initialized") | ||
) | ||
|
||
type Code string | ||
|
||
const ( | ||
ConstraintFailed Code = "CONSTRAINT_FAILED" | ||
TooManyClient Code = "TOO_MANY_CLIENT" | ||
var ( | ||
// Specific pq sql errors | ||
ErrConstraintFailed = pq.ErrorCode("23505") | ||
ErrTooManyClient = pq.ErrorCode("53300") | ||
) | ||
|
||
type Error struct { | ||
Code Code | ||
OriginalError error | ||
func IsNotFound(err error) bool { | ||
return errors.Is(err, ErrNotFound) | ||
} | ||
|
||
func (e Error) Is(err error) bool { | ||
storageErr, ok := err.(*Error) | ||
if !ok { | ||
return false | ||
} | ||
if storageErr.Code == "" { | ||
return true | ||
} | ||
return storageErr.Code == e.Code | ||
} | ||
|
||
func (e Error) Error() string { | ||
return fmt.Sprintf("%s [%s]", e.OriginalError, e.Code) | ||
type Error struct { | ||
code pq.ErrorCode | ||
err error | ||
} | ||
|
||
func NewError(code Code, originalError error) *Error { | ||
func NewError(code pq.ErrorCode, err error) *Error { | ||
return &Error{ | ||
Code: code, | ||
OriginalError: originalError, | ||
code: code, | ||
err: err, | ||
} | ||
} | ||
|
||
func IsError(err error) bool { | ||
return IsErrorCode(err, "") | ||
func (e *Error) Error() string { | ||
return fmt.Sprintf("[%s] %s", e.code, e.err) | ||
} | ||
|
||
func IsErrorCode(err error, code Code) bool { | ||
return errors.Is(err, &Error{ | ||
Code: code, | ||
}) | ||
func IsError(err error) bool { | ||
return errors.Is(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
Oops, something went wrong.