Error Handling

The recommend strategy for handling errors and list of all errors to handle

Recommend error handling

Errors must be caught with a catch following the upload promise. Alternatively, using async / await, you can catch errors with try / catch instead. There are quite a few errors that can be thrown. Instead of handling all errors separately, it's recommend you handle it as follows:

Promises
Async / Await
Promises
const networkErrors = ["upload_failed", "upload_could_not_initiate"];
const file = document.getElementById("file-input").files[0];
const task = UC.upload({
providerId: "<YOUR_PROVIDER_ID>",
files: [{ file: file }]
});
task.start()
.then((uploaded) => {
// Handle successful upload
})
.catch((e) => {
if (networkErrors.indexOf(e.code) !== -1) {
// Message to user: Upload failed due to network error
}
else {
// Message to user: Something went wrong internally
// Log error in your error tracker to follow up on
// e.g. Sentry.captureException(e);
}
});
Async / Await
const networkErrors = ["upload_failed", "upload_could_not_initiate"];
async function upload() {
const file = document.getElementById("file-input").files[0];
const task = UC.upload({
providerId: "<YOUR_PROVIDER_ID>",
files: [{ file: file }]
});
try {
const uploaded = await task.start();
// Handle successful upload
}
catch (e) {
if (networkErrors.indexOf(e.code) !== -1) {
// Message to user: Upload failed due to network error
}
else {
// Message to user: Something went wrong internally
// Log error in your error tracker to follow up on
// e.g. Sentry.captureException(e);
}
}
}

List of errors

Error code

Error message

missing_api_key

You must set an API key before uploading files

missing_provider_id

You must set a 'providerId' in upload options

task_already_executed

An upload task may only be started once

task_already_aborted

Upload task already aborted, it can no longer start

task_no_files

Property 'files' is missing or is an empty array

mismatch_length_signed_urls_and_files

Something went wrong with our service, contact us

internal_error

Something went wrong with our service, contact us

invalid_api_key

API key doesn't match any services

invalid_provider_id

Provider ID doesn't match any service providers

not_enough_uploads

Not enough upload credits on this service

upload_aborted

The upload request was aborted

upload_failed

One or more files failed to upload due to a network error or an error HTTP response code

upload_could_not_initiate

The upload request for one or more files failed, likely due to mis-configured CORS