]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/helpers/utils/upload.ts
Support ICU in TS components
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / utils / upload.ts
index a3fce7fee721f168c5ef7a5eb8523cc6f540641a..5c2600a0d45c78c0f5dc09a0258d8072748bbb5f 100644 (file)
@@ -2,36 +2,43 @@ import { HttpErrorResponse } from '@angular/common/http'
 import { Notifier } from '@app/core'
 import { HttpStatusCode } from '@shared/models'
 
-function genericUploadErrorHandler (parameters: {
+function genericUploadErrorHandler (options: {
   err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>
   name: string
   notifier: Notifier
   sticky?: boolean
 }) {
-  const { err, name, notifier, sticky } = { sticky: false, ...parameters }
-  const title = $localize`The upload failed`
-  let message = err.message
-
-  if (err instanceof ErrorEvent) { // network error
-    message = $localize`The connection was interrupted`
-    notifier.error(message, title, null, sticky)
-  } else if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) {
-    message = $localize`The server encountered an error`
-    notifier.error(message, title, null, sticky)
-  } else if (err.status === HttpStatusCode.REQUEST_TIMEOUT_408) {
-    message = $localize`Your ${name} file couldn't be transferred before the set timeout (usually 10min)`
-    notifier.error(message, title, null, sticky)
-  } else if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) {
-    const maxFileSize = err.headers?.get('X-File-Maximum-Size') || '8G'
-    message = $localize`Your ${name} file was too large (max. size: ${maxFileSize})`
-    notifier.error(message, title, null, sticky)
-  } else {
-    notifier.error(err.message, title)
-  }
+  const { err, name, notifier, sticky = false } = options
+  const title = $localize`Upload failed`
+  const message = buildMessage(name, err)
 
+  notifier.error(message, title, null, sticky)
   return message
 }
 
 export {
   genericUploadErrorHandler
 }
+
+// ---------------------------------------------------------------------------
+
+function buildMessage (name: string, err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>) {
+  if (err instanceof ErrorEvent) { // network error
+    return $localize`The connection was interrupted`
+  }
+
+  if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) {
+    return $localize`The server encountered an error`
+  }
+
+  if (err.status === HttpStatusCode.REQUEST_TIMEOUT_408) {
+    return $localize`Your ${name} file couldn't be transferred before the server proxy timeout`
+  }
+
+  if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) {
+    const maxFileSize = err.headers?.get('X-File-Maximum-Size') || '8G'
+    return $localize`Your ${name} file was too large (max. size: ${maxFileSize})`
+  }
+
+  return err.message
+}