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
Hide 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
...
@@ -2,8 +2,8 @@ package errorx
import
(
import
(
"fmt"
"fmt"
"g
ithub.com/go-playground/validator/v10
"
"g
o-micro.dev/v4/errors
"
"go
kratos
-base/common/validate"
"go
micro
-base/common/validate"
)
)
const
(
const
(
...
@@ -22,36 +22,22 @@ func (c *ResponseError) Error() string {
...
@@ -22,36 +22,22 @@ func (c *ResponseError) Error() string {
return
fmt
.
Sprintf
(
"[%d]%s"
,
c
.
Code
,
c
.
Msg
)
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
var
c
=
DefaultError
if
len
(
code
)
>
0
{
if
len
(
code
)
>
0
{
c
=
code
[
0
]
c
=
code
[
0
]
}
}
e
:=
&
ResponseError
{
return
&
errors
.
Error
{
Code
:
c
,
Code
:
int32
(
c
),
Msg
:
message
,
Detail
:
detail
,
Status
:
"操作失败"
,
}
}
return
e
}
}
func
ParseError
(
err
error
)
(
errorCode
int
,
msg
string
)
{
func
NewParamsError
(
err
error
)
error
{
switch
v
:=
err
.
(
type
)
{
return
&
errors
.
Error
{
case
*
ResponseError
:
Code
:
int32
(
ParamsError
),
errorCode
=
v
.
Code
Detail
:
validate
.
TransError
(
err
),
msg
=
v
.
Msg
Status
:
"参数不正确"
,
case
validator
.
ValidationErrors
:
errorCode
=
ParamsError
if
len
(
v
)
>
0
{
msg
=
v
[
0
]
.
Translate
(
validate
.
GrtTrans
())
}
else
{
msg
=
v
.
Error
()
}
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
...
@@ -9,8 +9,8 @@ import (
...
@@ -9,8 +9,8 @@ import (
)
)
var
(
var
(
vt
*
validator
.
Validate
vt
*
validator
.
Validate
tr
ut
.
Translator
tr
ans
ut
.
Translator
)
)
func
newTranslate
(
v
*
validator
.
Validate
)
ut
.
Translator
{
func
newTranslate
(
v
*
validator
.
Validate
)
ut
.
Translator
{
...
@@ -18,20 +18,36 @@ func newTranslate(v *validator.Validate) ut.Translator {
...
@@ -18,20 +18,36 @@ func newTranslate(v *validator.Validate) ut.Translator {
enT
:=
en
.
New
()
enT
:=
en
.
New
()
uni
:=
ut
.
New
(
enT
,
zhT
)
uni
:=
ut
.
New
(
enT
,
zhT
)
tr
ans
,
_
:=
uni
.
GetTranslator
(
"zh"
)
tr
,
_
:=
uni
.
GetTranslator
(
"zh"
)
_
=
zhTranslations
.
RegisterDefaultTranslations
(
v
,
tr
ans
)
_
=
zhTranslations
.
RegisterDefaultTranslations
(
v
,
tr
)
return
tr
ans
return
tr
}
}
func
init
()
{
func
init
()
{
vt
=
validator
.
New
()
vt
=
validator
.
New
()
tr
=
newTranslate
(
vt
)
tr
ans
=
newTranslate
(
vt
)
}
}
func
GrtTrans
()
ut
.
Translator
{
func
GrtTrans
()
ut
.
Translator
{
return
tr
return
tr
ans
}
}
func
Struct
(
data
interface
{})
error
{
func
Struct
(
data
interface
{})
error
{
return
vt
.
Struct
(
data
)
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