Enable return error for arbitray request job
authorCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 05:50:31 +0000 (13:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 12 Aug 2015 05:50:31 +0000 (13:50 +0800)
atom/browser/net/js_asker.cc
atom/browser/net/js_asker.h

index 96c6736..b53799b 100644 (file)
@@ -110,6 +110,18 @@ void AskForOptions(v8::Isolate* isolate,
   handler.Run(request, wrapped_callback.ToLocalChecked());
 }
 
+bool IsErrorOptions(base::Value* value, int* error) {
+  if (value->IsType(base::Value::TYPE_DICTIONARY)) {
+    base::DictionaryValue* dict = static_cast<base::DictionaryValue*>(value);
+    if (dict->GetInteger("error", error))
+      return true;
+  } else if (value->IsType(base::Value::TYPE_INTEGER)) {
+    if (value->GetAsInteger(error))
+      return true;
+  }
+  return false;
+}
+
 }  // namespace internal
 
 }  // namespace atom
index 97b384e..0f5f058 100644 (file)
@@ -29,6 +29,9 @@ void AskForOptions(v8::Isolate* isolate,
                    net::URLRequest* request,
                    const ResponseCallback& callback);
 
+// Test whether the |options| means an error.
+bool IsErrorOptions(base::Value* value, int* error);
+
 }  // namespace internal
 
 template<typename RequestJob>
@@ -63,13 +66,13 @@ class JsAsker : public RequestJob {
 
   // Called when the JS handler has sent the response, we need to decide whether
   // to start, or fail the job.
-  void OnResponse(bool success, scoped_ptr<base::Value> options) {
-    if (success && options) {
-      StartAsync(options.Pass());
+  void OnResponse(bool success, scoped_ptr<base::Value> value) {
+    int error = net::ERR_NOT_IMPLEMENTED;
+    if (success && value && !internal::IsErrorOptions(value.get(), &error)) {
+      StartAsync(value.Pass());
     } else {
       RequestJob::NotifyStartError(
-          net::URLRequestStatus(net::URLRequestStatus::FAILED,
-                                net::ERR_NOT_IMPLEMENTED));
+          net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
     }
   }