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