Skip to content

Commit a282b8f

Browse files
Merge pull request #7 from tianxiaoliang/main
add error register
2 parents 32fbf4d + 9dc71a7 commit a282b8f

File tree

3 files changed

+116
-3
lines changed

3 files changed

+116
-3
lines changed

discovery/error.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,33 @@ const (
5454
ErrUnavailableQuota int32 = 500101
5555
)
5656

57-
var errors = map[int32]string{}
57+
var errorsMap = map[int32]string{
58+
ErrInvalidParams: "Invalid parameter(s)",
59+
ErrUnhealthy: "Server is Unhealthy",
60+
ErrServiceAlreadyExists: "Micro-service already exists",
61+
ErrServiceNotExists: "Micro-service does not exist",
62+
ErrServiceVersionNotExists: "Micro-service version does not exist",
63+
ErrDeployedInstance: "Micro-service has deployed instance(s)",
64+
ErrDependedOnConsumer: "Consumer(s) depends on this micro-service",
65+
ErrUndefinedSchemaID: "Undefined schema id",
66+
ErrModifySchemaNotAllow: "Not allowed to modify schema",
67+
ErrSchemaNotExists: "Schema does not exist",
68+
ErrInstanceNotExists: "Instance does not exist",
69+
ErrPermissionDeny: "Access micro-service refused",
70+
ErrTagNotExists: "Tag does not exist",
71+
ErrRuleAlreadyExists: "Rule already exist",
72+
ErrBlackAndWhiteRule: "Can not have both 'BLACK' and 'WHITE'",
73+
ErrModifyRuleNotAllow: "Not allowed to modify the type of the rule",
74+
ErrRuleNotExists: "Rule does not exist",
75+
ErrNotEnoughQuota: "Not enough quota",
76+
ErrUnauthorized: "Request unauthorized",
77+
ErrInternal: "Internal server error",
78+
ErrUnavailableBackend: "Registry service is unavailable",
79+
ErrUnavailableQuota: "Quota service is unavailable",
80+
ErrEndpointAlreadyExists: "Endpoint is already belong to other service",
81+
ErrForbidden: "Forbidden",
82+
ErrConflictAccount: "account name is duplicated",
83+
}
5884

5985
type Error struct {
6086
Code int32 `json:"errorCode,string"`
@@ -85,11 +111,20 @@ func (e *Error) InternalError() bool {
85111
func NewError(code int32, detail string) *Error {
86112
return &Error{
87113
Code: code,
88-
Message: errors[code],
114+
Message: errorsMap[code],
89115
Detail: detail,
90116
}
91117
}
92118

93119
func NewErrorf(code int32, format string, args ...interface{}) *Error {
94120
return NewError(code, fmt.Sprintf(format, args...))
95121
}
122+
func RegisterErrors(errs map[int32]string) {
123+
for err, msg := range errs {
124+
if err < 400000 || err >= 600000 {
125+
fmt.Sprintf("error code[%v] should be between 4xx and 5xx\n", err)
126+
continue
127+
}
128+
errorsMap[err] = msg
129+
}
130+
}

discovery/error_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
package discovery_test
18+
19+
import (
20+
"github.com/go-chassis/cari/discovery"
21+
"github.com/stretchr/testify/assert"
22+
"net/http"
23+
"testing"
24+
)
25+
26+
func TestError_StatusCode(t *testing.T) {
27+
e := discovery.Error{Code: 503999}
28+
if e.StatusCode() != http.StatusServiceUnavailable {
29+
t.Fatalf("TestError_StatusCode %v failed", e)
30+
}
31+
32+
if !e.InternalError() {
33+
t.Fatalf("TestInternalError failed")
34+
}
35+
}
36+
37+
func TestNewError(t *testing.T) {
38+
var err error
39+
err = discovery.NewError(discovery.ErrInvalidParams, "test1")
40+
if err == nil {
41+
t.Fatalf("TestNewError failed")
42+
}
43+
err = discovery.NewErrorf(discovery.ErrInvalidParams, "%s", "test2")
44+
if err == nil {
45+
t.Fatalf("TestNewErrorf failed")
46+
}
47+
48+
if len(err.Error()) == 0 {
49+
t.Fatalf("TestError failed")
50+
}
51+
52+
if len(err.(*discovery.Error).Marshal()) == 0 {
53+
t.Fatalf("TestMarshal failed")
54+
}
55+
56+
if err.(*discovery.Error).StatusCode() != http.StatusBadRequest {
57+
t.Fatalf("TestStatusCode failed, %d", err.(*discovery.Error).StatusCode())
58+
}
59+
60+
if err.(*discovery.Error).InternalError() {
61+
t.Fatalf("TestInternalError failed")
62+
}
63+
64+
err = discovery.NewErrorf(discovery.ErrInvalidParams, "")
65+
if len(err.Error()) == 0 {
66+
t.Fatalf("TestNewErrorf with empty detial failed")
67+
}
68+
}
69+
70+
func TestRegisterErrors(t *testing.T) {
71+
discovery.RegisterErrors(map[int32]string{503999: "test1", 1: "none"})
72+
73+
e := discovery.NewError(503999, "test2")
74+
assert.Equal(t, "test1", e.Message)
75+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/go-chassis/cari
22

33
go 1.13
44

5-
require github.com/gogo/protobuf v1.3.1
5+
require (
6+
github.com/gogo/protobuf v1.3.1
7+
github.com/stretchr/testify v1.6.1
8+
)

0 commit comments

Comments
 (0)