Skip to content

Commit

Permalink
Merge pull request #3 from go-admin-team/dev
Browse files Browse the repository at this point in the history
核型库基本成型
  • Loading branch information
mss-boot authored Mar 8, 2021
2 parents 82fce0d + 503b489 commit 1af6382
Show file tree
Hide file tree
Showing 24 changed files with 1,247 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

# idea
/.idea

/plugins/logger/zap/testdata/*
23 changes: 23 additions & 0 deletions errors/error_code.go
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)
}
54 changes: 54 additions & 0 deletions errors/error_code_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions errors/errors.go
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())
}
177 changes: 177 additions & 0 deletions errors/errors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions errors/errors.proto
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;
};
7 changes: 7 additions & 0 deletions errors/type.go
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
}
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ require (
github.com/bitly/go-simplejson v0.5.0
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/bsm/redislock v0.5.0
github.com/casbin/casbin/v2 v2.24.0
github.com/fsnotify/fsnotify v1.4.7
github.com/ghodss/yaml v1.0.0
github.com/gin-gonic/gin v1.6.3
github.com/go-redis/redis/v7 v7.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/golang/protobuf v1.4.2 // indirect
github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.2
github.com/imdario/mergo v0.3.9
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/robinjoseph08/redisqueue/v2 v2.1.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/cast v1.3.1
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/image v0.0.0-20201208152932-35266b937fa6 // indirect
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 // indirect
google.golang.org/protobuf v1.25.0 // indirect
golang.org/x/tools v0.0.0-20210115202250-e0d201561e39 // indirect
google.golang.org/protobuf v1.25.0
gorm.io/driver/mysql v1.0.3
gorm.io/gorm v1.20.12
gorm.io/plugin/dbresolver v1.1.0
)
Loading

0 comments on commit 1af6382

Please sign in to comment.