refactor: improve error handling a bit
parent
2dcee45406
commit
598597f726
15
docker.v
15
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) {
|
fn (mut d DockerConn) read_response() !(http.Response, string) {
|
||||||
head := d.read_response_head()!
|
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
|
// 204 means "No Content", so we can assume nothing follows after this
|
||||||
if head.status_code == 204 {
|
if head.status() == .no_content {
|
||||||
return head, ''
|
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()
|
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>() !T {
|
fn (mut d DockerConn) read_json_response<T>() !T {
|
||||||
|
|
7
errors.v
7
errors.v
|
@ -1,6 +1,7 @@
|
||||||
module docker
|
module docker
|
||||||
|
|
||||||
struct DockerError {
|
struct DockerError {
|
||||||
|
pub mut:
|
||||||
status int [skip]
|
status int [skip]
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
@ -13,9 +14,9 @@ fn (err DockerError) msg() string {
|
||||||
return err.message
|
return err.message
|
||||||
}
|
}
|
||||||
|
|
||||||
fn docker_error(status int, message string) IError {
|
fn docker_error(status int, message string) DockerError {
|
||||||
return IError(DockerError{
|
return DockerError{
|
||||||
status: status
|
status: status
|
||||||
message: message
|
message: message
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
29
images.v
29
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')!
|
d.send_request(Method.post, '/images/create?fromImage=$image&tag=$tag')!
|
||||||
head := d.read_response_head()!
|
head := d.read_response_head()!
|
||||||
|
|
||||||
if head.status_code != 200 {
|
if head.status().is_error() {
|
||||||
content_length := head.header.get(http.CommonHeader.content_length)!.int()
|
content_length := head.header.get(.content_length)!.int()
|
||||||
body := d.read_response_body(content_length)!
|
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
|
// 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.
|
// 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 {
|
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')!
|
d.send_request(.post, '/commit?container=$id&repo=$repo&tag=$tag')!
|
||||||
head, body := d.read_response()!
|
_, body := d.read_response()!
|
||||||
|
|
||||||
if head.status_code != 201 {
|
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := json.decode(Image, body)!
|
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.
|
// remove_image removes the image with the given id.
|
||||||
pub fn (mut d DockerConn) remove_image(id string) ! {
|
pub fn (mut d DockerConn) remove_image(id string) ! {
|
||||||
d.send_request(Method.delete, '/images/$id')!
|
d.send_request(.delete, '/images/$id')!
|
||||||
head, body := d.read_response()!
|
d.read_response()!
|
||||||
|
|
||||||
if head.status_code != 200 {
|
|
||||||
data := json.decode(DockerError, body)!
|
|
||||||
|
|
||||||
return error(data.message)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue