forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise2.go
66 lines (56 loc) · 1.84 KB
/
exercise2.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// https://play.golang.org/p/8UyOIVjFtY
// Create a custom error type called appError that contains three fields, err error,
// message string and code int. Implement the error interface providing your own message
// using these three fields. Implement a second method named Temporary that returns false
// when the value of the code field is 9. Write a function called checkFlag that accepts
// a bool value. If the value is false, return a pointer of your custom error type
// initialized as you like. If the value is true, return a default error. Write a main
// function to call the checkFlag function and check the error.
package main
import (
"errors"
"fmt"
)
// appError is a custom error type for the program.
type appError struct {
err error
message string
code int
}
// Error implements the error interface for appError.
func (a *appError) Error() string {
return fmt.Sprintf("App Error[%s] Message[%s] Code[%d]", a.err, a.message, a.code)
}
// Temporary implements behavior about the error.
func (a *appError) Temporary() bool {
return (a.code != 9)
}
// temporary is used to test the error we receive.
type temporary interface {
Temporary() bool
}
// main is the entry point for the application.
func main() {
if err := checkFlag(false); err != nil {
switch e := err.(type) {
case temporary:
fmt.Println(err)
if !e.Temporary() {
fmt.Println("Critical Error!")
}
default:
fmt.Println(err)
}
}
}
// checkFlag returns one of two errors based on the value of the parameter.
func checkFlag(t bool) error {
// If the parameter is false return an appError.
if !t {
return &appError{errors.New("Flag False"), "The Flag was false", 9}
}
// Return a default error.
return errors.New("Flag True")
}