From a54854d80e96a94156d702379032a25e85a67914 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 11 Sep 2023 12:35:38 +0700 Subject: lib/errors: implement method Is The Is method will return true if the target error is instance of *E and the value of field Code and Name match with values in e. --- lib/errors/errors_example_test.go | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/errors/errors_example_test.go (limited to 'lib/errors/errors_example_test.go') diff --git a/lib/errors/errors_example_test.go b/lib/errors/errors_example_test.go new file mode 100644 index 00000000..84d1de2e --- /dev/null +++ b/lib/errors/errors_example_test.go @@ -0,0 +1,44 @@ +package errors_test + +import ( + "encoding/json" + "errors" + "fmt" + "log" + + liberrors "github.com/shuLhan/share/lib/errors" +) + +func ExampleErrors_Is() { + var ( + errFileNotFound = &liberrors.E{ + Code: 400, + Name: `ERR_NOT_FOUND`, + Message: `file not found`, + } + errResNotFound = &liberrors.E{ + Code: 404, + Name: `ERR_NOT_FOUND`, + Message: `resource not found`, + } + + rawJson = `{"code":400,"name":"ERR_NOT_FOUND","message":"file not found"}` + + e *liberrors.E + err error + ) + + err = json.Unmarshal([]byte(rawJson), &e) + if err != nil { + log.Fatal(err) + } + + var gotErr error = e + + fmt.Println(errors.Is(gotErr, errFileNotFound)) + fmt.Println(errors.Is(gotErr, errResNotFound)) + + // Output: + // true + // false +} -- cgit v1.3