Repository: go-kratos/kratos
Pull request #2178 (2022-07-09 17:16:29)

test(errors): improve coverage
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need to mark it as a WIP(Work In Progress) PR or draft PR
4. Please use a semantic commits format title, such as `<type>[optional scope]: <description>`, see: https://go-kratos.dev/docs/community/contribution#type
5. at the same time, please note that similar work should be submitted in one PR as far as possible to reduce the workload of reviewers. Do not split a work into multiple PR unless it should.
-->

<!--
🎉 感谢您向 Kratos 发送 PR 请求！以下是一些提示：
如果这是你第一次为 Kratos 贡献，请阅读我们的贡献指南：https://go-kratos.dev/en/docs/community/contribution/
2、确保您已经为您的 PR 添加或运行了适当的测试和lint，请在提交PR之前使用“make lint”和“make test”，使用“make clean”整理您的 go.mod。
3、如果请购单未完成，您可能需要将其标记为 WIP（在制品）PR 或 draft PR
4、请使用语义提交格式标题，如“<类型>[可选范围]：<说明>`，请参阅：https://go-kratos.dev/docs/community/contribution#type
5. 同时请注意，同类的工作请尽量在一个PR中提交，以减轻 review 者的工作负担，不要把一项工作拆分成很多个PR，除非它应该这样做。
-->

<!--
-->

#### Description (what this PR does / why we need it):
<!--
* The description should include the motivation for this PR or contrast this with previous behavior
-->


#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If your PR is not fully resolved the issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->
#2147

#### Other special notes for the reviewers:
<!--
* Somethings that need extra attention for the reviewers
* Some additional notes, TODO list, etc.
-->



Patch:
diff --git a/api/metadata/server.go b/api/metadata/server.go
index 445ddeb6..3438d132 100644
--- a/api/metadata/server.go
+++ b/api/metadata/server.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"compress/gzip"
 	"context"
+	"errors"
 	"fmt"
 	"io"
 	"sync"
@@ -76,6 +77,10 @@ func (s *Server) load() error {
 			}
 			fdps, e := allDependency(fdp)
 			if e != nil {
+				if errors.Is(e, protoregistry.NotFound) {
+					// Skip this service if one of its dependencies is not found.
+					continue
+				}
 				err = e
 				return false
 			}


Test patch:
diff --git a/errors/errors_test.go b/errors/errors_test.go
index f76d5151..5196d0b6 100644
--- a/errors/errors_test.go
+++ b/errors/errors_test.go
@@ -114,20 +114,37 @@ func TestCause(t *testing.T) {
 }
 
 func TestOther(t *testing.T) {
+	err := Errorf(10001, "test code 10001", "message")
+	// Code
 	if !reflect.DeepEqual(Code(nil), 200) {
 		t.Errorf("Code(nil) = %v, want %v", Code(nil), 200)
 	}
 	if !reflect.DeepEqual(Code(errors.New("test")), UnknownCode) {
 		t.Errorf(`Code(errors.New("test")) = %v, want %v`, Code(nil), 200)
 	}
-	if !reflect.DeepEqual(Reason(errors.New("test")), UnknownReason) {
-		t.Errorf(`Reason(errors.New("test")) = %v, want %v`, Reason(nil), UnknownReason)
-	}
-	err := Errorf(10001, "test code 10001", "message")
 	if !reflect.DeepEqual(Code(err), 10001) {
 		t.Errorf(`Code(err) = %v, want %v`, Code(err), 10001)
 	}
+	// Reason
+	if !reflect.DeepEqual(Reason(nil), UnknownReason) {
+		t.Errorf(`Reason(nil) = %v, want %v`, Reason(nil), UnknownReason)
+	}
+	if !reflect.DeepEqual(Reason(errors.New("test")), UnknownReason) {
+		t.Errorf(`Reason(errors.New("test")) = %v, want %v`, Reason(nil), UnknownReason)
+	}
 	if !reflect.DeepEqual(Reason(err), "test code 10001") {
 		t.Errorf(`Reason(err) = %v, want %v`, Reason(err), "test code 10001")
 	}
+	// Clone
+	err400 := Newf(http.StatusBadRequest, "BAD_REQUEST", "param invalid")
+	err400.Metadata = map[string]string{
+		"key1": "val1",
+		"key2": "val2",
+	}
+	if cerr := Clone(err400); cerr == nil || cerr.Error() != err400.Error() {
+		t.Errorf("Clone(err) = %v, want %v", Clone(err400), err400)
+	}
+	if cerr := Clone(nil); cerr != nil {
+		t.Errorf("Clone(nil) = %v, want %v", Clone(err400), err400)
+	}
 }