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