]>
Commit | Line | Data |
---|---|---|
dd24f1bb C |
1 | import { HttpErrorResponse } from '@angular/common/http' |
2 | import { Notifier } from '@app/core' | |
3 | import { HttpStatusCode } from '@shared/models' | |
4 | ||
eaa52952 | 5 | function 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 | ||
19 | export { | |
20 | genericUploadErrorHandler | |
21 | } | |
eaa52952 C |
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') || '8G' | |
40 | return $localize`Your ${name} file was too large (max. size: ${maxFileSize})` | |
41 | } | |
42 | ||
43 | return err.message | |
44 | } |