2019-02-22 03:23:03 +00:00
|
|
|
// Package api contains all the handlers that the client will invoke on the middleware server
|
2019-02-22 03:18:08 +00:00
|
|
|
package api
|
2019-02-27 03:59:30 +00:00
|
|
|
|
|
|
|
|
import (
|
2019-02-28 04:11:35 +00:00
|
|
|
"encoding/json"
|
2019-02-27 03:59:30 +00:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-28 04:11:35 +00:00
|
|
|
type ApiError struct {
|
|
|
|
|
Code int64 `json:"code,omitempty"`
|
|
|
|
|
Msg string `json:"msg,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 03:59:30 +00:00
|
|
|
// HandleAPIError handles any error that bubbles up to the controller layer
|
|
|
|
|
// TODO: Make the error generic enough with HTTP status codes and messages as well
|
|
|
|
|
func HandleAPIError(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
|
// Write content-type, statuscode, payload
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(500)
|
2019-02-28 04:11:35 +00:00
|
|
|
fmt.Fprintf(w, "%s", createErrorBody(err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createErrorBody(err error) []byte {
|
|
|
|
|
apiError := ApiError{
|
|
|
|
|
Code: -1,
|
|
|
|
|
Msg: err.Error(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errorJSON, _ := json.Marshal(apiError)
|
|
|
|
|
return errorJSON
|
2019-02-27 03:59:30 +00:00
|
|
|
}
|