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