javascriptpriority-web-sdk

Recovering from errors from Priority


I am running a piece of code to upload files to Priority using the Priority-Web-SDK. When everything is squared away, it works as expected. (files are uploaded, fields are filled in, etc) When, for example, a file has an extension not allowed by priority, uploadFile() returns an error as expected. However, subsequent commands fail with this message:

A previous request has failed, causing all subsequent requests to fail

The result of this is that if I have four files to upload, and the second one fails, I can not upload the next two.

This is the loop that is causing my issues:

for (let file of files) {
    await baseForm.uploadFile(file, updateFileProgress)
        .then((u) => uploadResult = u)
        .then(() => baseForm.startSubForm(SUB_FORM))
        .then((r) => subForm = r)
        .then(() => subForm.newRow())
        .then(() => subForm.fieldUpdate("EXTFILENAME", uploadResult.file))
        .then(() => subForm.fieldUpdate("ORIG_FILENAME", file.name))
        .then(() => subForm.saveRow(1)) //Close subForm
        .catch((error) => {
            baseForm.startSubForm(SUB_FORM)
                .then((r) => subForm = r)
                .then(() => subForm.newRow())
                .then(() => subForm.fieldUpdate("ORIG_FILENAME", file.name))
                .then(() => subForm.fieldUpdate("INTERNAL_ERR", "Upload Error: " + file.name + " " +error.message))
                .then(() => subForm.saveRow(1)) //Close subForm
                .catch((error2) => {
                    uploadEnd(file.name + ": " + error2.message)
                })
        })
}

*The await keeps the uploads running sequentially.
*uploadEnd() closes the program on success or failure

Is there a way to reset the connection without restarting the entire process from login()?


Solution

  • I'm answering my own questions because I was able to come to a solution before anyone else chimed in...

    There are two things going on here in parallel that got confused. (I think...)

    1) The primary catch() runs synchronously 2) The primary catch() was not saving the row or closing the sub form properly. This is the issue that was throwing errors, and not, as I had thought the error thrown by uploadFile() which was being handled correctly.

    Now, looking at this with hindsight, it seems that they are quite connected and solving the synchronicity issue would solve the second issue as well. However, I also think it is more semantically and operationally correct to only open the sub form once and close it once. Moving baseForm.startSubForm(SUB_FORM) outside the form loop fixes that.

    Once I understood that the catch() runs synchronously, it was a simple matter of adding another async and await and the code now works as intended:

    .then(() => baseForm.startSubForm(SUB_FORM))
    .then((r) => subForm = r)
    .then(async () => {
        for (let file of files) {
            uploadFileStart(file);
            await subForm.uploadFile(file, updateFileProgress)
                .then((u) => uploadResult = u)
                .then(() => subForm.newRow())
                .then(() => subForm.fieldUpdate("EXTFILENAME", uploadResult.file))
                .then(() => subForm.fieldUpdate("ORIG_FILENAME", file.name))
                .then(() => subForm.saveRow()) //Do not close subForm
                .catch(async (error) => {
                    await subForm.undo()
                        .then(() => subForm.newRow())
                        .then(() => subForm.fieldUpdate("ORIG_FILENAME", file.name))
                        .then(() => subForm.fieldUpdate("INTERNAL_ERR", "Upload Error: " + file.name + " " +error.message))
                        .then(() => subForm.saveRow())  //Do not close subForm
                        .catch((error2) => {
                            uploadEnd(file.name + ": " + error2.message)
                        })
                })
        }
    })
    .then(() => subForm.endCurrentForm())  //Close subForm
    

    I would appreciate any comments that would help streamline the code more.