]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/helpers/utils/upload.ts
Don't display unknown information
[github/Chocobozzz/PeerTube.git] / client / src / app / helpers / utils / upload.ts
CommitLineData
dd24f1bb
C
1import { HttpErrorResponse } from '@angular/common/http'
2import { Notifier } from '@app/core'
3import { HttpStatusCode } from '@shared/models'
4
eaa52952 5function genericUploadErrorHandler (options: {
dd24f1bb
C
6 err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>
7 name: string
8 notifier: Notifier
9 sticky?: boolean
10}) {
eaa52952
C
11 const { err, name, notifier, sticky = false } = options
12 const title = $localize`Upload failed`
13 const message = buildMessage(name, err)
dd24f1bb 14
eaa52952 15 notifier.error(message, title, null, sticky)
dd24f1bb
C
16 return message
17}
18
19export {
20 genericUploadErrorHandler
21}
eaa52952
C
22
23// ---------------------------------------------------------------------------
24
25function 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) {
f5a12121
C
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
eaa52952
C
45 }
46
47 return err.message
48}