Skip to content

Commit 5e82bd3

Browse files
improve error handling, add client interface
1 parent a4e48ee commit 5e82bd3

File tree

3 files changed

+55
-95
lines changed

3 files changed

+55
-95
lines changed

do_files_push.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9-
"regexp"
109
"strings"
1110

1211
"github.com/Smartling/api-sdk-go"
1312
"github.com/reconquest/hierr-go"
1413
)
1514

1615
func doFilesPush(
17-
client *smartling.Client,
16+
client smartling.ClientInterface,
1817
config Config,
1918
args map[string]interface{},
2019
) error {
@@ -247,12 +246,8 @@ func doFilesPush(
247246
if err != nil {
248247
if returnError(err) {
249248
return NewError(
250-
hierr.Errorf(
251-
err,
252-
`unable to upload file "%s"`,
253-
file,
254-
),
255-
249+
err,
250+
fmt.Sprintf(`unable to upload file "%s"`, file),
256251
`Check, that you have enough permissions to upload file to`+
257252
` the specified project`,
258253
)
@@ -284,19 +279,19 @@ func doFilesPush(
284279
}
285280

286281
func returnError(err error) bool {
287-
if err.Error() == "failed to upload original file: unable to authenticate: "+
288-
"authentication parameters are invalid" {
282+
if errors.Is(err, smartling.NotAuthorizedError{}) {
289283
return true
290284
}
291285

292-
reasons := []string{
293-
"AUTHENTICATION_ERROR",
294-
"AUTHORIZATION_ERROR",
295-
"MAINTENANCE_MODE_ERROR",
286+
if (errors.Is(err, smartling.APIError{})) {
287+
reasons := map[string]struct{}{
288+
"AUTHENTICATION_ERROR": {},
289+
"AUTHORIZATION_ERROR": {},
290+
"MAINTENANCE_MODE_ERROR": {},
291+
}
292+
_, ok := reasons[err.(smartling.APIError).Code]
293+
return ok
296294
}
297295

298-
apiCode, _ := regexp.MatchString(
299-
`(?s)Response Body:.+{.+"code":.+"`+strings.Join(reasons, "|")+`".+}.+Response Headers:`,
300-
err.Error())
301-
return apiCode
296+
return false
302297
}

error.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func NewError(cause error, description string, args ...interface{}) Error {
1616
}
1717
}
1818

19+
func (err Error) Unwrap() error {
20+
return err.Cause
21+
}
22+
1923
func (err Error) Error() string {
2024
return fmt.Sprintf(
2125
"ERROR: %s\n\n%s",

push_test.go

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
smartling "github.com/Smartling/api-sdk-go"
78
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
810
"io/ioutil"
911
"net/http"
1012
"strings"
@@ -18,13 +20,26 @@ type request struct {
1820

1921
type roundTripFunc func(req *http.Request) *http.Response
2022

21-
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
22-
return f(req), nil
23+
type MockSmartlingClient struct {
24+
mock.Mock
2325
}
2426

25-
func mockHttpClient(fn roundTripFunc) *http.Client {
27+
func (mock *MockSmartlingClient) UploadFile(
28+
projectID string,
29+
request smartling.FileUploadRequest,
30+
) (*smartling.FileUploadResult, error) {
31+
args := mock.Called(projectID, request)
32+
var result smartling.FileUploadResult
33+
return &result, args.Error(1)
34+
}
35+
36+
func (function roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
37+
return function(req), nil
38+
}
39+
40+
func mockHttpClient(function roundTripFunc) *http.Client {
2641
return &http.Client{
27-
Transport: fn,
42+
Transport: function,
2843
}
2944
}
3045

@@ -42,105 +57,51 @@ func TestPushStopUnauthorized(t *testing.T) {
4257

4358
err := doFilesPush(&client, getConfig(), args)
4459

45-
assert.EqualError(
46-
t,
47-
err,
48-
"ERROR: unable to upload file \"README.md\"\n"+
49-
"└─ failed to upload original file: unable to authenticate: "+
50-
"authentication parameters are invalid\n\n"+
51-
"Check, that you have enough permissions to upload file to the specified project")
60+
assert.True(t, errors.Is(err, smartling.NotAuthorizedError{}))
5261
}
5362

5463
func TestPushContinueFakeError(t *testing.T) {
5564
args := getArgs("README.md README.md")
5665

57-
responses := []request{{`{
58-
"response": {
59-
"code": "SUCCESS",
60-
"data": {
61-
"accessToken": "accessToken",
62-
"refreshToken": "refreshToken"
63-
}
64-
}
65-
}`, 200},
66-
{`{
67-
"response": {
68-
"code": "SUCCESS",
69-
"data": {
70-
}
71-
}
72-
}`, 401},
73-
{`{
74-
"response": {
75-
"code": "SUCCESS",
76-
"data": {
77-
}
78-
}
79-
}`, 200},
80-
}
81-
82-
httpClient := getMockHttpClient(responses)
83-
8466
mockGlobber(args)
8567
defer func() {
8668
globFilesLocally = globFilesLocallyFunc
8769
}()
8870

89-
client := getClient(httpClient)
71+
client := new(MockSmartlingClient)
72+
client.On("UploadFile", "test", mock.Anything).
73+
Return(nil, smartling.APIError{Cause: errors.New("some error")}).
74+
Times(2)
9075

91-
err := doFilesPush(&client, getConfig(), args)
76+
err := doFilesPush(client, getConfig(), args)
9277
assert.EqualError(
9378
t,
9479
err,
95-
"ERROR: failed to upload 1 files\n\nfailed to upload files README.md")
80+
"ERROR: failed to upload 2 files\n\nfailed to upload files README.md, README.md")
81+
client.AssertExpectations(t)
9682
}
9783

9884
func TestPushStopApiError(t *testing.T) {
9985
args := getArgs("README.md README.md")
10086

101-
responses := []request{{`{
102-
"response": {
103-
"code": "SUCCESS",
104-
"data": {
105-
"accessToken": "accessToken",
106-
"refreshToken": "refreshToken"
107-
}
108-
}
109-
}`, 200},
110-
{`{
111-
"response": {
112-
"code": "MAINTENANCE_MODE_ERROR",
113-
"data": {
114-
"accessToken": "accessToken",
115-
"refreshToken": "refreshToken"
116-
}
117-
}
118-
}`, 500},
119-
{`{
120-
"response": {
121-
"code": "SUCCESS",
122-
"data": {
123-
"accessToken": "accessToken",
124-
"refreshToken": "refreshToken"
125-
}
126-
}
127-
}`, 200},
128-
}
129-
130-
httpClient := getMockHttpClient(responses)
131-
13287
mockGlobber(args)
13388
defer func() {
13489
globFilesLocally = globFilesLocallyFunc
13590
}()
13691

137-
client := getClient(httpClient)
92+
client := new(MockSmartlingClient)
93+
expectedError := smartling.APIError{
94+
Cause: errors.New("some error"),
95+
Code: "MAINTENANCE_MODE_ERROR",
96+
}
97+
client.On("UploadFile", "test", mock.Anything).
98+
Return(nil, expectedError).
99+
Once()
138100

139-
err := doFilesPush(&client, getConfig(), args)
101+
err := doFilesPush(client, getConfig(), args)
140102

141-
assert.Error(t, err)
142-
assert.Contains(t, err.Error(), "ERROR: unable to upload file \"README.md\"\n"+
143-
"└─ failed to upload original file: API call returned unexpected HTTP code: 500")
103+
assert.True(t, errors.Is(err, expectedError))
104+
client.AssertExpectations(t)
144105
}
145106

146107
func getMockHttpClient(responses []request) *http.Client {

0 commit comments

Comments
 (0)