1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-05-22 10:15:24 +02:00

examples: update mod (#862)

* fix examples errors
This commit is contained in:
Tony Chen
2021-04-25 22:26:31 +08:00
committed by GitHub
parent 9806191b7f
commit 3780f70c91
17 changed files with 71 additions and 97 deletions
+7 -4
View File
@@ -31,19 +31,22 @@ func (e *Error) WithMetadata(md map[string]string) *Error {
// Is matches each error in the chain with the target value.
func (e *Error) Is(err error) bool {
if target := new(Error); errors.As(err, &target) {
return target.Code == e.Code && target.Reason == e.Reason
return target.Code == e.Code &&
target.Domain == e.Domain &&
target.Reason == e.Reason
}
return false
}
func (e *Error) Error() string {
return fmt.Sprintf("error: code = %d domain = %s reason = %s message = %s metadata = %v", e.Code, e.Domain, e.Reason, e.Message, e.Metadata)
return fmt.Sprintf("error: code = %d domain = %s reason = %s message = %s", e.Code, e.Domain, e.Reason, e.Message)
}
// New returns an error object for the code, message and error info.
func New(code int, reason, message string) *Error {
func New(code int, domain, reason, message string) *Error {
return &Error{
Code: code,
Domain: domain,
Reason: reason,
Message: message,
}
@@ -85,5 +88,5 @@ func FromError(err error) *Error {
if target := new(Error); errors.As(err, &target) {
return target
}
return New(http.StatusInternalServerError, "", err.Error())
return New(http.StatusInternalServerError, "", "", err.Error())
}
+2 -2
View File
@@ -10,8 +10,8 @@ func TestError(t *testing.T) {
var (
base *Error
)
err := New(400, "reason", "message")
err2 := New(400, "reason", "message")
err := New(400, "domain", "reason", "message")
err2 := New(400, "domain", "reason", "message")
err3 := err.WithMetadata(map[string]string{
"foo": "bar",
})
+14 -14
View File
@@ -3,8 +3,8 @@ package errors
import "net/http"
// BadRequest new BadRequest error that is mapped to a 400 response.
func BadRequest(reason, message string) *Error {
return New(http.StatusBadRequest, reason, message)
func BadRequest(domain, reason, message string) *Error {
return New(http.StatusBadRequest, domain, reason, message)
}
// IsBadRequest determines if err is an error which indicates a BadRequest error.
@@ -14,8 +14,8 @@ func IsBadRequest(err error) bool {
}
// Unauthorized new Unauthorized error that is mapped to a 401 response.
func Unauthorized(reason, message string) *Error {
return New(http.StatusUnauthorized, reason, message)
func Unauthorized(domain, reason, message string) *Error {
return New(http.StatusUnauthorized, domain, reason, message)
}
// IsUnauthorized determines if err is an error which indicates a Unauthorized error.
@@ -25,8 +25,8 @@ func IsUnauthorized(err error) bool {
}
// Forbidden new Forbidden error that is mapped to a 403 response.
func Forbidden(reason, message string) *Error {
return New(http.StatusForbidden, reason, message)
func Forbidden(domain, reason, message string) *Error {
return New(http.StatusForbidden, domain, reason, message)
}
// IsForbidden determines if err is an error which indicates a Forbidden error.
@@ -36,8 +36,8 @@ func IsForbidden(err error) bool {
}
// NotFound new NotFound error that is mapped to a 404 response.
func NotFound(reason, message string) *Error {
return New(http.StatusNotFound, reason, message)
func NotFound(domain, reason, message string) *Error {
return New(http.StatusNotFound, domain, reason, message)
}
// IsNotFound determines if err is an error which indicates an NotFound error.
@@ -47,8 +47,8 @@ func IsNotFound(err error) bool {
}
// Conflict new Conflict error that is mapped to a 409 response.
func Conflict(reason, message string) *Error {
return New(http.StatusConflict, reason, message)
func Conflict(domain, reason, message string) *Error {
return New(http.StatusConflict, domain, reason, message)
}
// IsConflict determines if err is an error which indicates a Conflict error.
@@ -58,8 +58,8 @@ func IsConflict(err error) bool {
}
// InternalServer new InternalServer error that is mapped to a 500 response.
func InternalServer(reason, message string) *Error {
return New(http.StatusInternalServerError, reason, message)
func InternalServer(domain, reason, message string) *Error {
return New(http.StatusInternalServerError, domain, reason, message)
}
// IsInternalServer determines if err is an error which indicates an InternalServer error.
@@ -69,8 +69,8 @@ func IsInternalServer(err error) bool {
}
// ServiceUnavailable new ServiceUnavailable error that is mapped to a HTTP 503 response.
func ServiceUnavailable(reason, message string) *Error {
return New(http.StatusServiceUnavailable, reason, message)
func ServiceUnavailable(domain, reason, message string) *Error {
return New(http.StatusServiceUnavailable, domain, reason, message)
}
// IsServiceUnavailable determines if err is an error which indicates a ServiceUnavailable error.
+7 -7
View File
@@ -5,13 +5,13 @@ import "testing"
func TestTypes(t *testing.T) {
var (
input = []*Error{
BadRequest("reason_400", "message_400"),
Unauthorized("reason_401", "message_401"),
Forbidden("reason_403", "message_403"),
NotFound("reason_404", "message_404"),
Conflict("reason_409", "message_409"),
InternalServer("reason_500", "message_500"),
ServiceUnavailable("reason_503", "message_503"),
BadRequest("domain_400", "reason_400", "message_400"),
Unauthorized("domain_401", "reason_401", "message_401"),
Forbidden("domain_403", "reason_403", "message_403"),
NotFound("domain_404", "reason_404", "message_404"),
Conflict("domain_409", "reason_409", "message_409"),
InternalServer("domain_500", "reason_500", "message_500"),
ServiceUnavailable("domain_503", "reason_503", "message_503"),
}
output = []func(error) bool{
IsBadRequest,