Skip to content

feat: improve error handling #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package proton

import (
"context"
"encoding/json"
"net/http"

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

func (c *Client) CreateFile(ctx context.Context, shareID string, req CreateFileReq) (CreateFileRes, error) {
var res struct {
Code int
File CreateFileRes
}

if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
resp, err := c.doRes(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/files")
}); err != nil {
})
if err != nil { // if the status code is not 200~299, it's considered an error

// handle the file or folder name exists error
if resp.StatusCode() == http.StatusUnprocessableEntity /* 422 */ {
var apiError APIError
err := json.Unmarshal(resp.Body(), &apiError)
if err != nil {
return CreateFileRes{}, err
}
if apiError.Code == AFileOrFolderNameExist {
return CreateFileRes{}, ErrFileNameExist // since we are in CreateFile, so we return this error
}
}

// handle draft exists error
if resp.StatusCode() == http.StatusConflict /* 409 */ {
var apiError APIError
err := json.Unmarshal(resp.Body(), &apiError)
if err != nil {
return CreateFileRes{}, err
}
if apiError.Code == ADraftExist {
return CreateFileRes{}, ErrADraftExist
}
}

return CreateFileRes{}, err
}

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

if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
resp, err := c.doRes(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).SetBody(req).Post("/drive/shares/" + shareID + "/folders")
}); err != nil {
})
if err != nil { // if the status code is not 200~299, it's considered an error

// handle the file or folder name exists error
if resp.StatusCode() == http.StatusUnprocessableEntity /* 422 */ {
// log.Println(resp.String())
var apiError APIError
err := json.Unmarshal(resp.Body(), &apiError)
if err != nil {
return CreateFolderRes{}, err
}
if apiError.Code == AFileOrFolderNameExist {
return CreateFolderRes{}, ErrFolderNameExist // since we are in CreateFolder, so we return this error
}
}

return CreateFolderRes{}, err
}

Expand Down
10 changes: 10 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ const (
PaidPlanRequired Code = 10004
AuthRefreshTokenInvalid Code = 10013
HumanValidationInvalidToken Code = 12087

// ProtonDrive
AFileOrFolderNameExist Code = 2500
ADraftExist Code = 2500
)

var (
ErrFileNameExist = errors.New("a file with that name already exists (Code=2500, Status=422)")
ErrFolderNameExist = errors.New("a folder with that name already exists (Code=2500, Status=422)")
ErrADraftExist = errors.New("draft already exists on this revision (Code=2500, Status=409)")
)

type ErrDetails []byte
Expand Down