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

Commit

Permalink
Merge pull request #26 from dtduc91/Handle-error-for-import-function
Browse files Browse the repository at this point in the history
Handle error for import function
  • Loading branch information
cy-kaneko authored Dec 22, 2017
2 parents f764de5 + c844ea3 commit 7d30753
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
47 changes: 29 additions & 18 deletions bulkRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func (bulk *BulkRequests) Request(app *kintone.App) (*DataResponseBulkPOST, inte
if err != nil {
return nil, err
}
body, err22 := parseResponse(resp)
if err22 != nil {
return nil, err22
body, errParse := parseResponse(resp)
if errParse != nil {
return nil, errParse
}
resp1, err := bulk.Decode(body)
if err != nil {
Expand Down Expand Up @@ -378,43 +378,54 @@ func (err *ErrorResponse) show(prefix string) {
func (bulk *BulkRequests) HandelResponse(rep *DataResponseBulkPOST, err interface{}, lastRowImport, rowNumber uint64) {

if err != nil {
CLIMessage := ""
fmt.Printf(" => ERROR OCCURRED\n")
CLIMessage := fmt.Sprintf("ERROR.\nFor error details, please read the details above.\n")
CLIMessage += fmt.Sprintf("Lines %d to %d of the imported file contain errors. Please fix the errors on the file, and re-import it with the flag \"-l %d\"\n", lastRowImport, rowNumber, lastRowImport)

method := map[string]string{"POST": "INSERT", "PUT": "UPDATE"}
methodOccuredError := ""
if reflect.TypeOf(err).String() == "*main.BulkRequestsError" {
errorResp := &ErrorResponse{}
errorResp.Status = fmt.Sprintf("%v - %v", err.(*BulkRequestsError).HTTPStatus, err.(*BulkRequestsError).Message)
errorResp.Message = "Please check your params config"
errorResp.Errors = err.(*BulkRequestsError).Errors
errorResp.ID = err.(*BulkRequestsError).ID
errorResp.Code = err.(*BulkRequestsError).Code
if reflect.TypeOf(err).String() != "*main.BulkRequestsErrors" {
if reflect.TypeOf(err).String() != "*main.BulkRequestsError" {
fmt.Printf("\n")
fmt.Println(err)
fmt.Printf("\n")
// Reset CLI Message
CLIMessage = ""
} else {
errorResp := &ErrorResponse{}
errorResp.Status = err.(*BulkRequestsError).HTTPStatus
errorResp.Message = err.(*BulkRequestsError).Message
errorResp.Errors = err.(*BulkRequestsError).Errors
errorResp.ID = err.(*BulkRequestsError).ID
errorResp.Code = err.(*BulkRequestsError).Code
errorResp.show("")
}
} else {

errorsResp := err.(*BulkRequestsErrors)
CLIMessage = fmt.Sprintf("ERROR.\nFor error details, please read the details above.\n")
CLIMessage += fmt.Sprintf("Lines %d to %d of the imported file contain errors. Please fix the errors on the file, and re-import it with the flag \"-l %d\"\n", lastRowImport, rowNumber, lastRowImport)
fmt.Printf(" => ERROR OCCURRED\n")
fmt.Println("Status: ", errorsResp.HTTPStatus)
for idx, errorItem := range errorsResp.Results {
if errorItem.Code == "" {
continue
}
errorResp := &ErrorResponse{}
errorResp.ID = errorItem.ID
errorResp.Code = errorItem.Code
errorResp.Status = errorsResp.HTTPStatus
errorResp.Message = errorItem.Message
errorResp.Errors = errorItem.Errors

errorResp.show("")
methodOccuredError = method[bulk.Requests[idx].Method]
}
}
fmt.Printf("%v: ", time.Now().Format("[2006-01-02 15:04:05]"))
showTimeLog()
fmt.Printf("PROCESS STOPPED!\n\n")
if CLIMessage != "" {
fmt.Println(methodOccuredError, CLIMessage)
}
os.Exit(502)
os.Exit(1)
}
fmt.Println(" => SUCCESS")
}
func showTimeLog() {
fmt.Printf("%v: ", time.Now().Format("[2006-01-02 15:04:05]"))
}
7 changes: 3 additions & 4 deletions importWithBulkRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"regexp"
"strconv"
"time"

"github.com/kintone/go-kintone"
)
Expand Down Expand Up @@ -172,7 +171,7 @@ func importFromCSV(app *kintone.App, _reader io.Reader) error {
}
}
if rowNumber%(ConstBulkRequestLimitRecordOption) == 0 {
fmt.Printf("%v: ", time.Now().Format("[2006-01-02 15:04:05]"))
showTimeLog()
fmt.Printf("Start from lines: %d - %d", lastRowImport, rowNumber)

resp, err := bulkRequests.Request(app)
Expand All @@ -185,12 +184,12 @@ func importFromCSV(app *kintone.App, _reader io.Reader) error {
}
}
if len(bulkRequests.Requests) > 0 {
fmt.Printf("%v: ", time.Now().Format("[2006-01-02 15:04:05]"))
showTimeLog()
fmt.Printf("Start from lines: %d - %d", lastRowImport, rowNumber)
resp, err := bulkRequests.Request(app)
bulkRequests.HandelResponse(resp, err, lastRowImport, rowNumber)
}
fmt.Printf("%v: ", time.Now().Format("[2006-01-02 15:04:05]"))
showTimeLog()
fmt.Printf("DONE\n")

return nil
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func main() {
err = writeCsv(app, os.Stdout)
}
} else {
importDataFromFile(app)
err = importDataFromFile(app)
}
}
// Filter flag: the first flag have priority
Expand All @@ -242,7 +242,8 @@ func main() {
if config.filePath == "" {
err = importFromCSV(app, os.Stdin)
} else {
importDataFromFile(app)

err = importDataFromFile(app)
}
}

Expand Down

0 comments on commit 7d30753

Please sign in to comment.