Skip to content

Commit 4aae206

Browse files
authored
feat: Defined a unified error (#1353)
* feat: unified error code; separate application handler and service into parts * fix: license header lack * fix: copilot review err fix * fix: simplify the if-else condition * chore: rename Error() to String() * chore: add extra String() in Error * feat: 新增listMeshes接口 * fix: copilot review
1 parent ca9fb03 commit 4aae206

File tree

17 files changed

+751
-487
lines changed

17 files changed

+751
-487
lines changed

app/dubbo-admin/dubbo-admin.yaml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,23 @@ console:
3939
store:
4040
type: memory
4141
discovery:
42-
- type: nacos
43-
id: nacos-44.33
44-
address:
45-
registry: nacos://47.76.94.134:8848?username=nacos&password=nacos
46-
configCenter: nacos://47.76.94.134:8848?username=nacos&password=nacos
47-
metadataReport: nacos://47.76.94.134:8848?username=nacos&password=nacos
48-
49-
- type: etcd
50-
id: etcd-44.33
51-
address: http://127.0.0.1:2379
42+
# - type: nacos
43+
# name: nacos-44.33
44+
# address:
45+
# registry: nacos://47.76.94.134:8848?username=nacos&password=nacos
46+
# configCenter: nacos://47.76.94.134:8848?username=nacos&password=nacos
47+
# metadataReport: nacos://47.76.94.134:8848?username=nacos&password=nacos
48+
#
49+
# - type: etcd
50+
# name: etcd-44.33
51+
# address:
52+
# registry: http://127.0.0.1:2379
53+
# configCenter: http://127.0.0.1:2379
54+
# metadataReport: http://127.0.0.1:2379
5255

5356
# mock discovery is only for development
5457
- type: mock
58+
name: mockRegistry
5559
engine:
5660
name: k8s1.28.6
5761
type: kubernetes

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package bizerror
19+
20+
type Error interface {
21+
Code() ErrorCode
22+
Message() string
23+
Error() string
24+
String() string
25+
}
26+
27+
type ErrorCode string
28+
29+
const (
30+
UnknownError ErrorCode = "UnknownError"
31+
InvalidArgument ErrorCode = "InvalidArgument"
32+
StoreError ErrorCode = "StoreError"
33+
AppNotFound ErrorCode = "AppNotFound"
34+
Unauthorized ErrorCode = "Unauthorized"
35+
SessionError ErrorCode = "SessionError"
36+
)
37+
38+
type bizError struct {
39+
code ErrorCode
40+
message string
41+
}
42+
43+
var _ Error = &bizError{}
44+
45+
func NewBizError(code ErrorCode, message string) Error {
46+
return &bizError{
47+
code: code,
48+
message: message,
49+
}
50+
}
51+
52+
func (b *bizError) Code() ErrorCode {
53+
return b.code
54+
}
55+
56+
func (b *bizError) Message() string {
57+
return b.message
58+
}
59+
60+
func (b *bizError) Error() string {
61+
return b.String()
62+
}
63+
64+
func (b *bizError) String() string {
65+
return string(b.code) + ": " + b.message
66+
}

pkg/config/discovery/config.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717

1818
package discovery
1919

20-
import "github.com/apache/dubbo-admin/pkg/config"
20+
import (
21+
"github.com/duke-git/lancet/v2/strutil"
22+
23+
"github.com/apache/dubbo-admin/pkg/common/bizerror"
24+
"github.com/apache/dubbo-admin/pkg/config"
25+
)
2126

2227
type Type string
2328

@@ -31,9 +36,9 @@ const (
3136
// Config defines Discovery configuration
3237
type Config struct {
3338
config.BaseConfig
34-
Name string `json:"name"`
35-
Type Type `json:"type"`
36-
Address AddressConfig
39+
Name string `json:"name"`
40+
Type Type `json:"type"`
41+
Address AddressConfig `json:"address"`
3742
}
3843

3944
// AddressConfig defines Discovery Engine address
@@ -54,3 +59,13 @@ func DefaultDiscoveryEnginConfig() *Config {
5459
},
5560
}
5661
}
62+
63+
func (c *Config) Validate() error {
64+
if strutil.IsBlank(c.Name) {
65+
return bizerror.NewBizError(bizerror.InvalidArgument, "discovery name is needed")
66+
}
67+
if strutil.IsBlank(string(c.Type)) {
68+
return bizerror.NewBizError(bizerror.InvalidArgument, "discovery type is needed")
69+
}
70+
return nil
71+
}

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+
authErr := bizerror.NewBizError(bizerror.Unauthorized, "no access, please login")
135+
c.JSON(http.StatusUnauthorized, model.NewBizErrorResp(authErr))
134136
c.Abort()
135137
return
136138
}

0 commit comments

Comments
 (0)