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