ソースを参照

companion-client: use different informer message for auth errors

Ifedapo Olarewaju 6 年 前
コミット
cb7a209037

+ 11 - 0
packages/@uppy/companion-client/src/AuthError.js

@@ -0,0 +1,11 @@
+'use strict'
+
+class AuthError extends Error {
+  constructor () {
+    super('Authorization required')
+    this.name = 'AuthError'
+    this.isAuthError = true
+  }
+}
+
+module.exports = AuthError

+ 9 - 4
packages/@uppy/companion-client/src/RequestClient.js

@@ -1,5 +1,7 @@
 'use strict'
 
+const AuthError = require('./AuthError')
+
 // Remove the trailing slash so we can always safely append /xyz.
 function stripSlash (url) {
   return url.replace(/\/$/, '')
@@ -64,7 +66,7 @@ module.exports = class RequestClient {
 
   _json(res) {
     if (res.status === 401) {
-      throw new Error('Auth required')
+      throw new AuthError()
     }
 
     if (res.status < 200 || res.status > 300) {
@@ -84,7 +86,8 @@ module.exports = class RequestClient {
           .then(this._getPostResponseFunc(skipPostResponse))
           .then((res) => this._json(res).then(resolve))
           .catch((err) => {
-            reject(new Error(`Could not get ${this._getUrl(path)}. ${err}`))
+            err = err.isAuthError ? err : new Error(`Could not get ${this._getUrl(path)}. ${err}`)
+            reject(err)
           })
       })
     })
@@ -102,7 +105,8 @@ module.exports = class RequestClient {
           .then(this._getPostResponseFunc(skipPostResponse))
           .then((res) => this._json(res).then(resolve))
           .catch((err) => {
-            reject(new Error(`Could not post ${this._getUrl(path)}. ${err}`))
+            err = err.isAuthError ? err : new Error(`Could not post ${this._getUrl(path)}. ${err}`)
+            reject(err)
           })
       })
     })
@@ -120,7 +124,8 @@ module.exports = class RequestClient {
           .then(this._getPostResponseFunc(skipPostResponse))
           .then((res) => this._json(res).then(resolve))
           .catch((err) => {
-            reject(new Error(`Could not delete ${this._getUrl(path)}. ${err}`))
+            err = err.isAuthError ? err : new Error(`Could not delete ${this._getUrl(path)}. ${err}`)
+            reject(err)
           })
       })
     })

+ 1 - 0
packages/@uppy/core/src/index.js

@@ -36,6 +36,7 @@ class Uppy {
         exceedsSize: 'This file exceeds maximum allowed size of',
         youCanOnlyUploadFileTypes: 'You can only upload: %{types}',
         companionError: 'Connection with Companion failed',
+        companionAuthError: 'Authorization required',
         failedToUpload: 'Failed to upload %{file}',
         noInternetConnection: 'No Internet connection',
         connectedToInternet: 'Connected to the Internet',

+ 1 - 1
packages/@uppy/provider-views/src/index.js

@@ -452,8 +452,8 @@ module.exports = class ProviderView {
 
   handleError (error) {
     const uppy = this.plugin.uppy
-    const message = uppy.i18n('companionError')
     uppy.log(error.toString())
+    const message = uppy.i18n(error.isAuthError ? 'companionAuthError' : 'companionError')
     uppy.info({message: message, details: error.toString()}, 'error', 5000)
   }