Skip to content
Snippets Groups Projects
Commit 18705152 authored by Christian Pointner's avatar Christian Pointner
Browse files

return file object even if there is a problem with the import

parent 5be80859
No related branches found
No related tags found
No related merge requests found
...@@ -88,32 +88,20 @@ func (api *API) CreateFileForShow(c *gin.Context) { ...@@ -88,32 +88,20 @@ func (api *API) CreateFileForShow(c *gin.Context) {
sendError(c, err) sendError(c, err)
return return
} }
// TODO: from here on, in case of an error we should either delete the newly created
// file or at least return the id of the created file so the UI can deal with it.
// From here on, in case of an error we should either delete the newly created
// file or at least return the id of it so the API user can deal with the problem.
status := http.StatusCreated
refID := "" // TODO: get this from query paremeter refID := "" // TODO: get this from query paremeter
job, err := api.importer.CreateJob(showID, file.ID, srcURI, sess.Username, refID) job, err := api.importer.CreateJob(showID, file.ID, srcURI, sess.Username, refID)
if err != nil { if err != nil {
// possible errors: status, _ = statusCodeFromError(err)
// - any error returned by store.GetFile(): goto create_file_response
// possible reasons for this:
// * connection problem to the store database
// * somebody deleted the file
// - importer.ErrFileNotNew -> leave file
sendError(c, err)
return
} }
if err = job.Start(context.Background(), 3*time.Hour); err != nil { // TODO: hardcoded value if err = job.Start(context.Background(), 3*time.Hour); err != nil { // TODO: hardcoded value
// possible errors: status, _ = statusCodeFromError(err)
// - importer.ErrAlreadyCanceled -> ?? goto create_file_response
// - error while creating job workdir -> ??
// - importer.ErrTooManyJobs -> ??
sendError(c, err)
return
}
if waitFor == "" {
c.JSON(http.StatusCreated, file)
return
} }
switch waitFor { switch waitFor {
...@@ -122,14 +110,22 @@ func (api *API) CreateFileForShow(c *gin.Context) { ...@@ -122,14 +110,22 @@ func (api *API) CreateFileForShow(c *gin.Context) {
case "done": case "done":
<-job.Done() <-job.Done()
} }
create_file_response:
if file, err = api.store.GetFile(showID, file.ID); err != nil { if file, err = api.store.GetFile(showID, file.ID); err != nil {
// possible reasons why this failed: // GetFile only fails if the file does not exist or there is
// - connection problem to the store database // a problem with the database connection. The former probably
// - somebody deleted the file // means that the file got deleted by somebody else... all other
sendError(c, err) // errors most likely mean that the file still exists. In this
return // case it's better to return the file so the API user can use
} // the file ID for further inspection.
c.JSON(http.StatusCreated, file) if err == store.ErrNotFound {
sendError(c, err)
return
}
status, _ = statusCodeFromError(err)
}
c.JSON(status, file)
} }
func (api *API) ReadFileOfShow(c *gin.Context) { func (api *API) ReadFileOfShow(c *gin.Context) {
......
...@@ -38,9 +38,10 @@ func idFromString(s string) (uint64, error) { ...@@ -38,9 +38,10 @@ func idFromString(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 64) return strconv.ParseUint(s, 10, 64)
} }
func sendError(c *gin.Context, err error) { func statusCodeFromError(err error) (code int, response ErrorResponse) {
code := http.StatusInternalServerError code = http.StatusInternalServerError
response := ErrorResponse{Error: err.Error()} response = ErrorResponse{Error: err.Error()}
switch err.(type) { switch err.(type) {
case *store.ErrFileInUse: case *store.ErrFileInUse:
code = http.StatusConflict code = http.StatusConflict
...@@ -83,6 +84,11 @@ func sendError(c *gin.Context, err error) { ...@@ -83,6 +84,11 @@ func sendError(c *gin.Context, err error) {
code = http.StatusConflict code = http.StatusConflict
} }
} }
return
}
func sendError(c *gin.Context, err error) {
code, response := statusCodeFromError(err)
c.JSON(code, response) c.JSON(code, response)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment