]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/helpers/utils/upload.ts
Don't display unknown information
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / utils / upload.ts
1 import { HttpErrorResponse } from '@angular/common/http'
2 import { Notifier } from '@app/core'
3 import { HttpStatusCode } from '@shared/models'
4
5 function genericUploadErrorHandler (options: {
6 err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>
7 name: string
8 notifier: Notifier
9 sticky?: boolean
10 }) {
11 const { err, name, notifier, sticky = false } = options
12 const title = $localize`Upload failed`
13 const message = buildMessage(name, err)
14
15 notifier.error(message, title, null, sticky)
16 return message
17 }
18
19 export {
20 genericUploadErrorHandler
21 }
22
23 // ---------------------------------------------------------------------------
24
25 function buildMessage (name: string, err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>) {
26 if (err instanceof ErrorEvent) { // network error
27 return $localize`The connection was interrupted`
28 }
29
30 if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) {
31 return $localize`The server encountered an error`
32 }
33
34 if (err.status === HttpStatusCode.REQUEST_TIMEOUT_408) {
35 return $localize`Your ${name} file couldn't be transferred before the server proxy timeout`
36 }
37
38 if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) {
39 const maxFileSize = err.headers?.get('X-File-Maximum-Size')
40 let message = $localize`Your ${name} file was too large `
41
42 if (maxFileSize) message += $localize` (max. size: ${maxFileSize})`
43
44 return message
45 }
46
47 return err.message
48 }