-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from go-admin-team/dev
核型库基本成型
- Loading branch information
Showing
24 changed files
with
1,247 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ | |
|
||
# idea | ||
/.idea | ||
|
||
/plugins/logger/zap/testdata/* |
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,23 @@ | ||
package errors | ||
|
||
import "net/http" | ||
|
||
//go:generate stringer -type ErrorCode -output error_code_string.go | ||
|
||
type ErrorCode int32 | ||
|
||
const ( | ||
OK ErrorCode = http.StatusOK | ||
BadRequest ErrorCode = http.StatusBadRequest | ||
Unauthorized ErrorCode = http.StatusUnauthorized | ||
Forbidden ErrorCode = http.StatusForbidden | ||
NotFound ErrorCode = http.StatusNotFound | ||
MethodNotAllowed ErrorCode = http.StatusMethodNotAllowed | ||
Timeout ErrorCode = http.StatusRequestTimeout | ||
Conflict ErrorCode = http.StatusConflict | ||
InternalServerError ErrorCode = http.StatusInternalServerError | ||
) | ||
|
||
func (e ErrorCode) Code() int32 { | ||
return int32(e) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,64 @@ | ||
// Package errors provides a way to return detailed information | ||
// for an RPC request error. The error is normally JSON encoded. | ||
package errors | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
//go:generate protoc -I. --go_out=paths=source_relative:. errors.proto | ||
|
||
func (e *Error) Error() string { | ||
b, _ := json.Marshal(e) | ||
return string(b) | ||
} | ||
|
||
// New generates a custom error. | ||
func New(id, msg string, code ErrorCoder) error { | ||
return &Error{ | ||
RequestId: id, | ||
Code: code.Code(), | ||
Msg: msg, | ||
Status: code.String(), | ||
} | ||
} | ||
|
||
// Parse tries to parse a JSON string into an error. If that | ||
// fails, it will set the given string as the error detail. | ||
func Parse(err string) *Error { | ||
e := new(Error) | ||
errr := json.Unmarshal([]byte(err), e) | ||
if errr != nil { | ||
e.Msg = err | ||
} | ||
return e | ||
} | ||
|
||
// Equal tries to compare errors | ||
func Equal(err1 error, err2 error) bool { | ||
verr1, ok1 := err1.(*Error) | ||
verr2, ok2 := err2.(*Error) | ||
|
||
if ok1 != ok2 { | ||
return false | ||
} | ||
|
||
if !ok1 { | ||
return err1 == err2 | ||
} | ||
|
||
if verr1.Code != verr2.Code { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// FromError try to convert go error to *Error | ||
func FromError(err error) *Error { | ||
if verr, ok := err.(*Error); ok && verr != nil { | ||
return verr | ||
} | ||
|
||
return Parse(err.Error()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
syntax = "proto3"; | ||
|
||
package errors; | ||
|
||
option go_package = "github.com/go-admin-team/go-admin-core/errors"; | ||
|
||
message Error { | ||
string requestId = 1; | ||
int32 code = 2; | ||
string msg = 3; | ||
string status = 4; | ||
}; |
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,7 @@ | ||
package errors | ||
|
||
// ErrorCoder error code | ||
type ErrorCoder interface { | ||
String() string | ||
Code() int32 | ||
} |
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.