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