X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fshared%2Frest%2Frest-extractor.service.ts;h=f149569ef5028b2a5c27f6e92dc0bf432db957ba;hb=f7cc67b455a12ccae9b0ea16876d166720364357;hp=59dea788016d5046c30efd3e472797c68058b887;hpb=10db166bbe510af27cb20cc0f028537af0df9c41;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts index 59dea7880..f149569ef 100644 --- a/client/src/app/shared/rest/rest-extractor.service.ts +++ b/client/src/app/shared/rest/rest-extractor.service.ts @@ -1,13 +1,18 @@ +import { throwError as observableThrowError } from 'rxjs' import { Injectable } from '@angular/core' -import { Observable } from 'rxjs/Observable' -import { HttpErrorResponse } from '@angular/common/http' - -import { Utils } from '../utils' +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 } @@ -16,7 +21,7 @@ export class RestExtractor { const data: T[] = result.data const newData: T[] = [] - data.forEach(d => newData.push(fun.call(this, d, additionalArgs))) + data.forEach(d => newData.push(fun.apply(this, [ d ].concat(additionalArgs)))) return { total: result.total, @@ -28,45 +33,57 @@ export class RestExtractor { return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ]) } - convertDateToHuman (target: object, fieldsToConvert: string[]) { - const source = {} - fieldsToConvert.forEach(field => { - source[field] = Utils.dateToHuman(target[field]) - }) + convertDateToHuman (target: { [ id: string ]: string }, fieldsToConvert: string[]) { + fieldsToConvert.forEach(field => target[field] = dateToHuman(target[field])) - return Object.assign(target, source) + return target } - handleError (err: HttpErrorResponse) { + 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) { - if (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.error) { - errorMessage = err.error.error + 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}, body was: ${errorMessage}`) + console.error(`Backend returned code ${err.status}, errorMessage is: ${errorMessage}`) } else { errorMessage = err } - const errorObj = { + const errorObj: { message: string, status: string, body: string } = { message: errorMessage, status: undefined, body: undefined @@ -77,6 +94,15 @@ export class RestExtractor { errorObj.body = err.error } - return Observable.throw(errorObj) + 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) } }