]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Injectable } from '@angular/core' |
df98563e | 2 | import { Observable } from 'rxjs/Observable' |
d592e0a9 | 3 | import { HttpErrorResponse } from '@angular/common/http' |
de59c48f | 4 | |
d592e0a9 C |
5 | import { Utils } from '../utils' |
6 | import { ResultList } from '../../../../../shared' | |
de59c48f C |
7 | |
8 | @Injectable() | |
9 | export class RestExtractor { | |
10 | ||
d592e0a9 | 11 | extractDataBool () { |
df98563e | 12 | return true |
de59c48f C |
13 | } |
14 | ||
d592e0a9 C |
15 | applyToResultListData <T> (result: ResultList<T>, fun: Function, additionalArgs?: any[]): ResultList<T> { |
16 | const data: T[] = result.data | |
17 | const newData: T[] = [] | |
de59c48f | 18 | |
d592e0a9 | 19 | data.forEach(d => newData.push(fun.call(this, d, additionalArgs))) |
de59c48f | 20 | |
d592e0a9 C |
21 | return { |
22 | total: result.total, | |
23 | data: newData | |
24 | } | |
de59c48f C |
25 | } |
26 | ||
d592e0a9 C |
27 | convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> { |
28 | return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ]) | |
de59c48f C |
29 | } |
30 | ||
d592e0a9 C |
31 | convertDateToHuman (target: object, fieldsToConvert: string[]) { |
32 | const source = {} | |
33 | fieldsToConvert.forEach(field => { | |
34 | source[field] = Utils.dateToHuman(target[field]) | |
35 | }) | |
bf68dd75 | 36 | |
d592e0a9 C |
37 | return Object.assign(target, source) |
38 | } | |
de59c48f | 39 | |
d592e0a9 C |
40 | handleError (err: HttpErrorResponse) { |
41 | let errorMessage | |
42 | ||
43 | if (err.error instanceof Error) { | |
44 | // A client-side or network error occurred. Handle it accordingly. | |
45 | errorMessage = err.error.message | |
46 | console.error('An error occurred:', errorMessage) | |
47 | } else if (err.status !== undefined) { | |
8376734e R |
48 | // A server-side error occurred. |
49 | // TODO: remove when angular/angular#19477 (comment) is fixed | |
50 | let body = JSON.parse(err.error) | |
51 | if (body) { | |
52 | if (body.errors) { | |
53 | const errors = body.errors | |
54 | const error = errors[Object.keys(errors)[0]] | |
55 | errorMessage = error.msg // Take the message of the first error | |
56 | } else if (body.error) { | |
57 | errorMessage = body.error | |
58 | } | |
59 | } | |
60 | errorMessage = errorMessage ? errorMessage : 'Unknown error.' | |
d592e0a9 C |
61 | console.error(`Backend returned code ${err.status}, body was: ${errorMessage}`) |
62 | } else { | |
63 | errorMessage = err | |
df98563e | 64 | } |
de59c48f | 65 | |
bfb3a98f C |
66 | const errorObj = { |
67 | message: errorMessage, | |
c9d6d155 C |
68 | status: undefined, |
69 | body: undefined | |
bfb3a98f C |
70 | } |
71 | ||
72 | if (err.status) { | |
73 | errorObj.status = err.status | |
c9d6d155 | 74 | errorObj.body = err.error |
bfb3a98f C |
75 | } |
76 | ||
77 | return Observable.throw(errorObj) | |
de59c48f C |
78 | } |
79 | } |