aboutsummaryrefslogtreecommitdiff
path: root/lib/errors/errors_example_test.go
blob: 84d1de2e28d6320706cf88354d00f8d5e4b76b55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}