]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/rest/rest-extractor.service.ts
redirect to login on 401, display error variants in 404 component
[github/Chocobozzz/PeerTube.git] / client / src / app / core / rest / rest-extractor.service.ts
CommitLineData
a51bad1a 1import { throwError as observableThrowError } from 'rxjs'
df98563e 2import { Injectable } from '@angular/core'
a51bad1a 3import { Router } from '@angular/router'
67ed6552 4import { dateToHuman } from '@app/helpers'
67ed6552 5import { ResultList } from '@shared/models'
f2eb23cd 6import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
de59c48f
C
7
8@Injectable()
9export class RestExtractor {
10
66357162 11 constructor (private router: Router) { }
a51bad1a 12
d592e0a9 13 extractDataBool () {
df98563e 14 return true
de59c48f
C
15 }
16
d592e0a9
C
17 applyToResultListData <T> (result: ResultList<T>, fun: Function, additionalArgs?: any[]): ResultList<T> {
18 const data: T[] = result.data
19 const newData: T[] = []
de59c48f 20
61bbc727 21 data.forEach(d => newData.push(fun.apply(this, [ d ].concat(additionalArgs))))
de59c48f 22
d592e0a9
C
23 return {
24 total: result.total,
25 data: newData
26 }
de59c48f
C
27 }
28
d592e0a9
C
29 convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> {
30 return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ])
de59c48f
C
31 }
32
c199c427 33 convertDateToHuman (target: { [ id: string ]: string }, fieldsToConvert: string[]) {
61bbc727 34 fieldsToConvert.forEach(field => target[field] = dateToHuman(target[field]))
bf68dd75 35
61bbc727 36 return target
d592e0a9 37 }
de59c48f 38
47f8de28 39 handleError (err: any) {
d592e0a9
C
40 let errorMessage
41
42 if (err.error instanceof Error) {
43 // A client-side or network error occurred. Handle it accordingly.
44 errorMessage = err.error.message
45 console.error('An error occurred:', errorMessage)
47f8de28
C
46 } else if (typeof err.error === 'string') {
47 errorMessage = err.error
d592e0a9 48 } else if (err.status !== undefined) {
8376734e 49 // A server-side error occurred.
490b595a
C
50 if (err.error && err.error.errors) {
51 const errors = err.error.errors
52 const errorsArray: string[] = []
53
54 Object.keys(errors).forEach(key => {
55 errorsArray.push(errors[key].msg)
56 })
57
58 errorMessage = errorsArray.join('. ')
59 } else if (err.error && err.error.error) {
60 errorMessage = err.error.error
f2eb23cd 61 } else if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) {
dc5bb5ce 62 errorMessage = $localize`Media is too large for the server. Please contact you administrator if you want to increase the limit size.`
f2eb23cd 63 } else if (err.status === HttpStatusCode.TOO_MANY_REQUESTS_429) {
490b595a
C
64 const secondsLeft = err.headers.get('retry-after')
65 if (secondsLeft) {
66 const minutesLeft = Math.floor(parseInt(secondsLeft, 10) / 60)
66357162 67 errorMessage = $localize`Too many attempts, please try again after ${minutesLeft} minutes.`
490b595a 68 } else {
66357162 69 errorMessage = $localize`Too many attempts, please try again later.`
490b595a 70 }
f2eb23cd 71 } else if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) {
66357162 72 errorMessage = $localize`Server error. Please retry later.`
8376734e 73 }
13fb4de9 74
8376734e 75 errorMessage = errorMessage ? errorMessage : 'Unknown error.'
490b595a 76 console.error(`Backend returned code ${err.status}, errorMessage is: ${errorMessage}`)
d592e0a9 77 } else {
2f1548fd 78 console.error(err)
d592e0a9 79 errorMessage = err
df98563e 80 }
de59c48f 81
c199c427 82 const errorObj: { message: string, status: string, body: string } = {
bfb3a98f 83 message: errorMessage,
c9d6d155
C
84 status: undefined,
85 body: undefined
bfb3a98f
C
86 }
87
88 if (err.status) {
89 errorObj.status = err.status
c9d6d155 90 errorObj.body = err.error
bfb3a98f
C
91 }
92
13359203 93 return observableThrowError(errorObj)
de59c48f 94 }
a51bad1a 95
ab398a05 96 redirectTo404IfNotFound (obj: { status: number }, type: 'video' | 'other', status = [ HttpStatusCode.NOT_FOUND_404 ]) {
a51bad1a
C
97 if (obj && obj.status && status.indexOf(obj.status) !== -1) {
98 // Do not use redirectService to avoid circular dependencies
ab398a05 99 this.router.navigate([ '/404' ], { state: { type, obj }, skipLocationChange: true })
a51bad1a
C
100 }
101
102 return observableThrowError(obj)
103 }
de59c48f 104}