aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/helpers/utils/upload.ts
blob: b609516128bd47d37ee28835d5434a77632c1ce4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { HttpErrorResponse } from '@angular/common/http'
import { Notifier } from '@app/core'
import { HttpStatusCode } from '@shared/models'

function genericUploadErrorHandler (options: {
  err: Pick<HttpErrorResponse, 'message' | 'status' | 'headers'>
  name: string
  notifier?: Notifier
  sticky?: boolean
}) {
  const { err, name, notifier, sticky = false } = options
  const title = $localize`Upload failed`
  const message = buildMessage(name, err)

  if (notifier) 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')
    let message = $localize`Your ${name} file was too large `

    if (maxFileSize) message += $localize` (max. size: ${maxFileSize})`

    return message
  }

  return err.message
}