feat: properly handle RPC errors

main
Jef Roosens 2022-06-22 13:46:35 +02:00
parent 7efa42f1e5
commit e4ccbddb0c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 9 additions and 4 deletions

View File

@ -24,9 +24,10 @@ pub fn new_with_url(url string) &Client {
}
}
struct SuccessResponse {
result_count int [json: resultCount]
results []Package
struct Response {
@type string [json: 'type']
error string
results []Package
}
fn (c Client) request(params map[string][]string) ?[]Package {
@ -45,7 +46,11 @@ fn (c Client) request(params map[string][]string) ?[]Package {
return error('error')
}
data := json.decode(SuccessResponse, res.body)?
data := json.decode(Response, res.body)?
if data.@type == 'error' {
return error('Server responded with an error: $data.error')
}
return data.results
}