Skip to content

Commit 08c1958

Browse files
committed
feat: unified error code; separate application handler and service into parts
1 parent ca9fb03 commit 08c1958

File tree

13 files changed

+578
-437
lines changed

13 files changed

+578
-437
lines changed

pkg/common/bizerror/common.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,15 @@
1818
package bizerror
1919

2020
import (
21-
"errors"
2221
"fmt"
2322
)
2423

25-
type AssertionError struct {
26-
msg string
24+
func NewAssertionError(expected, actual interface{}) Error {
25+
return NewBizError(UnknownError, fmt.Sprintf("type assertion error, expected:%v, actual:%v", expected, actual))
2726
}
28-
29-
func NewAssertionError(expected, actual interface{}) error {
30-
return &AssertionError{
31-
msg: fmt.Sprintf("type assertion error, expected:%v, actual:%v", expected, actual),
32-
}
33-
}
34-
35-
func (e *AssertionError) Error() string {
36-
return e.msg
37-
}
38-
39-
type MeshNotFoundError struct {
40-
Mesh string
27+
func NewUnauthorizedError() Error {
28+
return NewBizError(Unauthorized, "no access, please login")
4129
}
42-
43-
func (m *MeshNotFoundError) Error() string {
44-
return fmt.Sprintf("mesh of name %s is not found", m.Mesh)
45-
}
46-
47-
func MeshNotFound(meshName string) error {
48-
return &MeshNotFoundError{meshName}
49-
}
50-
51-
func IsMeshNotFound(err error) bool {
52-
var meshNotFoundError *MeshNotFoundError
53-
ok := errors.As(err, &meshNotFoundError)
54-
return ok
30+
func MeshNotFoundError(mesh string) Error {
31+
return NewBizError(UnknownError, fmt.Sprintf("mesh of name %s is not found", mesh))
5532
}

pkg/common/bizerror/error.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package bizerror
2+
3+
type Error interface {
4+
Code() ErrorCode
5+
Message() string
6+
Error() string
7+
}
8+
9+
type ErrorCode string
10+
11+
const (
12+
UnknownError ErrorCode = "UnknownError"
13+
InvalidArgument ErrorCode = "InvalidArgument"
14+
StoreError ErrorCode = "StoreError"
15+
AppNotFound ErrorCode = "AppNotFound"
16+
Unauthorized ErrorCode = "Unauthorized"
17+
SessionError ErrorCode = "SessionError"
18+
)
19+
20+
type bizError struct {
21+
code ErrorCode
22+
message string
23+
}
24+
25+
var _ Error = &bizError{}
26+
27+
func NewBizError(code ErrorCode, message string) Error {
28+
return &bizError{
29+
code: code,
30+
message: message,
31+
}
32+
}
33+
34+
func (b *bizError) Code() ErrorCode {
35+
return b.code
36+
}
37+
38+
func (b *bizError) Message() string {
39+
return b.message
40+
}
41+
42+
func (b *bizError) Error() string {
43+
return string(b.code) + ": " + b.message
44+
}

pkg/console/component.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/gin-gonic/gin"
3131

3232
ui "github.com/apache/dubbo-admin/app/dubbo-ui"
33+
"github.com/apache/dubbo-admin/pkg/common/bizerror"
3334
"github.com/apache/dubbo-admin/pkg/config/console"
3435
consolectx "github.com/apache/dubbo-admin/pkg/console/context"
3536
"github.com/apache/dubbo-admin/pkg/console/model"
@@ -130,7 +131,8 @@ func (c *consoleWebServer) authMiddleware() gin.HandlerFunc {
130131
session := sessions.Default(c)
131132
user := session.Get("user")
132133
if user == nil {
133-
c.JSON(http.StatusUnauthorized, model.NewUnauthorizedResp())
134+
err := bizerror.NewAssertionError(bizerror.UnknownError, "please login")
135+
c.JSON(http.StatusUnauthorized, model.NewBizErrorResp(err))
134136
c.Abort()
135137
return
136138
}

0 commit comments

Comments
 (0)