<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Jun 6, 2018 at 1:31 PM Richard W.M. Jones <<a href="mailto:rjones@redhat.com">rjones@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks: Nir Soffer<br>
---<br>
 v2v/rhv-upload-plugin.py | 69 +++++++++++++++++++++++++++++-------------------<br>
 1 file changed, 42 insertions(+), 27 deletions(-)<br>
<br>
diff --git a/v2v/rhv-upload-plugin.py b/v2v/rhv-upload-plugin.py<br>
index 791c9e7d2..f43ad0097 100644<br>
--- a/v2v/rhv-upload-plugin.py<br>
+++ b/v2v/rhv-upload-plugin.py<br>
@@ -228,6 +228,32 @@ def can_flush(h):<br>
 def get_size(h):<br>
     return params['disk_size']<br>
<br>
+# Any unexpected HTTP response status from the server will end up<br>
+# calling this function which logs the full error, pauses the<br>
+# transfer, sets the failed state, and raises a RuntimeError<br>
+# exception.<br>
+def request_failed(h, r, msg):<br>
+    # Setting the failed flag in the handle causes the disk to be<br>
+    # cleaned up on close.<br>
+    h['failed'] = True<br>
+    h['transfer_service'].pause()<br>
+<br>
+    status = r.status<br>
+    reason = r.reason<br>
+    try:<br>
+        body = r.read()<br>
+    except EnvironmentError as e:<br>
+        body = "(Unable to read response body: %s)" % e<br>
+<br>
+    # Log the full error if we're verbose.<br>
+    debug("unexpected response from imageio server:")<br>
+    debug(msg)<br>
+    debug("%d: %s" % (status, reason))<br>
+    debug(body)<br>
+<br>
+    # Only a short error is included in the exception.<br>
+    raise RuntimeError("%s: %d %s: %r", msg, status, reason, body[:200])<br>
+<br>
 # For documentation see:<br>
 # <a href="https://github.com/oVirt/ovirt-imageio/blob/master/docs/random-io.md" rel="noreferrer" target="_blank">https://github.com/oVirt/ovirt-imageio/blob/master/docs/random-io.md</a><br>
 # For examples of working code to read/write from the server, see:<br>
@@ -248,16 +274,14 @@ def pread(h, count, offset):<br>
     r = http.getresponse()<br>
     # 206 = HTTP Partial Content.<br>
     if r.status != 206:<br>
-        h['transfer_service'].pause()<br>
-        h['failed'] = True<br>
-        raise RuntimeError("could not read sector (%d, %d): %d: %s" %<br>
-                           (offset, count, r.status, r.reason))<br>
+        request_failed(h, r,<br>
+                       "could not read sector offset %d size %d" %<br>
+                       (offset, count))<br>
     return r.read()<br>
<br>
 def pwrite(h, buf, offset):<br>
     http = h['http']<br>
     transfer = h['transfer']<br>
-    transfer_service = h['transfer_service']<br>
<br>
     count = len(buf)<br>
     h['highestwrite'] = max(h['highestwrite'], offset+count)<br>
@@ -275,15 +299,13 @@ def pwrite(h, buf, offset):<br>
<br>
     r = http.getresponse()<br>
     if r.status != 200:<br>
-        transfer_service.pause()<br>
-        h['failed'] = True<br>
-        raise RuntimeError("could not write sector (%d, %d): %d: %s" %<br>
-                           (offset, count, r.status, r.reason))<br>
+        request_failed(h, r,<br>
+                       "could not write sector offset %d size %d" %<br>
+                       (offset, count))<br>
<br>
 def zero(h, count, offset, may_trim):<br>
     http = h['http']<br>
     transfer = h['transfer']<br>
-    transfer_service = h['transfer_service']<br>
<br>
     # Unlike the trim and flush calls, there is no 'can_zero' method<br>
     # so nbdkit could call this even if the server doesn't support<br>
@@ -306,10 +328,9 @@ def zero(h, count, offset, may_trim):<br>
<br>
     r = http.getresponse()<br>
     if r.status != 200:<br>
-        transfer_service.pause()<br>
-        h['failed'] = True<br>
-        raise RuntimeError("could not zero sector (%d, %d): %d: %s" %<br>
-                           (offset, count, r.status, r.reason))<br>
+        request_failed(h, r,<br>
+                       "could not zero sector offset %d size %d" %<br>
+                       (offset, count))<br>
<br>
 def emulate_zero(h, count, offset):<br>
     # qemu-img convert starts by trying to zero/trim the whole device.<br>
@@ -334,15 +355,13 @@ def emulate_zero(h, count, offset):<br>
<br>
         r = http.getresponse()<br>
         if r.status != 200:<br>
-            transfer_service.pause()<br>
-            h['failed'] = True<br>
-            raise RuntimeError("could not write zeroes (%d, %d): %d: %s" %<br>
-                               (offset, count, r.status, r.reason))<br>
+            request_failed(h, r,<br>
+                           "could not write zeroes offset %d size %d" %<br>
+                           (offset, count))<br>
<br>
 def trim(h, count, offset):<br>
     http = h['http']<br>
     transfer = h['transfer']<br>
-    transfer_service = h['transfer_service']<br>
<br>
     # Construct the JSON request for trimming.<br>
     buf = json.dumps({'op': "trim",<br>
@@ -358,15 +377,13 @@ def trim(h, count, offset):<br>
<br>
     r = http.getresponse()<br>
     if r.status != 200:<br>
-        transfer_service.pause()<br>
-        h['failed'] = True<br>
-        raise RuntimeError("could not trim sector (%d, %d): %d: %s" %<br>
-                           (offset, count, r.status, r.reason))<br>
+        request_failed(h, r,<br>
+                       "could not trim sector offset %d size %d" %<br>
+                       (offset, count))<br>
<br>
 def flush(h):<br>
     http = h['http']<br>
     transfer = h['transfer']<br>
-    transfer_service = h['transfer_service']<br>
<br>
     # Construct the JSON request for flushing.<br>
     buf = json.dumps({'op': "flush"}).encode()<br>
@@ -379,9 +396,7 @@ def flush(h):<br>
<br>
     r = http.getresponse()<br>
     if r.status != 200:<br>
-        transfer_service.pause()<br>
-        h['failed'] = True<br>
-        raise RuntimeError("could not flush: %d: %s" % (r.status, r.reason))<br>
+        request_failed(h, r, "could not flush")<br>
<br>
 def delete_disk_on_failure(h):<br>
     disk_service = h['disk_service']<br>
-- <br>
2.16.2</blockquote><div><br></div><div>Looks good</div><div><br></div><div>Nir </div></div></div>