Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

handle upload dup error #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 34 additions & 2 deletions mediawiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Response struct {
//
// As a workaround you can use PageSlice which will create
// a list of pages from the map.
Pages map[string]Page
Pages map[string]Page
}
}

Expand Down Expand Up @@ -122,6 +122,27 @@ type uploadResponse struct {
}
}

type UploadDupError struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are duplicates/existing files the only warnings returned? If not, this should probably be a generic "Warnings" struct.

Upload struct {
Warnings struct {
Exists string `json:"exists"`
Duplicate []string `json:"duplicate"`
} `json:"warnings"`
} `json:"upload"`
}

func (u *UploadDupError) Error() string {

if len(u.Upload.Warnings.Exists) > 0 {
return u.Upload.Warnings.Exists
}

if len(u.Upload.Warnings.Duplicate) > 0 {
return u.Upload.Warnings.Duplicate[0]
}
return ""
}

// Helper function for translating MediaWiki errors in to Golang errors.
func checkError(response []byte) error {
var mwerror mwError
Expand Down Expand Up @@ -315,7 +336,18 @@ func (m *MWApi) Upload(dstFilename string, file io.Reader) error {
if err != nil {
return err
}
if !(response.Upload.Result == "Success" || response.Upload.Result == "Warning") {

if response.Upload.Result == "Warning" {

dup := UploadDupError{}
if err := json.Unmarshal(body, &dup); err != nil {
return err
}
return &dup
}

if !(response.Upload.Result == "Success") {

return errors.New(response.Upload.Result)
}
return nil
Expand Down