aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/rest/rest-extractor.service.ts
blob: 6492aa66de554a6af5f621c5c520d27639664ba4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { throwError as observableThrowError } from 'rxjs'
import { Injectable } from '@angular/core'
import { dateToHuman } from '@app/shared/misc/utils'
import { ResultList } from '../../../../../shared'
import { Router } from '@angular/router'
import { I18n } from '@ngx-translate/i18n-polyfill'

@Injectable()
export class RestExtractor {

  constructor (
    private router: Router,
    private i18n: I18n
  ) { }

  extractDataBool () {
    return true
  }

  applyToResultListData <T> (result: ResultList<T>, fun: Function, additionalArgs?: any[]): ResultList<T> {
    const data: T[] = result.data
    const newData: T[] = []

    data.forEach(d => newData.push(fun.apply(this, [ d ].concat(additionalArgs))))

    return {
      total: result.total,
      data: newData
    }
  }

  convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> {
    return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ])
  }

  convertDateToHuman (target: object, fieldsToConvert: string[]) {
    fieldsToConvert.forEach(field => target[field] = dateToHuman(target[field]))

    return target
  }

  handleError (err: any) {
    let errorMessage

    if (err.error instanceof Error) {
      // A client-side or network error occurred. Handle it accordingly.
      errorMessage = err.error.message
      console.error('An error occurred:', errorMessage)
    } else if (typeof err.error === 'string') {
      errorMessage = err.error
    } else if (err.status !== undefined) {
      // A server-side error occurred.
      if (err.error && err.error.errors) {
        const errors = err.error.errors
        const errorsArray: string[] = []

        Object.keys(errors).forEach(key => {
          errorsArray.push(errors[key].msg)
        })

        errorMessage = errorsArray.join('. ')
      } else if (err.error && err.error.error) {
        errorMessage = err.error.error
      } else if (err.status === 413) {
        errorMessage = this.i18n(
          'Request is too large for the server. Please contact you administrator if you want to increase the limit size.'
        )
      } else if (err.status === 429) {
        const secondsLeft = err.headers.get('retry-after')
        if (secondsLeft) {
          const minutesLeft = Math.floor(parseInt(secondsLeft, 10) / 60)
          errorMessage = this.i18n('Too many attempts, please try again after {{minutesLeft}} minutes.', { minutesLeft })
        } else {
          errorMessage = this.i18n('Too many attempts, please try again later.')
        }
      } else if (err.status === 500) {
        errorMessage = this.i18n('Server error. Please retry later.')
      }

      errorMessage = errorMessage ? errorMessage : 'Unknown error.'
      console.error(`Backend returned code ${err.status}, errorMessage is: ${errorMessage}`)
    } else {
      errorMessage = err
    }

    const errorObj = {
      message: errorMessage,
      status: undefined,
      body: undefined
    }

    if (err.status) {
      errorObj.status = err.status
      errorObj.body = err.error
    }

    return observableThrowError(errorObj)
  }

  redirectTo404IfNotFound (obj: { status: number }, status = [ 404 ]) {
    if (obj && obj.status && status.indexOf(obj.status) !== -1) {
      // Do not use redirectService to avoid circular dependencies
      this.router.navigate([ '/404' ], { skipLocationChange: true })
    }

    return observableThrowError(obj)
  }
}