]>
Commit | Line | Data |
---|---|---|
a51bad1a | 1 | import { throwError as observableThrowError } from 'rxjs' |
f228e9f0 | 2 | import { Inject, Injectable, LOCALE_ID } from '@angular/core' |
a51bad1a | 3 | import { Router } from '@angular/router' |
f228e9f0 | 4 | import { DateFormat, dateToHuman } from '@app/helpers' |
42b40636 | 5 | import { logger } from '@root-helpers/logger' |
f228e9f0 | 6 | import { HttpStatusCode, ResultList } from '@shared/models' |
de59c48f C |
7 | |
8 | @Injectable() | |
9 | export class RestExtractor { | |
10 | ||
f228e9f0 C |
11 | constructor ( |
12 | @Inject(LOCALE_ID) private localeId: string, | |
13 | private router: Router | |
14 | ) { } | |
a51bad1a | 15 | |
9df52d66 C |
16 | applyToResultListData <T, A, U> ( |
17 | result: ResultList<T>, | |
18 | fun: (data: T, ...args: A[]) => U, | |
19 | additionalArgs: A[] = [] | |
20 | ): ResultList<U> { | |
d592e0a9 | 21 | const data: T[] = result.data |
de59c48f | 22 | |
d592e0a9 C |
23 | return { |
24 | total: result.total, | |
9df52d66 | 25 | data: data.map(d => fun.apply(this, [ d, ...additionalArgs ])) |
d592e0a9 | 26 | } |
de59c48f C |
27 | } |
28 | ||
f228e9f0 C |
29 | convertResultListDateToHuman <T> ( |
30 | result: ResultList<T>, | |
31 | fieldsToConvert: string[] = [ 'createdAt' ], | |
32 | format?: DateFormat | |
33 | ): ResultList<T> { | |
34 | return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert, format ]) | |
de59c48f C |
35 | } |
36 | ||
f228e9f0 | 37 | convertDateToHuman (target: any, fieldsToConvert: string[], format?: DateFormat) { |
9df52d66 | 38 | fieldsToConvert.forEach(field => { |
f228e9f0 | 39 | target[field] = dateToHuman(this.localeId, new Date(target[field]), format) |
9df52d66 | 40 | }) |
bf68dd75 | 41 | |
61bbc727 | 42 | return target |
d592e0a9 | 43 | } |
de59c48f | 44 | |
eaa52952 C |
45 | redirectTo404IfNotFound (obj: { status: number }, type: 'video' | 'other', status = [ HttpStatusCode.NOT_FOUND_404 ]) { |
46 | if (obj?.status && status.includes(obj.status)) { | |
47 | // Do not use redirectService to avoid circular dependencies | |
48 | this.router.navigate([ '/404' ], { state: { type, obj }, skipLocationChange: true }) | |
49 | } | |
d592e0a9 | 50 | |
eaa52952 C |
51 | return observableThrowError(() => obj) |
52 | } | |
13fb4de9 | 53 | |
eaa52952 C |
54 | handleError (err: any) { |
55 | const errorMessage = this.buildErrorMessage(err) | |
de59c48f | 56 | |
c199c427 | 57 | const errorObj: { message: string, status: string, body: string } = { |
bfb3a98f | 58 | message: errorMessage, |
c9d6d155 C |
59 | status: undefined, |
60 | body: undefined | |
bfb3a98f C |
61 | } |
62 | ||
63 | if (err.status) { | |
64 | errorObj.status = err.status | |
c9d6d155 | 65 | errorObj.body = err.error |
bfb3a98f C |
66 | } |
67 | ||
1378c0d3 | 68 | return observableThrowError(() => errorObj) |
de59c48f | 69 | } |
a51bad1a | 70 | |
eaa52952 C |
71 | private buildErrorMessage (err: any) { |
72 | if (err.error instanceof Error) { | |
73 | // A client-side or network error occurred. Handle it accordingly. | |
74 | const errorMessage = err.error.detail || err.error.title | |
42b40636 | 75 | logger.error('An error occurred:', errorMessage) |
eaa52952 C |
76 | |
77 | return errorMessage | |
a51bad1a C |
78 | } |
79 | ||
eaa52952 C |
80 | if (typeof err.error === 'string') { |
81 | return err.error | |
82 | } | |
83 | ||
84 | if (err.status !== undefined) { | |
85 | const errorMessage = this.buildServerErrorMessage(err) | |
42b40636 | 86 | logger.error(`Backend returned code ${err.status}, errorMessage is: ${errorMessage}`) |
eaa52952 C |
87 | |
88 | return errorMessage | |
89 | } | |
90 | ||
42b40636 | 91 | logger.error(err) |
eaa52952 C |
92 | return err |
93 | } | |
94 | ||
95 | private buildServerErrorMessage (err: any) { | |
96 | // A server-side error occurred. | |
97 | if (err.error?.errors) { | |
98 | const errors = err.error.errors | |
99 | ||
100 | return Object.keys(errors) | |
101 | .map(key => errors[key].msg) | |
102 | .join('. ') | |
103 | } | |
104 | ||
105 | if (err.error?.error) { | |
106 | return err.error.error | |
107 | } | |
108 | ||
109 | if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) { | |
110 | return $localize`Media is too large for the server. Please contact you administrator if you want to increase the limit size.` | |
111 | } | |
112 | ||
113 | if (err.status === HttpStatusCode.TOO_MANY_REQUESTS_429) { | |
114 | const secondsLeft = err.headers.get('retry-after') | |
115 | ||
116 | if (secondsLeft) { | |
117 | const minutesLeft = Math.floor(parseInt(secondsLeft, 10) / 60) | |
118 | return $localize`Too many attempts, please try again after ${minutesLeft} minutes.` | |
119 | } | |
120 | ||
121 | return $localize`Too many attempts, please try again later.` | |
122 | } | |
123 | ||
124 | if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) { | |
125 | return $localize`Server error. Please retry later.` | |
126 | } | |
127 | ||
128 | return $localize`Unknown server error` | |
a51bad1a | 129 | } |
de59c48f | 130 | } |