Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gomicro-base
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lizifeng
gomicro-base
Commits
69953656
Commit
69953656
authored
Jun 10, 2022
by
kzkzzzz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 修改错误
parent
8a2cccea
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
95 deletions
+35
-95
error.go
common/errorx/error.go
+12
-26
encode.go
common/resutil/encode.go
+0
-36
response.go
common/resutil/response.go
+0
-26
trans.go
common/validate/trans.go
+23
-7
No files found.
common/errorx/error.go
View file @
69953656
...
...
@@ -2,8 +2,8 @@ package errorx
import
(
"fmt"
"g
ithub.com/go-playground/validator/v10
"
"go
kratos
-base/common/validate"
"g
o-micro.dev/v4/errors
"
"go
micro
-base/common/validate"
)
const
(
...
...
@@ -22,36 +22,22 @@ func (c *ResponseError) Error() string {
return
fmt
.
Sprintf
(
"[%d]%s"
,
c
.
Code
,
c
.
Msg
)
}
func
New
Error
(
message
string
,
code
...
int
)
error
{
func
New
MicroError
(
detail
string
,
code
...
int
)
error
{
var
c
=
DefaultError
if
len
(
code
)
>
0
{
c
=
code
[
0
]
}
e
:=
&
ResponseError
{
Code
:
c
,
Msg
:
message
,
return
&
errors
.
Error
{
Code
:
int32
(
c
),
Detail
:
detail
,
Status
:
"操作失败"
,
}
return
e
}
func
ParseError
(
err
error
)
(
errorCode
int
,
msg
string
)
{
switch
v
:=
err
.
(
type
)
{
case
*
ResponseError
:
errorCode
=
v
.
Code
msg
=
v
.
Msg
case
validator
.
ValidationErrors
:
errorCode
=
ParamsError
if
len
(
v
)
>
0
{
msg
=
v
[
0
]
.
Translate
(
validate
.
GrtTrans
())
}
else
{
msg
=
v
.
Error
()
func
NewParamsError
(
err
error
)
error
{
return
&
errors
.
Error
{
Code
:
int32
(
ParamsError
),
Detail
:
validate
.
TransError
(
err
),
Status
:
"参数不正确"
,
}
case
validator
.
FieldError
:
errorCode
=
ParamsError
msg
=
v
.
Translate
(
validate
.
GrtTrans
())
default
:
errorCode
=
DefaultError
msg
=
err
.
Error
()
}
return
}
common/resutil/encode.go
deleted
100644 → 0
View file @
8a2cccea
package
resutil
import
(
"encoding/json"
"github.com/go-kratos/kratos/v2/log"
"net/http"
)
func
HttpResponseEncoder
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
v
interface
{})
error
{
if
v
==
nil
{
_
,
err
:=
w
.
Write
(
nil
)
return
err
}
data
,
err
:=
json
.
Marshal
(
MakeHttpSuccess
(
v
))
if
err
!=
nil
{
return
err
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
_
,
err
=
w
.
Write
(
data
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
HttpErrorEncoder
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
err
error
)
{
data
,
err
:=
json
.
Marshal
(
MakeHttpError
(
err
))
if
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
log
.
Errorf
(
"HttpErrorEncoder json编码失败: %s"
,
err
)
return
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
_
,
_
=
w
.
Write
(
data
)
}
common/resutil/response.go
deleted
100644 → 0
View file @
8a2cccea
package
resutil
import
(
"gokratos-base/common/errorx"
)
type
HttpResponse
struct
{
Code
int
`json:"code"`
Msg
string
`json:"msg"`
Data
interface
{}
`json:"data,omitempty"`
}
func
MakeHttpSuccess
(
data
interface
{})
(
r
*
HttpResponse
)
{
r
=
&
HttpResponse
{
Code
:
1
,
Msg
:
"ok"
,
Data
:
data
,
}
return
}
func
MakeHttpError
(
err
error
)
(
r
*
HttpResponse
)
{
r
=
&
HttpResponse
{}
r
.
Code
,
r
.
Msg
=
errorx
.
ParseError
(
err
)
return
}
common/validate/trans.go
View file @
69953656
...
...
@@ -10,7 +10,7 @@ import (
var
(
vt
*
validator
.
Validate
tr
ut
.
Translator
tr
ans
ut
.
Translator
)
func
newTranslate
(
v
*
validator
.
Validate
)
ut
.
Translator
{
...
...
@@ -18,20 +18,36 @@ func newTranslate(v *validator.Validate) ut.Translator {
enT
:=
en
.
New
()
uni
:=
ut
.
New
(
enT
,
zhT
)
tr
ans
,
_
:=
uni
.
GetTranslator
(
"zh"
)
_
=
zhTranslations
.
RegisterDefaultTranslations
(
v
,
tr
ans
)
return
tr
ans
tr
,
_
:=
uni
.
GetTranslator
(
"zh"
)
_
=
zhTranslations
.
RegisterDefaultTranslations
(
v
,
tr
)
return
tr
}
func
init
()
{
vt
=
validator
.
New
()
tr
=
newTranslate
(
vt
)
tr
ans
=
newTranslate
(
vt
)
}
func
GrtTrans
()
ut
.
Translator
{
return
tr
return
tr
ans
}
func
Struct
(
data
interface
{})
error
{
return
vt
.
Struct
(
data
)
}
func
TransError
(
err
error
)
(
msg
string
)
{
switch
v
:=
err
.
(
type
)
{
case
validator
.
ValidationErrors
:
if
len
(
v
)
>
0
{
msg
=
v
[
0
]
.
Translate
(
trans
)
}
else
{
msg
=
v
.
Error
()
}
case
validator
.
FieldError
:
msg
=
v
.
Translate
(
trans
)
default
:
msg
=
err
.
Error
()
}
return
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment