Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions app/dubbo-admin/dubbo-admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ console:
store:
type: memory
discovery:
- type: nacos
id: nacos-44.33
address:
registry: nacos://47.76.94.134:8848?username=nacos&password=nacos
configCenter: nacos://47.76.94.134:8848?username=nacos&password=nacos
metadataReport: nacos://47.76.94.134:8848?username=nacos&password=nacos

- type: etcd
id: etcd-44.33
address: http://127.0.0.1:2379
# - type: nacos
# name: nacos-44.33
# address:
# registry: nacos://47.76.94.134:8848?username=nacos&password=nacos
# configCenter: nacos://47.76.94.134:8848?username=nacos&password=nacos
# metadataReport: nacos://47.76.94.134:8848?username=nacos&password=nacos
#
# - type: etcd
# name: etcd-44.33
# address:
# registry: http://127.0.0.1:2379
# configCenter: http://127.0.0.1:2379
# metadataReport: http://127.0.0.1:2379

# mock discovery is only for development
- type: mock
name: mockRegistry
engine:
name: k8s1.28.6
type: kubernetes
Expand Down
35 changes: 6 additions & 29 deletions pkg/common/bizerror/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,15 @@
package bizerror

import (
"errors"
"fmt"
)

type AssertionError struct {
msg string
func NewAssertionError(expected, actual interface{}) Error {
return NewBizError(UnknownError, fmt.Sprintf("type assertion error, expected:%v, actual:%v", expected, actual))
}

func NewAssertionError(expected, actual interface{}) error {
return &AssertionError{
msg: fmt.Sprintf("type assertion error, expected:%v, actual:%v", expected, actual),
}
}

func (e *AssertionError) Error() string {
return e.msg
}

type MeshNotFoundError struct {
Mesh string
func NewUnauthorizedError() Error {
return NewBizError(Unauthorized, "no access, please login")
}

func (m *MeshNotFoundError) Error() string {
return fmt.Sprintf("mesh of name %s is not found", m.Mesh)
}

func MeshNotFound(meshName string) error {
return &MeshNotFoundError{meshName}
}

func IsMeshNotFound(err error) bool {
var meshNotFoundError *MeshNotFoundError
ok := errors.As(err, &meshNotFoundError)
return ok
func MeshNotFoundError(mesh string) Error {
return NewBizError(UnknownError, fmt.Sprintf("mesh of name %s is not found", mesh))
}
66 changes: 66 additions & 0 deletions pkg/common/bizerror/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bizerror

type Error interface {
Code() ErrorCode
Message() string
Error() string
Comment thread
robocanic marked this conversation as resolved.
String() string
}

type ErrorCode string

const (
UnknownError ErrorCode = "UnknownError"
InvalidArgument ErrorCode = "InvalidArgument"
StoreError ErrorCode = "StoreError"
AppNotFound ErrorCode = "AppNotFound"
Unauthorized ErrorCode = "Unauthorized"
SessionError ErrorCode = "SessionError"
)

type bizError struct {
code ErrorCode
message string
}

var _ Error = &bizError{}

func NewBizError(code ErrorCode, message string) Error {
return &bizError{
code: code,
message: message,
}
}

func (b *bizError) Code() ErrorCode {
return b.code
}

func (b *bizError) Message() string {
return b.message
}

func (b *bizError) Error() string {
return b.String()
}

func (b *bizError) String() string {
return string(b.code) + ": " + b.message
}
23 changes: 19 additions & 4 deletions pkg/config/discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

package discovery

import "github.com/apache/dubbo-admin/pkg/config"
import (
"github.com/duke-git/lancet/v2/strutil"

"github.com/apache/dubbo-admin/pkg/common/bizerror"
"github.com/apache/dubbo-admin/pkg/config"
)

type Type string

Expand All @@ -31,9 +36,9 @@ const (
// Config defines Discovery configuration
type Config struct {
config.BaseConfig
Name string `json:"name"`
Type Type `json:"type"`
Address AddressConfig
Name string `json:"name"`
Type Type `json:"type"`
Address AddressConfig `json:"address"`
}

// AddressConfig defines Discovery Engine address
Expand All @@ -54,3 +59,13 @@ func DefaultDiscoveryEnginConfig() *Config {
},
}
}

func (c *Config) Validate() error {
if strutil.IsBlank(c.Name) {
return bizerror.NewBizError(bizerror.InvalidArgument, "discovery name is needed")
}
if strutil.IsBlank(string(c.Type)) {
return bizerror.NewBizError(bizerror.InvalidArgument, "discovery type is needed")
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/console/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/gin-gonic/gin"

ui "github.com/apache/dubbo-admin/app/dubbo-ui"
"github.com/apache/dubbo-admin/pkg/common/bizerror"
"github.com/apache/dubbo-admin/pkg/config/console"
consolectx "github.com/apache/dubbo-admin/pkg/console/context"
"github.com/apache/dubbo-admin/pkg/console/model"
Expand Down Expand Up @@ -130,7 +131,8 @@ func (c *consoleWebServer) authMiddleware() gin.HandlerFunc {
session := sessions.Default(c)
user := session.Get("user")
if user == nil {
c.JSON(http.StatusUnauthorized, model.NewUnauthorizedResp())
authErr := bizerror.NewBizError(bizerror.Unauthorized, "no access, please login")
c.JSON(http.StatusUnauthorized, model.NewBizErrorResp(authErr))
c.Abort()
return
}
Expand Down
Loading
Loading