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