Send backend errors using HTTP instead of an ad hoc JSON RPC.
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 13 Oct 2012 21:56:15 +0000 (17:56 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 13 Oct 2012 21:56:15 +0000 (17:56 -0400)
frontend/main.js
server/server.go

index 7d6aa1b29d846226432b94a9e7ef4f2f56e0d994..69acada0d5c71aa44f3cad847dc8687f797f106a 100644 (file)
@@ -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);
+      }
   });
 };
 
index a4214efb8cc565f0f4d73747c2320fa0688a8700..6913314949145a8944a5b2af4d0699e786a87d74 100644 (file)
@@ -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{}) {