Skip to content

Commit ef30bf9

Browse files
author
Oleg Sucharevich
committed
handle errors from requests
1 parent 926a0a4 commit ef30bf9

File tree

9 files changed

+47
-24
lines changed

9 files changed

+47
-24
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.2
1+
0.0.3

cmd/create_token.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var createTokenCmd = &cobra.Command{
2828
Run: func(cmd *cobra.Command, args []string) {
2929
client := viper.Get("codefresh")
3030
codefreshClient := utils.CastToCodefreshOrDie(client)
31-
token := codefreshClient.GenerateToken("TestToken", "hybrid/codefresh-re")
31+
token, err := codefreshClient.GenerateToken("TestToken", "hybrid/codefresh-re")
32+
internal.DieOnError(err)
3233
table := internal.CreateTable()
3334
table.SetHeader([]string{"name", "token"})
3435
table.Append([]string{token.Name, token.Value})

cmd/get_pipeline.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ var getPipelineCmd = &cobra.Command{
3737
table := internal.CreateTable()
3838
table.SetHeader([]string{"Pipeline Name", "Created At", "Updated At"})
3939
table.Append([]string{"", "", ""})
40-
pipelines := codefreshClient.GetPipelines()
40+
pipelines, err := codefreshClient.GetPipelines()
41+
internal.DieOnError(err)
4142
for _, p := range pipelines {
4243
table.Append([]string{
4344
p.Metadata.Name,

cmd/get_tokens.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ var getTokensCmd = &cobra.Command{
3434
table := internal.CreateTable()
3535
table.SetHeader([]string{"Created", "ID", "Name", "Reference Subject", "Reference Type", "Token"})
3636
table.Append([]string{"", "", ""})
37-
tokens := codefreshClient.GetTokens()
37+
tokens, err := codefreshClient.GetTokens()
38+
internal.DieOnError(err)
3839
for _, t := range tokens {
3940
table.Append([]string{
4041
humanize.Time(t.Created),

cmd/run_pipeline.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"errors"
1919
"fmt"
2020

21+
"github.com/codefresh-io/go-sdk/internal"
2122
"github.com/codefresh-io/go-sdk/pkg/utils"
2223
"github.com/spf13/cobra"
2324
"github.com/spf13/viper"
@@ -39,7 +40,8 @@ var runPipelineCmd = &cobra.Command{
3940
client := viper.Get("codefresh")
4041
codefreshClient := utils.CastToCodefreshOrDie(client)
4142
for _, name := range args {
42-
build := codefreshClient.RunPipeline(name)
43+
build, err := codefreshClient.RunPipeline(name)
44+
internal.DieOnError(err)
4345
fmt.Printf("Pipeline started with ID: %s\n", build)
4446
}
4547
},

internal/error_handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func DieOnError(err error) {
9+
if err != nil {
10+
fmt.Println(err)
11+
os.Exit(1)
12+
}
13+
}

pkg/codefresh/codefresh.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package codefresh
22

33
import (
4+
"fmt"
5+
46
"gopkg.in/h2non/gentleman.v2"
57
"gopkg.in/h2non/gentleman.v2/plugins/body"
68
"gopkg.in/h2non/gentleman.v2/plugins/query"
79
)
810

911
type (
1012
Codefresh interface {
11-
requestAPI(*requestOptions) *gentleman.Response
13+
requestAPI(*requestOptions) (*gentleman.Response, error)
1214
ITokenAPI
1315
IPipelineAPI
1416
}
@@ -23,7 +25,7 @@ func New(opt *ClietOptions) Codefresh {
2325
}
2426
}
2527

26-
func (c *codefresh) requestAPI(opt *requestOptions) *gentleman.Response {
28+
func (c *codefresh) requestAPI(opt *requestOptions) (*gentleman.Response, error) {
2729
req := c.client.Request()
2830
req.Path(opt.path)
2931
req.Method(opt.method)
@@ -37,5 +39,8 @@ func (c *codefresh) requestAPI(opt *requestOptions) *gentleman.Response {
3739
}
3840
}
3941
res, _ := req.Send()
40-
return res
42+
if res.StatusCode > 400 {
43+
return res, fmt.Errorf("Error occured during API invocation\nError: %s", res.String())
44+
}
45+
return res, nil
4146
}

pkg/codefresh/pipeline.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
type (
1010
// IPipelineAPI declers Codefresh pipeline API
1111
IPipelineAPI interface {
12-
GetPipelines() []*Pipeline
13-
RunPipeline(string) string
12+
GetPipelines() ([]*Pipeline, error)
13+
RunPipeline(string) (string, error)
1414
}
1515

1616
PipelineMetadata struct {
@@ -61,20 +61,20 @@ type (
6161
)
6262

6363
// GetPipelines - returns pipelines from API
64-
func (c *codefresh) GetPipelines() []*Pipeline {
64+
func (c *codefresh) GetPipelines() ([]*Pipeline, error) {
6565
r := &getPipelineResponse{}
66-
resp := c.requestAPI(&requestOptions{
66+
resp, err := c.requestAPI(&requestOptions{
6767
path: "/api/pipelines",
6868
method: "GET",
6969
})
7070
resp.JSON(r)
71-
return r.Docs
71+
return r.Docs, err
7272
}
7373

74-
func (c *codefresh) RunPipeline(name string) string {
75-
resp := c.requestAPI(&requestOptions{
74+
func (c *codefresh) RunPipeline(name string) (string, error) {
75+
resp, err := c.requestAPI(&requestOptions{
7676
path: fmt.Sprintf("/api/pipelines/run/%s", url.PathEscape(name)),
7777
method: "POST",
7878
})
79-
return resp.String()
79+
return resp.String(), err
8080
}

pkg/codefresh/tokens.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
type (
99
ITokenAPI interface {
10-
GenerateToken(name string, subject string) *Token
11-
GetTokens() []*Token
10+
GenerateToken(name string, subject string) (*Token, error)
11+
GetTokens() ([]*Token, error)
1212
}
1313

1414
Token struct {
@@ -40,8 +40,8 @@ func (s tokenSubjectType) String() string {
4040
return [...]string{"runtime-environment"}[s]
4141
}
4242

43-
func (c *codefresh) GenerateToken(name string, subject string) *Token {
44-
resp := c.requestAPI(&requestOptions{
43+
func (c *codefresh) GenerateToken(name string, subject string) (*Token, error) {
44+
resp, err := c.requestAPI(&requestOptions{
4545
path: "/api/auth/key",
4646
method: "POST",
4747
body: map[string]interface{}{
@@ -55,17 +55,17 @@ func (c *codefresh) GenerateToken(name string, subject string) *Token {
5555
return &Token{
5656
Name: name,
5757
Value: resp.String(),
58-
}
58+
}, err
5959
}
6060

61-
func (c *codefresh) GetTokens() []*Token {
61+
func (c *codefresh) GetTokens() ([]*Token, error) {
6262
emptySlice := make([]*Token, 0)
63-
resp := c.requestAPI(&requestOptions{
63+
resp, err := c.requestAPI(&requestOptions{
6464
path: "/api/auth/keys",
6565
method: "GET",
6666
})
6767
tokensAsBytes := []byte(resp.String())
6868
json.Unmarshal(tokensAsBytes, &emptySlice)
6969

70-
return emptySlice
70+
return emptySlice, err
7171
}

0 commit comments

Comments
 (0)