-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreshelper.go
63 lines (47 loc) · 1.63 KB
/
reshelper.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
package reshelper
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
// ErrorMessage defines a structure for handling errors
type ErrorMessage struct {
AppCode string `json:"app_code"`
Message string `json:"message"`
}
// Message defines a format to send JSON messages
type Message struct {
Status int `json:"status_code"`
AppCode string `json:"app_code"`
Message string `json:"message"`
Remedies string `json:"potential_remedies"`
}
// HardError sends 500 and logs a server error to stdout
func HardError(w http.ResponseWriter, em ErrorMessage) {
// Send response
w.WriteHeader(http.StatusInternalServerError)
// Log
timestamp := time.Now().Format("2006-01-02 15:04:05")
fmt.Printf("%s :: %d [%s] %s\n", timestamp, http.StatusInternalServerError, em.AppCode, em.Message)
}
// SendMessage sends informational response message and logs it
func SendMessage(w http.ResponseWriter, rm Message) {
// Send response
w.WriteHeader(rm.Status)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(rm)
// Log
timestamp := time.Now().Format("2006-01-02 15:04:05")
fmt.Printf("%s :: %d [%s] %s\n", timestamp, rm.Status, rm.AppCode, rm.Message)
}
// DecodeError - Error decoding JSON onto struct
var DecodeError = "DECODE_ERR"
// ExternalConnectionError - Error connecting to external resource
var ExternalConnectionError = "EXT_CONNECTION_ERR"
// ExternalRequestFailed - Request to external resource failed
var ExternalRequestFailed = "EXT_REQUEST_FAIL"
// MissingKey - A key required to complete a task is missing
var MissingKey = "MISSING_KEY"
// ParseError - Error when parsing
var ParseError = "PARSE_ERR"