Commit 17149133 authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Add error handling.

parent 5735e7d0
......@@ -15,7 +15,8 @@
"token": "aBcDeFgHiJkLmNoPqRsTuVwXyZ",
"user": "digicademy",
"repo": "digicademy/svsal",
"commit_keyword": ""
"commit_keyword": "",
"commit_dontpublish_keyword": "test"
},
"metadata": {
"fields": [
......
......@@ -15,10 +15,7 @@ import (
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo"
)
// 0. decide where trigger keywords can be stored - in commit messages?
// 1. parse github push request -> get commits
// 2. retrieve commits -> get files
// 3. retrieve files -> pipe to zenodo
// ====== Types =======
// PushHook stores the payload of a github push webhook event
type PushHook struct {
......@@ -271,11 +268,17 @@ type FileInfo struct {
Date time.Time
}
// ====== Functions =======
// 1. parse github push request -> get commits, check repo, pusher, keywords in commit msgs
// 2. retrieve commits -> get files
// 3. retrieve files -> pipe to zenodo
// ProcessHook processes a Webhook's payload
// it returns a (boolean) doPublish value, a map of filenames/urls with all concerned files, and an error value
func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (bool, map[string]FileInfo, error) {
doPublish := false
doPublish := true
switch hookType {
case "push":
......@@ -286,15 +289,15 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
err := json.NewDecoder(r).Decode(&payload)
if err != nil {
log.Printf("Error processing push hook: %+v", err)
return false, nil, fmt.Errorf("error processing push hook: %s", err)
return false, nil, tei2zenodo.NewError("errInternal", fmt.Sprintf("error processing push hook: %s", err.Error()), 500, err)
}
if payload.Repository.FullName != conf.AllowedGit.Repo && conf.AllowedGit.Repo != "" {
log.Printf("Repo not allowed: %s", payload.Repository.FullName)
return false, nil, fmt.Errorf("repo not allowed: %s", payload.Repository.FullName)
return false, nil, tei2zenodo.NewError("errBadConfig", fmt.Sprintf("repo not allowed: %s", payload.Repository.FullName), 500, nil)
}
if payload.Pusher.Name != conf.AllowedGit.User && conf.AllowedGit.User != "" {
log.Printf("Git user not allowed as pusher: %s", payload.Pusher.Name)
return false, nil, fmt.Errorf("git user not allowed as pusher: %s", payload.Pusher.Name)
return false, nil, tei2zenodo.NewError("errBadConfig", fmt.Sprintf("git user not allowed as pusher: %s", payload.Pusher.Name), 500, nil)
}
if conf.Verbose {
log.Printf("Push payload: %+v", payload)
......@@ -302,18 +305,24 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
commitsURL := payload.Repository.CommitsURL
// Get all commits and their associated files
log.Printf("--- Get all commits and their associated files ---")
files := make(map[string]FileInfo)
for _, c := range payload.Commits {
sha := c.URL[strings.LastIndex(c.URL, "/")+1:]
log.Printf(" commit %s ...", sha)
if conf.Verbose {
log.Printf(" commit %s ...", sha)
}
if !(strings.Contains(c.Message, conf.AllowedGit.Keyword)) && conf.AllowedGit.Keyword != "" {
log.Printf("Required keyword not contained in commit message for commit %s", sha)
if conf.Verbose {
log.Printf("Required keyword not contained in commit message for commit %s", sha)
}
continue
}
f, err := retrieveFiles(commitsURL, sha, conf)
if err != nil {
log.Printf("Error retrieving files from commit %s: %+v", sha, err)
return false, nil, fmt.Errorf("error retrieving files from commit %s: %s", sha, err)
return false, nil, tei2zenodo.NewError("errInternal", fmt.Sprintf("error retrieving files from commit %s: %s", sha, err), 500, err)
}
for k, v := range f {
if old, ok := files[k]; ok { // file is already present
......@@ -324,16 +333,21 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
files[k] = v
}
}
if !(strings.Contains(c.Message, conf.AllowedGit.DontPublishKeyword)) && conf.AllowedGit.DontPublishKeyword != "" {
log.Printf("!!! Don't-publish-keyword '%s' contained in commit message for commit %s,\n", conf.AllowedGit.DontPublishKeyword, sha)
log.Printf("!!! not going to publish zenodo deposit !!!")
doPublish = false
}
}
// log.Printf("Parsed hook: %+v", payload)
if conf.Verbose {
log.Printf("Push hook with %d commits and %d files: %+v", len(payload.Commits), len(files), files)
log.Printf(" Parsed push hook with %d commits and %d files: %+v", len(payload.Commits), len(files), files)
} else {
fls := ""
var fls []string
for s := range files {
fls = fls + " " + s
fls = append(fls, s)
}
log.Printf("Push hook with %d commits and %d files: %s", len(payload.Commits), len(files), fls)
log.Printf(" Parsed push hook with %d commits and %d files: %s", len(payload.Commits), len(files), strings.Join(fls, ", "))
}
return doPublish, files, nil
}
......@@ -344,7 +358,7 @@ func ProcessHook(hookType string, r io.ReadSeeker, conf *tei2zenodo.Config) (boo
default:
{
log.Printf("Unknown hook type %s", hookType)
return false, nil, fmt.Errorf("unknown hook type %s", hookType)
return false, nil, tei2zenodo.NewError("errWebhook", fmt.Sprintf("hook type %s not implemented", hookType), 501, nil)
}
}
}
......@@ -359,15 +373,15 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
// Send GET request
if conf.Verbose {
log.Printf(" Get files at %s", targetURI)
log.Printf(" Get files at %s:", targetURI)
} else {
log.Printf(" Get files")
log.Printf(" Get files:")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Problem creating GET request: %v ...", err)
return files, fmt.Errorf("problem creating GET request: %s", err)
log.Printf("Problem sending GET request: %v ...", err)
return nil, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error sending GET request: %s", err.Error()), 500, err)
}
defer resp.Body.Close()
......@@ -375,11 +389,11 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Problem reading GET response: %v ...", err)
return files, fmt.Errorf("problem reading GET response: %s", err)
return nil, tei2zenodo.NewError("errNetComm", fmt.Sprintf("error reading GET request: %s", err.Error()), 500, err)
}
if strconv.Itoa(resp.StatusCode)[:1] != "2" {
log.Printf("Problem reported by github: %d %v. %+v ...", resp.StatusCode, err, content)
return files, fmt.Errorf("problem reported by github: %d %v", resp.StatusCode, err)
return nil, tei2zenodo.NewError("errNetComm", fmt.Sprintf("problem reported by github: %d %v", resp.StatusCode, err), 500, err)
}
// Parse response
......@@ -387,24 +401,30 @@ func retrieveFiles(commitsURL string, sha string, conf *tei2zenodo.Config) (map[
err = json.Unmarshal(content, &commit)
if err != nil {
log.Printf("Problem parsing github's response: %v ...", err)
return nil, fmt.Errorf("problem parsing github's response: %s", err)
return nil, tei2zenodo.NewError("errParse", fmt.Sprintf("problem parsing github's response: %s", err), 500, err)
}
// layout := "2006-01-02T15:04:05.000Z"
str := commit.Commit.Committer.Date
commitDate, err := time.Parse(time.RFC3339, str)
for i, f := range commit.Files {
counter := 0
for _, f := range commit.Files {
filename := f.Filename
url := f.RawURL
modType := f.Status
if conf.Verbose {
log.Printf(" %d. %s %s: %s", i+1, modType, filename, url)
} else {
log.Printf(" %d. %s %s", i+1, modType, filename)
}
if modType != "deleted" && modType != "renamed" {
counter++
if conf.Verbose {
log.Printf(" %d. '%s' (%s): %s", counter, filename, modType, url)
} else {
log.Printf(" %d. '%s' (%s)", counter, filename, modType)
}
files[filename] = FileInfo{URL: url, Change: modType, Date: commitDate}
} else {
if conf.Verbose {
log.Printf(" ignore %s file %s: %s", modType, filename, url)
}
}
}
return files, nil
......
......@@ -40,6 +40,12 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
var doPublish bool
var myDeposit tei2zenodo.Deposit
if conf.Verbose {
log.Printf("====== Received POST message from %s at %s ======", c.Request.RemoteAddr, conf.FileAPI)
} else {
log.Printf("====== Received POST message ======")
}
// Read file from Request body
buf := new(bytes.Buffer)
buf.ReadFrom(c.Request.Body)
......@@ -47,9 +53,16 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
r = strings.NewReader(file)
// Get filename for upload
filename := c.Request.FormValue(`filename`)
var filename string
filename = c.Request.FormValue(`filename`)
if filename == "" {
filename = t2zxml.GetFilename(r)
f, GFErr := t2zxml.GetFilename(r)
if GFErr != nil {
log.Printf("Problem reading filename: %+v", GFErr)
AbortMsg(500, tei2zenodo.NewError("errParse", fmt.Sprintf("Error reading filename: %s", GFErr), 500, GFErr), c)
return
}
filename = f
r.Seek(0, 0)
}
myDeposit.Filename = filename
......@@ -62,13 +75,26 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
}
// Send file to processing...
err := zenodo.ProcessFile(&conf, &myDeposit, r, doPublish)
if err != nil {
AbortMsg(500, err, c)
return
PFErr := zenodo.ProcessFile(&conf, &myDeposit, r, doPublish)
if PFErr != nil {
switch PFErr.Typ {
case "errNoTEIXML":
{
log.Printf("Problem processing file %s: %+v", myDeposit.Filename, PFErr)
AbortMsg(500, tei2zenodo.NewError("errZProcessing", fmt.Sprintf("problem processing file %s", myDeposit.Filename), 500, PFErr), c)
return
}
default:
{
log.Printf("Problem processing file %s: %+v", myDeposit.Filename, PFErr)
AbortMsg(500, tei2zenodo.NewError("errZProcessing", fmt.Sprintf("problem processing file %s", myDeposit.Filename), 500, PFErr), c)
return
}
}
}
log.Printf("=== All done ===")
log.Printf("====== All done ======")
c.JSON(200, myDeposit)
})
}
......@@ -79,6 +105,11 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
// Read hook type from header
hookType := c.Request.Header.Get("X-GitHub-Event")
if conf.Verbose {
log.Printf("====== Received %s message from %s at %s ======", hookType, c.Request.RemoteAddr, conf.WebhookAPI)
} else {
log.Printf("====== Received %s message ======", hookType)
}
// Read payload from Request body
buf := new(bytes.Buffer)
......@@ -87,27 +118,39 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
r = strings.NewReader(file)
// Process Hook to extract files
doPublish, files, err := github.ProcessHook(hookType, r, &conf)
if err != nil {
log.Printf("Error parsing webhook event: %v", err)
AbortMsg(500, fmt.Errorf("problem parsing webhook event: %s", err), c)
doPublish, files, PHErr := github.ProcessHook(hookType, r, &conf)
if PHErr != nil {
log.Printf("Error parsing webhook event: %v", PHErr)
AbortMsg(500, tei2zenodo.NewError("errWebhook", fmt.Sprintf("problem parsing webhook for %s event", hookType), 500, PHErr), c)
}
log.Printf("%d files returned", len(files))
log.Printf("doPublish: %s", strconv.FormatBool(doPublish))
// log.Printf(" %d files returned", len(files))
log.Printf(" doPublish: %s", strconv.FormatBool(doPublish))
// Send each file to processing
for f := range files {
var myDeposit tei2zenodo.Deposit
log.Printf("Processing file %s", f)
log.Printf("\n")
log.Printf("Processing file '%s'", f)
myDeposit.Filename = strings.Replace(f, "/", "_", -1)
err := zenodo.ProcessDownloadFile(&conf, &myDeposit, files[f].URL, doPublish)
if err != nil {
AbortMsg(500, err, c)
return
PDErr := zenodo.ProcessDownloadFile(&conf, &myDeposit, files[f].URL, doPublish)
if PDErr != nil {
switch PDErr.Typ {
case "errNoTEIXML":
{
log.Printf(" No TEI XML file, skipping...")
continue
}
default:
{
log.Printf("Problem processing file %s: %+v", myDeposit.Filename, PDErr)
AbortMsg(500, tei2zenodo.NewError("errZProcessing", fmt.Sprintf("problem processing file %s", myDeposit.Filename), 500, PDErr), c)
return
}
}
}
}
log.Printf("=== All done ===")
log.Printf("====== All done ======")
c.JSON(200, "processed hook successfully")
})
}
......@@ -116,7 +159,7 @@ func SetupRoutes(conf tei2zenodo.Config) *gin.Engine {
}
// AbortMsg returns an error code and message.
func AbortMsg(code int, err error, c *gin.Context) {
func AbortMsg(code int, err *tei2zenodo.Error, c *gin.Context) {
c.String(code, "Something has gone wrong, causing a %v error.\n", code)
// A custom error page with HTML templates can be shown by c.HTML()
// A JSON object would be given like c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Seite nicht gefunden"})
......
......@@ -15,34 +15,45 @@ import (
)
// GetFilename extracts what can serve as filename. Like /TEI/@xml:id
// it returns a string to serve as filename
func GetFilename(r io.Reader) string {
// it returns a string to serve as filename and an error value
func GetFilename(r io.Reader) (string, error) {
// Parse document (in r) wih antchfx/xmlquery...
doc, err := xmlquery.Parse(r)
if err != nil {
log.Printf("Could not parse xml.\n")
return ""
return "", tei2zenodo.NewError("errParse", "could not parse xml", 500, err)
}
t := xmlquery.FindOne(doc, `/TEI/@xml:id`)
if n := t; t != nil {
u := n.InnerText()
return u + ".xml"
return u + ".xml", nil
}
return ""
return "", nil
}
// ParseTEI reads a TEI file and parses its metadata into a ZMetadata variable.
// Returns a doi (maybe empty) and an error value.
func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, error) {
func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (string, *tei2zenodo.Error) {
conf := c.Metadata
var doc *xmlquery.Node
log.Printf(" Parse TEI XML file")
// Parse document r wih antchfx/xmlquery...
doc, err := xmlquery.Parse(r)
if err != nil {
doc, PErr := xmlquery.Parse(r)
if PErr != nil {
log.Printf("Could not parse xml.\n")
return "", err
return "", tei2zenodo.NewError("errParse", "could not parse xml", 500, PErr)
}
if doc == nil || (doc.FirstChild == nil && doc.InnerText() == "") {
log.Printf(" Parse returned no or empty node only.\n")
return "", tei2zenodo.NewError("errNoTEIXML", "not a TEI XML file", 400, nil)
}
rootElement := xmlquery.FindOne(doc, `/*[1]`)
if !(rootElement.NamespaceURI == "http://www.tei-c.org/ns/1.0" && rootElement.Data == "TEI") {
log.Printf(" This is not a TEI XML file. %+v", rootElement)
return "", tei2zenodo.NewError("errNoTEIXML", "not a TEI XML file", 400, nil)
}
re := regexp.MustCompile(`\s+`)
......@@ -114,7 +125,7 @@ func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (stri
xexpr, err := xpath.Compile(confSubfields[m].XExpression)
if err != nil {
log.Printf("Erroneous XPath expression: %s ...", confSubfields[m].XExpression)
return "", fmt.Errorf("erroneous XPath expression: %s", confSubfields[m].XExpression)
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("erroneous XPath expression: %s", confSubfields[m].XExpression), 500, err)
}
switch zStructFieldtype.String() {
case "string":
......@@ -139,12 +150,12 @@ func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (stri
default:
log.Printf("Unknown (hardcoded?) metadata type: %s.%s ...", structFieldtype, zStructFieldtype)
return "", fmt.Errorf("xml: unknown (hardcoded?) metadata type: %s.%s", structFieldtype, zStructFieldtype)
return "", tei2zenodo.NewError("errInternal", fmt.Sprintf("xml: unknown (hardcoded?) metadata type: %s.%s", structFieldtype, zStructFieldtype), 500, nil)
}
} else if confSubfields[m].Field == "name" || confSubfields[m].Field == "type" {
log.Printf("Problem with config: XPath or XExpression missing in %v ...", conf.Fields[j])
return "", fmt.Errorf("xml: malformed config (xpath/xexpression missing): %v", conf.Fields[j])
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("XPath or XExpression missing in %v ...", conf.Fields[j]), 500, nil)
}
}
}
......@@ -176,12 +187,13 @@ func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (stri
f.Set(newSlice)
default:
log.Printf("Problem with type conversion of %s (%s)", structFieldname, varType.Name())
return "", fmt.Errorf("xml: malformed config (type problem in %s [%s])", structFieldname, varType.Name())
return "", tei2zenodo.NewError("errInternal", fmt.Sprintf("type problem in %s [%s]", structFieldname, varType.Name()), 500, nil)
}
}
} else {
log.Printf("Problem with config: XPath missing in %v ...", conf.Fields[j])
return "", fmt.Errorf("xml: malformed config (xpath missing): %v", conf.Fields[j])
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("XPath or XExpression missing in %v ...", conf.Fields[j]), 500, nil)
}
} else if conf.Fields[j].XPath != "" {
......@@ -208,13 +220,13 @@ func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (stri
default:
log.Printf("Unknown (hardcoded?) metadata type: %s ...", structFieldtype)
return "", fmt.Errorf("xml: unknown (hardcoded?) metadata type: %s", structFieldtype)
return "", tei2zenodo.NewError("errInternal", fmt.Sprintf("xml: unknown (hardcoded?) metadata type: %s", structFieldtype), 500, nil)
}
} else if conf.Fields[j].XExpression != "" {
xexpr, err := xpath.Compile(conf.Fields[j].XExpression)
if err != nil {
log.Printf("Erroneous XPath expression: %s ...", conf.Fields[j].XExpression)
return "", fmt.Errorf("erroneous XPath expression: %s", conf.Fields[j].XExpression)
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("erroneous XPath expression: %s", conf.Fields[j].XExpression), 500, err)
}
switch structFieldtype.String() {
case "string":
......@@ -239,27 +251,27 @@ func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (stri
default:
log.Printf("Unknown (hardcoded?) metadata type: %s ...", structFieldtype)
return "", fmt.Errorf("xml: unknown (hardcoded?) metadata type: %s", structFieldtype)
return "", tei2zenodo.NewError("errInternal", fmt.Sprintf("xml: unknown (hardcoded?) metadata type: %s", structFieldtype), 500, nil)
}
} else {
log.Printf("Malformed config entry: %v ...", conf.Fields[j])
return "", fmt.Errorf("xml: malformed config entry: %v", conf.Fields[j])
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("XPath, XExpression or Subfields missing in %v ...", conf.Fields[j]), 500, nil)
}
}
}
}
doi := md.DOI
log.Printf("Success.")
log.Printf(" Success.")
if c.Verbose {
log.Printf(" Title: %v", md.Title)
log.Printf(" Title: %v", md.Title)
if len(md.Creators) > 0 {
log.Printf(" Creator 1: %v", md.Creators[0])
log.Printf(" Creator 1: %v", md.Creators[0])
}
if len(md.Contributors) > 0 {
log.Printf(" Contributor 1: %v", md.Contributors[0])
log.Printf(" Contributor 1: %v", md.Contributors[0])
}
log.Printf(" DOI: %s", doi)
log.Printf(" DOI: %s", doi)
}
return doi, nil
......@@ -269,18 +281,20 @@ func ParseTEI(r io.Reader, md *tei2zenodo.ZMetadata, c *tei2zenodo.Config) (stri
// it returns the string serialization of the new document and an error value
func MixinDOI(r io.Reader, doi string, c *tei2zenodo.Config) (string, error) {
log.Printf(" Mix new DOI in TEI document")
// Parse document (in r)...
var doc etree.Document
_, err := doc.ReadFrom(r)
if err != nil {
log.Printf("Could not parse xml.\n")
return "", err
return "", tei2zenodo.NewError("errParse", "could not parse xml", 500, err)
}
pStmt := doc.FindElement(`/TEI/teiHeader/fileDesc/publicationStmt`)
if pStmt == nil {
log.Printf("XML file had no /TEI/teiHeader/fileDesc/publicationStmt element.")
err := fmt.Errorf("XML file had no /TEI/teiHeader/fileDesc/publicationStmt element")
return "", err
return "", tei2zenodo.NewError("errParse", "XML file had no /TEI/teiHeader/fileDesc/publicationStmt element", 500, err)
}
topLevelIdno := pStmt.FindElement(`./idno`)
......@@ -317,13 +331,13 @@ func MixinDOI(r io.Reader, doi string, c *tei2zenodo.Config) (string, error) {
output, err := doc.WriteToString()
if err != nil {
log.Printf("Problem serializing xml to string.")
return "", err
log.Printf("Error serializing xml to string.")
return "", tei2zenodo.NewError("errSerializing", "problem serializing XML to string", 500, err)
} else if output == "" {
err := fmt.Errorf("serialization of xml resulted in empty string")
return "", err
log.Printf("Serializing xml resulted in empty string.")
return "", tei2zenodo.NewError("errSerializing", "serializing XML resulted in empty string", 500, err)
}
log.Printf("Success.")
log.Printf(" Success.")
return output, nil
}
......@@ -16,6 +16,8 @@ import (
"gitlab.gwdg.de/rg-mpg-de/tei2zenodo/internal/pkg/t2zxml"
)
// ====== Types ======
// DOIResponse stores a response from doi.org
type DOIResponse struct {
ResponseCode int
......@@ -43,19 +45,25 @@ type ZPostData struct {
Metadata *tei2zenodo.ZMetadata `json:"metadata"`
}
// ====== Functions ======
// ProcessDownloadFile takes a URL, downloads the file and continues processing
func ProcessDownloadFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, url string, doPublish bool) error {
func ProcessDownloadFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, url string, doPublish bool) *tei2zenodo.Error {
// Compile GET request
targetURI := url
req, err := http.NewRequest("GET", targetURI, nil)
req, RQErr := http.NewRequest("GET", targetURI, nil)
if RQErr != nil {
log.Printf("Problem creating GET request: %v ...", RQErr)
return tei2zenodo.NewError("errInternal", fmt.Sprintf("error creating GET request: %s", RQErr.Error()), 500, RQErr)
}
req.Header.Add("Authorization", "Bearer "+conf.AllowedGit.Token)
// Send GET request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Problem creating GET request: %v ...", err)
return fmt.Errorf("problem creating GET request: %s", err)
resp, RSPErr := client.Do(req)
if RSPErr != nil {
log.Printf("Problem sending GET request: %v ...", RSPErr)
return tei2zenodo.NewError("errNetComm", fmt.Sprintf("error sending GET request: %s", RSPErr.Error()), 500, RSPErr)
}
defer resp.Body.Close()
......@@ -66,59 +74,77 @@ func ProcessDownloadFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit,
file := buf.String()
r = strings.NewReader(file)
err = ProcessFile(conf, myDeposit, r, doPublish)
if err != nil {
log.Printf("Problem processing file: %v ...", err)
return fmt.Errorf("problem processing file: %s", err)
PFErr := ProcessFile(conf, myDeposit, r, doPublish)
if PFErr != nil {
switch PFErr.Typ {
case "errNoTEIXML":
{
return PFErr
}
default:
{
log.Printf("Problem processing file %s: %v ...", myDeposit.Filename, PFErr)
return tei2zenodo.NewError("errZProcessing", fmt.Sprintf("error processing file %s: %s", myDeposit.Filename, PFErr.Error()), 500, PFErr)
}
}
}
return nil
}
// ProcessFile takes a file and processes parsing and zenodo upload
func ProcessFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, r io.ReadSeeker, doPublish bool) error {
func ProcessFile(conf *tei2zenodo.Config, myDeposit *tei2zenodo.Deposit, r io.ReadSeeker, doPublish bool) *tei2zenodo.Error {
var md tei2zenodo.ZMetadata
// Parse TEI file
log.Printf("=== Parse TEI file ===")