From 761dcdc4a6b96d75841fa4e38b725b62b1cdb3a9 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Tue, 5 Dec 2023 19:00:46 +0800 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Antoine du Hamel --- packages/@uppy/companion/src/server/Uploader.js | 7 ++----- packages/@uppy/companion/src/server/provider/error.js | 6 +++--- .../@uppy/companion/src/server/provider/providerErrors.js | 4 ++-- .../@uppy/provider-views/src/ProviderView/ProviderView.jsx | 2 +- packages/@uppy/utils/src/UserFacingApiError.js | 5 +---- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/@uppy/companion/src/server/Uploader.js b/packages/@uppy/companion/src/server/Uploader.js index 663b0b4cc5..dd10fb9718 100644 --- a/packages/@uppy/companion/src/server/Uploader.js +++ b/packages/@uppy/companion/src/server/Uploader.js @@ -57,10 +57,7 @@ function sanitizeMetadata(inputMetadata) { } class AbortError extends Error { - constructor(message) { - super(message) - this.isAbortError = true - } + isAbortError = true } class ValidationError extends Error { @@ -317,7 +314,7 @@ class Uploader { const { url, extraData } = ret this.#emitSuccess(url, extraData) } catch (err) { - if (err.isAbortError) { + if (err?.isAbortError) { logger.error('Aborted upload', 'uploader.aborted', this.shortToken) return } diff --git a/packages/@uppy/companion/src/server/provider/error.js b/packages/@uppy/companion/src/server/provider/error.js index 758cab4188..efb4a53381 100644 --- a/packages/@uppy/companion/src/server/provider/error.js +++ b/packages/@uppy/companion/src/server/provider/error.js @@ -46,16 +46,16 @@ class ProviderAuthError extends ProviderApiError { */ function errorToResponse(err) { // @ts-ignore - if (err.isAuthError) { + if (err?.isAuthError) { return { code: 401, json: { message: err.message } } } - if (err.name === 'ProviderUserError') { + if (err?.name === 'ProviderUserError') { // @ts-ignore return { code: 400, json: err.json } } - if (err.name === 'ProviderApiError') { + if (err?.name === 'ProviderApiError') { // @ts-ignore if (err.statusCode >= 500) { // bad gateway i.e the provider APIs gateway diff --git a/packages/@uppy/companion/src/server/provider/providerErrors.js b/packages/@uppy/companion/src/server/provider/providerErrors.js index 5c77939f4f..715de19592 100644 --- a/packages/@uppy/companion/src/server/provider/providerErrors.js +++ b/packages/@uppy/companion/src/server/provider/providerErrors.js @@ -40,10 +40,10 @@ async function withProviderErrorHandling({ let statusCode let body - if (err.name === 'HTTPError') { + if (err?.name === 'HTTPError') { statusCode = err.response?.statusCode body = err.response?.body - } else if (err.name === 'StreamHttpJsonError') { + } else if (err?.name === 'StreamHttpJsonError') { statusCode = err.statusCode body = err.responseJson } diff --git a/packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx b/packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx index c5ba761934..68edf1bb88 100644 --- a/packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx +++ b/packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx @@ -187,7 +187,7 @@ export default class ProviderView extends View { } catch (err) { // This is the first call that happens when the provider view loads, after auth, so it's probably nice to show any // error occurring here to the user. - if (err.name === 'UserFacingApiError') { + if (err?.name === 'UserFacingApiError') { this.plugin.uppy.info({ message: this.plugin.uppy.i18n(err.message) }, 'warning', 5000) return } diff --git a/packages/@uppy/utils/src/UserFacingApiError.js b/packages/@uppy/utils/src/UserFacingApiError.js index 68330275c7..1bfe9da950 100644 --- a/packages/@uppy/utils/src/UserFacingApiError.js +++ b/packages/@uppy/utils/src/UserFacingApiError.js @@ -1,8 +1,5 @@ class UserFacingApiError extends Error { - constructor (message) { - super(message) - this.name = 'UserFacingApiError' - } + name = 'UserFacingApiError' } export default UserFacingApiError