tus-js-client icon indicating copy to clipboard operation
tus-js-client copied to clipboard

Improve error message when error occurs in user's callback

Open Acconut opened this issue 2 years ago • 0 comments

Consider following code:

input.addEventListener("change", function(e) {
    // Get the selected file from the input element
    var file = e.target.files[0]

    // Create a new tus upload
    var upload = new tus.Upload(file, {
        endpoint: "http://localhost:1080/files/",
        retryDelays: [0, 3000, 5000, 10000, 20000],
        metadata: {
            filename: file.name,
            filetype: file.type
        },
        onError: function(error) {
            console.log("Failed because: " + error)
        },
        onProgress: function(bytesUploaded, bytesTotal) {
            // This will cause an error
           someThing.someProperty
        },
        onSuccess: function() {
            console.log("Download %s from %s", upload.file.name, upload.url)
        }
    })

    // Start the upload
    upload.start()
})

This will cause the upload to fail with an error, such as Error: tus: failed to upload chunk at offset 0, caused by TypeError: Cannot read properties of undefined (reading 'someProperty'), originated from request. It looks like it is an internal failure of tus-js-client but actually it is coming from the user's callback, confusing the end user. This happened in #293 and #330, for example.

I propose to catch error when callbacks are invoked and bubble them up with a proper error description explaining the cause.

Working callbacks:

  • [x] onSuccess
  • [ ] onError
  • [ ] onProgress
  • [ ] onChunkComplete
  • [ ] onShouldRetry
  • [ ] onBeforeRequest
  • [ ] onAfterResponse

Acconut avatar Oct 29 '21 08:10 Acconut