github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/4-purely-functional/ch10-monads/01_car_steps/src/utils/utils.go (about) 1 package utils 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "net/http" 8 "os" 9 "path" 10 "runtime" 11 "time" 12 "strings" 13 ) 14 15 type APIError struct { 16 ErrorMessage string `json:"error_message"` 17 HTTPStatus int `json:"http_status"` 18 } 19 20 type HttpErrorHandler struct { 21 Caller string 22 Response http.ResponseWriter 23 Request *http.Request 24 } 25 26 const ( 27 ErrorActionErr = iota 28 ErrorActionWarn 29 ErrorActionDebug 30 ErrorActionInfo 31 ) 32 33 func NewHttpErrorHandle(caller string, response http.ResponseWriter, request *http.Request) *HttpErrorHandler { 34 return &HttpErrorHandler{caller, response, request} 35 } 36 37 func (h *HttpErrorHandler) Handle(err error, httpStatus int, action int) bool { 38 if err != nil { 39 _, filepath, line, _ := runtime.Caller(1) 40 _, file := path.Split(filepath) 41 Error.Printf("HttpErrorHandler()->[file:%s line:%d]: %s", file, line, err.Error()) 42 apiErr := &APIError{ 43 ErrorMessage: err.Error(), 44 HTTPStatus: httpStatus, 45 } 46 serialErr, _ := json.Marshal(&apiErr) 47 h.Response.Header().Set("Content-Type", "application/json") 48 h.Response.WriteHeader(httpStatus) 49 io.WriteString(h.Response, string(serialErr)) 50 } 51 return err != nil 52 } 53 54 func FromLineOfFile() string { 55 _, filepath, line, _ := runtime.Caller(1) 56 _, file := path.Split(filepath) 57 return fmt.Sprintf("[file:%s line:%d]", file, line) 58 } 59 60 func HandlePanic(err error) { 61 if err != nil { 62 _, filePath, lineNo, _ := runtime.Caller(1) 63 _, fileName := path.Split(filePath) 64 msg := fmt.Sprintf("[file:%s line:%d]: %s", fileName, lineNo, err.Error()) 65 panic(msg) 66 } 67 } 68 69 func HandleError(err error, action int) bool { 70 if err != nil { 71 _, filepath, line, _ := runtime.Caller(1) 72 _, file := path.Split(filepath) 73 switch action { 74 case ErrorActionErr: 75 Error.Printf("[file:%s line:%d]: %s", file, line, err.Error()) 76 break 77 case ErrorActionWarn: 78 Error.Printf("[file:%s line:%d]: %s", file, line, err.Error()) 79 break 80 case ErrorActionDebug: 81 Error.Printf("[file:%s line:%d]: %s", file, line, err.Error()) 82 break 83 case ErrorActionInfo: 84 Error.Printf("[file:%s line:%d]: %s", file, line, err.Error()) 85 break 86 } 87 } 88 return err != nil 89 } 90 91 func WriteFile(filename string, source io.Reader) error { 92 writer, err := os.Create(filename) 93 if err != nil { 94 return err 95 } 96 defer writer.Close() 97 io.Copy(writer, source) 98 return nil 99 } 100 101 func TimeTrack(start time.Time, name string) { 102 if Config.LogTimeTrack == true { 103 elapsed := time.Since(start) 104 Debug.Debugf("%s took %s", name, elapsed) 105 } 106 } 107 108 func PadRight(str string, padWith string, length int) string { 109 count := length - len(str) 110 if count < 0 { 111 count = 0 112 } 113 return str + strings.Repeat(padWith, count) 114 } 115 116 func InSlice(slice []string, searchFor string) (found bool) { 117 for _, v := range slice { 118 if searchFor == v { 119 found = true 120 } 121 } 122 return found 123 }