-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
53 lines (46 loc) · 1.14 KB
/
common.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
package restio
import (
"encoding/json"
"fmt"
"net/http"
"os"
"reflect"
"github.com/bitrise-io/go-utils/log"
)
// Panic ...
func Panic(f string, args ...interface{}) {
log.Errorf(f, args...)
os.Exit(1)
}
// RespondJSON ...
func RespondJSON(w http.ResponseWriter, status int, payload interface{}) {
response, err := json.Marshal(payload)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write([]byte(response))
}
// RespondError ...
func RespondError(w http.ResponseWriter, sc int, f string, args ...interface{}) {
RespondJSON(w, sc, map[string]string{"error": fmt.Sprintf(f, args...)})
}
func dynamicFunc(f interface{}) error {
fnValue := reflect.ValueOf(f)
arguments := []reflect.Value{}
fnResults := fnValue.Call(arguments)
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
for _, r := range fnResults {
if r.Type().Implements(errorInterface) {
intf := r.Interface()
if intf != nil {
return intf.(error)
}
return nil
}
}
return fmt.Errorf("function has no error return value")
}