-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@uppy/companion: unify http error responses (#5595)
* improve comments * unify http error responses when proxying requests this in order to make it easier to debug when setting up companion/transloadit integration, and lessen support burden on us. remove outdated `err.status` checks. this was added [7+ years ago](https://github.com/transloadit/uppy/blame/cf18689c1055055fc73a33fb9fe18e1046dfc8e4/packages/%40uppy/companion/src/standalone/index.js#L143) and we now use `got` which doesn't provide err.status Instead, for any other unhandled proxied HTTP request error responses, be nice and forward the JSON response to the client for easier debugging * Update packages/@uppy/companion/src/server/provider/error.js
- Loading branch information
Showing
10 changed files
with
85 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,38 @@ | ||
const Uploader = require('../Uploader') | ||
const logger = require('../logger') | ||
const { respondWithError } = require('../provider/error') | ||
|
||
async function startDownUpload({ req, res, getSize, download }) { | ||
try { | ||
logger.debug('Starting download stream.', null, req.id) | ||
const { stream, size: maybeSize } = await download() | ||
logger.debug('Starting download stream.', null, req.id) | ||
const { stream, size: maybeSize } = await download() | ||
|
||
let size | ||
// if the provider already knows the size, we can use that | ||
if (typeof maybeSize === 'number' && !Number.isNaN(maybeSize) && maybeSize > 0) { | ||
size = maybeSize | ||
} | ||
// if not we need to get the size | ||
if (size == null) { | ||
size = await getSize() | ||
} | ||
const { clientSocketConnectTimeout } = req.companion.options | ||
|
||
logger.debug('Instantiating uploader.', null, req.id) | ||
const uploader = new Uploader(Uploader.reqToOptions(req, size)) | ||
|
||
// "Forking" off the upload operation to background, so we can return the http request: | ||
; (async () => { | ||
// wait till the client has connected to the socket, before starting | ||
// the download, so that the client can receive all download/upload progress. | ||
logger.debug('Waiting for socket connection before beginning remote download/upload.', null, req.id) | ||
await uploader.awaitReady(clientSocketConnectTimeout) | ||
logger.debug('Socket connection received. Starting remote download/upload.', null, req.id) | ||
let size | ||
// if the provider already knows the size, we can use that | ||
if (typeof maybeSize === 'number' && !Number.isNaN(maybeSize) && maybeSize > 0) { | ||
size = maybeSize | ||
} | ||
// if not we need to get the size | ||
if (size == null) { | ||
size = await getSize() | ||
} | ||
const { clientSocketConnectTimeout } = req.companion.options | ||
|
||
await uploader.tryUploadStream(stream, req) | ||
})().catch((err) => logger.error(err)) | ||
logger.debug('Instantiating uploader.', null, req.id) | ||
const uploader = new Uploader(Uploader.reqToOptions(req, size)) | ||
|
||
// Respond the request | ||
// NOTE: the Uploader will continue running after the http request is responded | ||
res.status(200).json({ token: uploader.token }) | ||
} catch (err) { | ||
if (err.name === 'ValidationError') { | ||
logger.debug(err.message, 'uploader.validator.fail') | ||
res.status(400).json({ message: err.message }) | ||
return | ||
} | ||
// "Forking" off the upload operation to background, so we can return the http request: | ||
; (async () => { | ||
// wait till the client has connected to the socket, before starting | ||
// the download, so that the client can receive all download/upload progress. | ||
logger.debug('Waiting for socket connection before beginning remote download/upload.', null, req.id) | ||
await uploader.awaitReady(clientSocketConnectTimeout) | ||
logger.debug('Socket connection received. Starting remote download/upload.', null, req.id) | ||
|
||
if (respondWithError(err, res)) return | ||
await uploader.tryUploadStream(stream, req) | ||
})().catch((err) => logger.error(err)) | ||
|
||
throw err | ||
} | ||
// Respond the request | ||
// NOTE: the Uploader will continue running after the http request is responded | ||
res.status(200).json({ token: uploader.token }) | ||
} | ||
|
||
module.exports = { startDownUpload } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters