Skip to content

Commit b4d1389

Browse files
committed
feat: enhance error handling
Extracted from fork https://github.com/henrybear327/go-proton-api
1 parent 67bd01a commit b4d1389

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

link.go

+48-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package proton
22

33
import (
44
"context"
5+
"encoding/json"
6+
"net/http"
57

68
"github.com/go-resty/resty/v2"
79
)
@@ -22,12 +24,39 @@ func (c *Client) GetLink(ctx context.Context, shareID, linkID string) (Link, err
2224

2325
func (c *Client) CreateFile(ctx context.Context, shareID string, req CreateFileReq) (CreateFileRes, error) {
2426
var res struct {
27+
Code int
2528
File CreateFileRes
2629
}
2730

28-
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
31+
resp, err := c.doRes(ctx, func(r *resty.Request) (*resty.Response, error) {
2932
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/files")
30-
}); err != nil {
33+
})
34+
if err != nil { // if the status code is not 200~299, it's considered an error
35+
36+
// handle the file or folder name exists error
37+
if resp.StatusCode() == http.StatusUnprocessableEntity /* 422 */ {
38+
var apiError APIError
39+
err := json.Unmarshal(resp.Body(), &apiError)
40+
if err != nil {
41+
return CreateFileRes{}, err
42+
}
43+
if apiError.Code == AFileOrFolderNameExist {
44+
return CreateFileRes{}, ErrFileNameExist // since we are in CreateFile, so we return this error
45+
}
46+
}
47+
48+
// handle draft exists error
49+
if resp.StatusCode() == http.StatusConflict /* 409 */ {
50+
var apiError APIError
51+
err := json.Unmarshal(resp.Body(), &apiError)
52+
if err != nil {
53+
return CreateFileRes{}, err
54+
}
55+
if apiError.Code == ADraftExist {
56+
return CreateFileRes{}, ErrADraftExist
57+
}
58+
}
59+
3160
return CreateFileRes{}, err
3261
}
3362

@@ -39,9 +68,24 @@ func (c *Client) CreateFolder(ctx context.Context, shareID string, req CreateFol
3968
Folder CreateFolderRes
4069
}
4170

42-
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
71+
resp, err := c.doRes(ctx, func(r *resty.Request) (*resty.Response, error) {
4372
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/folders")
44-
}); err != nil {
73+
})
74+
if err != nil { // if the status code is not 200~299, it's considered an error
75+
76+
// handle the file or folder name exists error
77+
if resp.StatusCode() == http.StatusUnprocessableEntity /* 422 */ {
78+
// log.Println(resp.String())
79+
var apiError APIError
80+
err := json.Unmarshal(resp.Body(), &apiError)
81+
if err != nil {
82+
return CreateFolderRes{}, err
83+
}
84+
if apiError.Code == AFileOrFolderNameExist {
85+
return CreateFolderRes{}, ErrFolderNameExist // since we are in CreateFolder, so we return this error
86+
}
87+
}
88+
4589
return CreateFolderRes{}, err
4690
}
4791

response.go

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const (
3030
PaidPlanRequired Code = 10004
3131
AuthRefreshTokenInvalid Code = 10013
3232
HumanValidationInvalidToken Code = 12087
33+
34+
// ProtonDrive
35+
AFileOrFolderNameExist Code = 2500
36+
ADraftExist Code = 2500
37+
)
38+
39+
var (
40+
ErrFileNameExist = errors.New("a file with that name already exists (Code=2500, Status=422)")
41+
ErrFolderNameExist = errors.New("a folder with that name already exists (Code=2500, Status=422)")
42+
ErrADraftExist = errors.New("draft already exists on this revision (Code=2500, Status=409)")
3343
)
3444

3545
type ErrDetails []byte

0 commit comments

Comments
 (0)