Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/transloadit: fix handling of Tus errors and rate limiting #3429

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions packages/@uppy/transloadit/src/Client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
const fetchWithNetworkError = require('@uppy/utils/lib/fetchWithNetworkError')
const NetworkError = require('@uppy/utils/lib/NetworkError')

function fetchJSON (...args) {
return fetchWithNetworkError(...args).then(response => {
if (response.status === 429) {
// If the server asks the client to rate limit, reschedule the request 5s later.
return new Promise((resolve, reject) => {
setTimeout(() => fetchJSON(...args).then(resolve, reject), 5_000)
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
})
}

if (!response.ok) {
return Promise.reject(new NetworkError(response.statusText))
}

return response.json()
})
}

/**
* A Barebones HTTP API client for Transloadit.
Expand Down Expand Up @@ -43,12 +61,12 @@ module.exports = class Client {
data.append('num_expected_upload_files', expectedFiles)

const url = new URL('/assemblies', `${this.opts.service}`).href
return fetchWithNetworkError(url, {
return fetchJSON(url, {
method: 'post',
headers: this.#headers,
body: data,
})
.then((response) => response.json()).then((assembly) => {
.then((assembly) => {
if (assembly.error) {
const error = new Error(assembly.error)
error.details = assembly.message
Expand All @@ -73,8 +91,7 @@ module.exports = class Client {
reserveFile (assembly, file) {
const size = encodeURIComponent(file.size)
const url = `${assembly.assembly_ssl_url}/reserve_file?size=${size}`
return fetchWithNetworkError(url, { method: 'post', headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { method: 'post', headers: this.#headers })
.catch((err) => this.#reportError(err, { assembly, file, url, type: 'API_ERROR' }))
}

Expand All @@ -95,8 +112,7 @@ module.exports = class Client {

const qs = `size=${size}&filename=${filename}&fieldname=${fieldname}&s3Url=${uploadUrl}`
const url = `${assembly.assembly_ssl_url}/add_file?${qs}`
return fetchWithNetworkError(url, { method: 'post', headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { method: 'post', headers: this.#headers })
.catch((err) => this.#reportError(err, { assembly, file, url, type: 'API_ERROR' }))
}

Expand All @@ -107,8 +123,7 @@ module.exports = class Client {
*/
cancelAssembly (assembly) {
const url = assembly.assembly_ssl_url
return fetchWithNetworkError(url, { method: 'delete', headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { method: 'delete', headers: this.#headers })
.catch((err) => this.#reportError(err, { url, type: 'API_ERROR' }))
}

Expand All @@ -118,8 +133,7 @@ module.exports = class Client {
* @param {string} url The status endpoint of the assembly.
*/
getAssemblyStatus (url) {
return fetchWithNetworkError(url, { headers: this.#headers })
.then((response) => response.json())
return fetchJSON(url, { headers: this.#headers })
.catch((err) => this.#reportError(err, { url, type: 'STATUS_ERROR' }))
}

Expand All @@ -128,7 +142,7 @@ module.exports = class Client {
? `${err.message} (${err.details})`
: err.message

return fetchWithNetworkError('https://transloaditstatus.com/client_error', {
return fetchJSON('https://transloaditstatus.com/client_error', {
method: 'post',
body: JSON.stringify({
endpoint,
Expand All @@ -139,7 +153,6 @@ module.exports = class Client {
error: message,
}),
})
.then((response) => response.json())
}

#reportError = (err, params) => {
Expand Down
13 changes: 7 additions & 6 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,21 +694,22 @@ module.exports = class Transloadit extends BasePlugin {
})
}

#closeAssemblyIfExists = (assemblyID) => {
this.activeAssemblies[assemblyID]?.close()
}

#onError = (err = null, uploadID) => {
const state = this.getPluginState()
const assemblyIDs = state.uploadsAssemblies[uploadID]
assemblyIDs?.forEach(this.#closeAssemblyIfExists)

assemblyIDs?.forEach((assemblyID) => {
if (this.activeAssemblies[assemblyID]) {
this.activeAssemblies[assemblyID].close()
}
})
this.client.submitError(err)
// if we can't report the error that sucks
.catch(sendErrorToConsole(err))
}

#onTusError = (err) => {
#onTusError = (file, err) => {
this.#closeAssemblyIfExists(file.transloadit?.assembly)
if (err?.message?.startsWith('tus: ')) {
const endpoint = err.originalRequest?.getUnderlyingObject()?.responseURL
this.client.submitError(err, { endpoint, type: 'TUS_ERROR' })
Expand Down