cmd/browser: better reporting of XHR errors

When the JS fails in making a request to the back end we should give the
user as much information as possible. Before this change they would
just see the unhelpful message "[object Object]".

Change-Id: I3f274fac87c685367cd2ebcba819998dff39c92c
Reviewed-on: https://upspin-review.googlesource.com/11540
Reviewed-by: Rob Pike <r@golang.org>
diff --git a/cmd/browser/static/script.js b/cmd/browser/static/script.js
index 5de953d..c2f75bc 100644
--- a/cmd/browser/static/script.js
+++ b/cmd/browser/static/script.js
@@ -413,6 +413,19 @@
 		token: ""
 	};
 
+	// errorHandler returns an XHR error callback that invokes the given
+	// browser error callback with the human-readable error string.
+	function errorHandler(callback) {
+		return function(jqXHR, textStatus, errorThrown) {
+			console.log(textStatus, errorThrown);
+			if (errorThrown) {
+				callback(errorThrown);
+				return;
+			}
+			callback(textStatus);
+		}
+	}
+
 	function list(path, success, error) {
 		$.ajax("/_upspin", {
 			method: "POST",
@@ -429,7 +442,7 @@
 				}
 				success(data.Entries);
 			},
-			error: error
+			error: errorHandler(error)
 		});
 	}
 
@@ -449,7 +462,7 @@
 				}
 				success();
 			},
-			error: error
+			error: errorHandler(error)
 		});
 	}
 
@@ -470,7 +483,7 @@
 				}
 				success();
 			},
-			error: error
+			error: errorHandler(error)
 		});
 	}
 
@@ -490,7 +503,7 @@
 				}
 				success();
 			},
-			error: error
+			error: errorHandler(error)
 		});
 	}
 
@@ -506,7 +519,7 @@
 				}
 				success(data);
 			},
-			error: error
+			error: errorHandler(error)
 		});
 	}