Skip to content

Commit

Permalink
Merge pull request #9 from depot/api-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie authored Mar 23, 2022
2 parents f8e3c9c + 5023690 commit 2582887
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
17 changes: 16 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ func (d *Depot) InitBuild(projectID string) (*InitResponse, error) {
return nil, err
}

errorResponse, _ := tryParseErrorResponse(body)
if errorResponse != nil {
return nil, fmt.Errorf("%s", errorResponse.Error)
}

var response InitResponse
err = json.Unmarshal([]byte(body), &response)
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
Expand All @@ -86,5 +91,15 @@ func (d *Depot) FinishBuild(buildID string) error {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

errorResponse, _ := tryParseErrorResponse(body)
if errorResponse != nil {
return fmt.Errorf("%s", errorResponse.Error)
}

return nil
}
45 changes: 26 additions & 19 deletions pkg/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -31,17 +32,23 @@ type TokenErrorResponse struct {
}

func (d *Depot) AuthorizeDevice() (*TokenResponse, error) {
res, err := http.Post(fmt.Sprintf("%s/api/internal/cli/auth-request", d.BaseURL), "application/json", nil)
resp, err := http.Post(fmt.Sprintf("%s/api/internal/cli/auth-request", d.BaseURL), "application/json", nil)
if err != nil {
return nil, err
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

errorResponse, _ := tryParseErrorResponse(body)
if errorResponse != nil {
return nil, fmt.Errorf("%s", errorResponse.Error)
}

var response CLIAuthenticationResponse
err = json.NewDecoder(res.Body).Decode(&response)
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,30 +77,30 @@ func (d *Depot) AuthorizeDevice() (*TokenResponse, error) {
return nil, err
}

res, err := http.Post(response.TokenURL, "application/json", bytes.NewBuffer(tokenRequestBody))
resp, err := http.Post(response.TokenURL, "application/json", bytes.NewBuffer(tokenRequestBody))
if err != nil {
return nil, err
}

if res.StatusCode == http.StatusOK {
var tokenResponse TokenResponse
err = json.NewDecoder(res.Body).Decode(&tokenResponse)
if err != nil {
return nil, err
}
return &tokenResponse, nil
}

var errorResponse TokenErrorResponse
err = json.NewDecoder(res.Body).Decode(&errorResponse)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if errorResponse.Error == "authorization_pending" {
continue
errorResponse, _ := tryParseErrorResponse(body)
if errorResponse != nil {
if errorResponse.Error == "authorization_pending" {
continue
}

return nil, fmt.Errorf("error getting access token: %s", errorResponse.Error)
}

return nil, fmt.Errorf("error getting access token: %s", errorResponse.Error)
var response TokenResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return &response, nil
}
}
20 changes: 20 additions & 0 deletions pkg/api/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package api

import "encoding/json"

type ErrorResponse struct {
OK bool `json:"ok"`
Error string `json:"error"`
}

func tryParseErrorResponse(body []byte) (*ErrorResponse, error) {
var response ErrorResponse
err := json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
if response.OK {
return nil, nil
}
return &response, nil
}

0 comments on commit 2582887

Please sign in to comment.