Skip to content

Commit

Permalink
Pass status code on http error
Browse files Browse the repository at this point in the history
Current implementation fails to pass error messages back to users in the
case of some errors, notably 403 errors from gitlab. This commit borrows
the http error handling from the gitea driver and applies the
pattern to all of the other drivers.
  • Loading branch information
waciumawanjohi committed Nov 13, 2020
1 parent 3cf3f76 commit 7595c3b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 5 additions & 3 deletions scm/driver/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"

Expand Down Expand Up @@ -90,9 +92,9 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
if res.Status == 401 {
return res, scm.ErrNotAuthorized
} else if res.Status > 300 {
err := new(Error)
json.NewDecoder(res.Body).Decode(err) // #nosec
return res, err
return res, errors.New(
http.StatusText(res.Status),
)
}

if out == nil {
Expand Down
7 changes: 4 additions & 3 deletions scm/driver/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -159,9 +160,9 @@ func (c *wrapper) doRequest(ctx context.Context, req *scm.Request, in, out inter
if res.Status == 404 {
return res, scm.ErrNotFound
}
err := new(Error)
json.NewDecoder(res.Body).Decode(err)
return res, err
return res, errors.New(
http.StatusText(res.Status),
)
}

if out == nil {
Expand Down
7 changes: 4 additions & 3 deletions scm/driver/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -163,9 +164,9 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface
// if an error is encountered, unmarshal and return the
// error response.
if res.Status > 300 {
err := new(Error)
json.NewDecoder(res.Body).Decode(err)
return res, err
return res, errors.New(
http.StatusText(res.Status),
)
}

if out == nil {
Expand Down

0 comments on commit 7595c3b

Please sign in to comment.