From 598597f726b5d06731caf486125ba4fbdcc445f6 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 15 Dec 2022 11:17:51 +0100 Subject: [PATCH] refactor: improve error handling a bit --- docker.v | 15 ++++++++++++--- errors.v | 7 ++++--- images.v | 29 +++++++++-------------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/docker.v b/docker.v index fb5c304..701ea1f 100644 --- a/docker.v +++ b/docker.v @@ -115,8 +115,17 @@ fn (mut d DockerConn) read_response_body(length int) !string { fn (mut d DockerConn) read_response() !(http.Response, string) { head := d.read_response_head()! + if head.status().is_error() { + content_length := head.header.get(.content_length)!.int() + body := d.read_response_body(content_length)! + mut err := json.decode(DockerError, body)! + err.status = head.status_code + + return err + } + // 204 means "No Content", so we can assume nothing follows after this - if head.status_code == 204 { + if head.status() == .no_content { return head, '' } @@ -130,9 +139,9 @@ fn (mut d DockerConn) read_response() !(http.Response, string) { } content_length := head.header.get(http.CommonHeader.content_length)!.int() - res := d.read_response_body(content_length)! + body := d.read_response_body(content_length)! - return head, res + return head, body } fn (mut d DockerConn) read_json_response() !T { diff --git a/errors.v b/errors.v index 717f9fc..31ed81a 100644 --- a/errors.v +++ b/errors.v @@ -1,6 +1,7 @@ module docker struct DockerError { +pub mut: status int [skip] message string } @@ -13,9 +14,9 @@ fn (err DockerError) msg() string { return err.message } -fn docker_error(status int, message string) IError { - return IError(DockerError{ +fn docker_error(status int, message string) DockerError { + return DockerError{ status: status message: message - }) + } } diff --git a/images.v b/images.v index a409875..00316d7 100644 --- a/images.v +++ b/images.v @@ -13,12 +13,13 @@ pub fn (mut d DockerConn) pull_image(image string, tag string) ! { d.send_request(Method.post, '/images/create?fromImage=$image&tag=$tag')! head := d.read_response_head()! - if head.status_code != 200 { - content_length := head.header.get(http.CommonHeader.content_length)!.int() + if head.status().is_error() { + content_length := head.header.get(.content_length)!.int() body := d.read_response_body(content_length)! - data := json.decode(DockerError, body)! + mut err := json.decode(DockerError, body)! + err.status = head.status_code - return error(data.message) + return err } // Keep reading the body until the pull has completed @@ -33,14 +34,8 @@ pub fn (mut d DockerConn) pull_image(image string, tag string) ! { // create_image_from_container creates a new image from a container. pub fn (mut d DockerConn) create_image_from_container(id string, repo string, tag string) !Image { - d.send_request(Method.post, '/commit?container=$id&repo=$repo&tag=$tag')! - head, body := d.read_response()! - - if head.status_code != 201 { - data := json.decode(DockerError, body)! - - return error(data.message) - } + d.send_request(.post, '/commit?container=$id&repo=$repo&tag=$tag')! + _, body := d.read_response()! data := json.decode(Image, body)! @@ -49,12 +44,6 @@ pub fn (mut d DockerConn) create_image_from_container(id string, repo string, ta // remove_image removes the image with the given id. pub fn (mut d DockerConn) remove_image(id string) ! { - d.send_request(Method.delete, '/images/$id')! - head, body := d.read_response()! - - if head.status_code != 200 { - data := json.decode(DockerError, body)! - - return error(data.message) - } + d.send_request(.delete, '/images/$id')! + d.read_response()! }