1
18
19 package errors
20
21 import (
22 "encoding/json"
23 "net/http"
24 "scheduler/utils"
25 )
26
27 type ErrorReply struct {
28 Code int `json:"code,omitempty"`
29 Message string `json:"message,omitempty"`
30 Err error `json:"err,omitempty"`
31 }
32
33 const (
34 GenericError int = 1
35 DBError int = 2
36 GenericNotFoundError int = 3
37 InputNotValid int = 4
38 FaasConnectError int = 5
39 MarshalError int = 6
40 ServiceNotValid int = 100
41 GenericDeployError int = 200
42 GenericOpenFaasError int = 300
43 JobCannotBeScheduledError int = 400
44 JobDeliberatelyRejected int = 401
45 CannotRetrieveAction int = 402
46 JobCouldNotBeForwarded int = 403
47 PeerResponseNil int = 404
48 CannotRetrieveRecipientNode int = 405
49
50 DBDuplicateKey int = 11000
51 )
52
53 var errorMessages = map[int]string{
54 1: "Generic Error",
55 2: "DB Error",
56 3: "Not Found",
57 4: "Passed input is not correct or malformed",
58 5: "Could not contact OpenFaaS backend",
59 6: "Cannot marshal the struct",
60
61 100: "Passed service is not valid",
62
63 200: "Error while deploying the service",
64
65 300: "OpenFaas generic error, see logs",
66
67 400: "Job cannot be scheduled due to physical limitation (e.g. queue full)",
68 401: "Job have been deliberately rejected by the scheduler",
69 402: "It is not possible to retrieve the scheduling action",
70 403: "It was not possible to forward the request to the neighbor",
71 404: "Peer replied with nil response",
72 405: "Recipient node to which the job must be forwarded cannot be retrieved",
73
74 11000: "A key is duplicated",
75 }
76
77 var errorStatus = map[int]int{
78 1: 500,
79 2: 500,
80 3: 404,
81 4: 400,
82 5: 500,
83 6: 500,
84
85 100: 400,
86
87 200: 500,
88
89 300: 500,
90
91 400: 500,
92 401: 503,
93 402: 500,
94 403: 500,
95 404: 500,
96 405: 500,
97
98 11000: 400,
99 }
100
101 func GetErrorJson(errorCode int) (int, string, error) {
102 var errorReply = ErrorReply{Code: errorCode, Message: errorMessages[errorCode]}
103 errorReplyJSON, err := json.Marshal(errorReply)
104
105 if err != nil {
106 return -1, "", err
107 }
108
109 return errorStatus[errorCode], string(errorReplyJSON), nil
110 }
111
112 func GetErrorJsonMessage(errorCode int, msg string) (int, string, error) {
113 var errorReply = ErrorReply{Code: errorCode, Message: msg}
114 errorReplyJSON, err := json.Marshal(errorReply)
115
116 if err != nil {
117 return -1, "", err
118 }
119
120 return errorStatus[errorCode], string(errorReplyJSON), nil
121 }
122
123 func ReplyWithError(w *http.ResponseWriter, errorCode int, customHeaders *map[string]string) {
124 errorStatusCode, errorResponseJson, _ := GetErrorJson(errorCode)
125
126 utils.HttpSendJSONResponse(w, errorStatusCode, errorResponseJson, customHeaders)
127 }
128
129 func ReplyWithErrorMessage(w *http.ResponseWriter, errorCode int, msg string, customHeaders *map[string]string) {
130 var errorReply = ErrorReply{Code: errorCode, Message: msg}
131 errorReplyJSON, _ := json.Marshal(errorReply)
132
133 utils.HttpSendJSONResponse(w, errorStatus[errorCode], string(errorReplyJSON), customHeaders)
134 }
135
View as plain text