1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

improved error handling

This commit is contained in:
Matthias Hannig
2018-08-05 18:25:59 +02:00
parent d6c9522f51
commit 058ce19858
3 changed files with 62 additions and 3 deletions

View File

@ -45,9 +45,14 @@ func endpoint(wrapped apiEndpoint) httprouter.Handle {
// Get result from handler // Get result from handler
result, err := wrapped(req, params) result, err := wrapped(req, params)
if err != nil { if err != nil {
result = api.ErrorResponse{ // Get affected rs id
Error: err.Error(), rsId, paramErr := validateSourceId(params.ByName("id"))
if paramErr != nil {
rsId = -1
} }
// Make error response
result = apiErrorResponse(rsId, err)
payload, _ := json.Marshal(result) payload, _ := json.Marshal(result)
http.Error(res, string(payload), http.StatusInternalServerError) http.Error(res, string(payload), http.StatusInternalServerError)
return return

View File

@ -12,7 +12,10 @@ type Details map[string]interface{}
// Error Handling // Error Handling
type ErrorResponse struct { type ErrorResponse struct {
Error string `json:"error"` Message string `json:"message"`
Code int `json:"code"`
Tag string `json:"tag"`
RouteserverId int `json:"routeserver_id"`
} }
// Cache aware api response // Cache aware api response

51
backend/api_errors.go Normal file
View File

@ -0,0 +1,51 @@
package main
// Improve error handling
// Create api.ErrorResponses based on errors returned from server.
// Strip out potentially sensitive information, eg. connection errors
// to internal IP addresses.
import (
"net/url"
"strings"
"github.com/alice-lg/alice-lg/backend/api"
)
const (
GENERIC_ERROR_TAG = "GENERIC_ERROR"
CONNECTION_REFUSED_TAG = "CONNECTION_REFUSED"
CONNECTION_TIMEOUT_TAG = "CONNECTION_TIMEOUT"
)
const (
GENERIC_ERROR_CODE = 42
CONNECTION_REFUSED_CODE = 100
CONNECTION_TIMEOUT_CODE = 101
)
func apiErrorResponse(routeserverId int, err error) api.ErrorResponse {
code := GENERIC_ERROR_CODE
message := err.Error()
tag := GENERIC_ERROR_TAG
switch e := err.(type) {
case *url.Error:
if strings.Contains(message, "connection refused") {
tag = CONNECTION_REFUSED_TAG
code = CONNECTION_REFUSED_CODE
message = "Connection refused while dialing the API"
} else if e.Timeout() {
tag = CONNECTION_TIMEOUT_TAG
code = CONNECTION_TIMEOUT_CODE
message = "Connection timed out when connecting to the backend API"
}
}
return api.ErrorResponse{
Code: code,
Tag: tag,
Message: message,
RouteserverId: routeserverId,
}
}