diff options
| author | Shulhan <ms@kilabit.info> | 2023-09-11 12:35:38 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-09-11 12:35:38 +0700 |
| commit | a54854d80e96a94156d702379032a25e85a67914 (patch) | |
| tree | 2e3cd23901d3a640117a7b510e0a660ce3af72e7 /lib/errors/errors_example_test.go | |
| parent | ef1b1a8992cc71cbc52dbf68218c2feffb0c3c94 (diff) | |
| download | pakakeh.go-a54854d80e96a94156d702379032a25e85a67914.tar.xz | |
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.
Diffstat (limited to 'lib/errors/errors_example_test.go')
| -rw-r--r-- | lib/errors/errors_example_test.go | 44 |
1 files changed, 44 insertions, 0 deletions
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 +} |
