From bbff68f29f55d559f4d94a4b7e434dc63e7d8d87 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 13 Oct 2012 17:56:15 -0400 Subject: [PATCH] Send backend errors using HTTP instead of an ad hoc JSON RPC. --- frontend/main.js | 8 +++++++- server/server.go | 21 +++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/frontend/main.js b/frontend/main.js index 7d6aa1b..69acada 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -44,7 +44,13 @@ armadillo.App.prototype.sendRequest = function(action, data, callback) { url: 'service', type: 'POST', data: data, - success: callback + success: callback, + error: function(xhr, status, error) { + app.showError(xhr.responseText); + console.log(xhr); + console.log(status); + console.log(error); + } }); }; diff --git a/server/server.go b/server/server.go index a4214ef..6913314 100644 --- a/server/server.go +++ b/server/server.go @@ -127,20 +127,17 @@ func downloadHandler(response http.ResponseWriter, request *http.Request) { } } -func errorResponse(response http.ResponseWriter, message string) { +func errorResponse(rw http.ResponseWriter, msg string) { + // TODO: Replace errorResponse with httpError. + httpError(rw, msg, 400) +} + +func httpError(rw http.ResponseWriter, message string, code int) { message = strings.Replace(message, gConfig.JailRoot, "/", -1) - data := map[string]interface{}{ - "error": -1, - "message": message, - } - json_data, err := json.Marshal(data) - response.Header().Set("Content-Type", "application/json") - if err != nil { - io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}") - } else { - response.Write(json_data) - } + rw.WriteHeader(code) + rw.Header().Set("Content-Type", "text/plain") + fmt.Fprint(rw, message) } func okResponse(response http.ResponseWriter, data interface{}) { -- 2.43.5