From eaa529528cafcfb291009f9f99d296c81e792899 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 May 2022 16:29:01 +0200 Subject: Support ICU in TS components --- client/src/app/core/rest/rest-extractor.service.ts | 111 ++++++++++++--------- 1 file changed, 65 insertions(+), 46 deletions(-) (limited to 'client/src/app/core') diff --git a/client/src/app/core/rest/rest-extractor.service.ts b/client/src/app/core/rest/rest-extractor.service.ts index 17053811c..86c7484a5 100644 --- a/client/src/app/core/rest/rest-extractor.service.ts +++ b/client/src/app/core/rest/rest-extractor.service.ts @@ -34,49 +34,17 @@ export class RestExtractor { return target } - handleError (err: any) { - let errorMessage + redirectTo404IfNotFound (obj: { status: number }, type: 'video' | 'other', status = [ HttpStatusCode.NOT_FOUND_404 ]) { + if (obj?.status && status.includes(obj.status)) { + // Do not use redirectService to avoid circular dependencies + this.router.navigate([ '/404' ], { state: { type, obj }, skipLocationChange: true }) + } - if (err.error instanceof Error) { - // A client-side or network error occurred. Handle it accordingly. - errorMessage = err.error.detail || err.error.title - 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?.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 - } else if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) { - // eslint-disable-next-line max-len - errorMessage = $localize`Media is too large for the server. Please contact you administrator if you want to increase the limit size.` - } else if (err.status === HttpStatusCode.TOO_MANY_REQUESTS_429) { - const secondsLeft = err.headers.get('retry-after') - if (secondsLeft) { - const minutesLeft = Math.floor(parseInt(secondsLeft, 10) / 60) - errorMessage = $localize`Too many attempts, please try again after ${minutesLeft} minutes.` - } else { - errorMessage = $localize`Too many attempts, please try again later.` - } - } else if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) { - errorMessage = $localize`Server error. Please retry later.` - } + return observableThrowError(() => obj) + } - errorMessage = errorMessage || 'Unknown error.' - console.error(`Backend returned code ${err.status}, errorMessage is: ${errorMessage}`) - } else { - console.error(err) - errorMessage = err - } + handleError (err: any) { + const errorMessage = this.buildErrorMessage(err) const errorObj: { message: string, status: string, body: string } = { message: errorMessage, @@ -92,12 +60,63 @@ export class RestExtractor { return observableThrowError(() => errorObj) } - redirectTo404IfNotFound (obj: { status: number }, type: 'video' | 'other', status = [ HttpStatusCode.NOT_FOUND_404 ]) { - if (obj?.status && status.includes(obj.status)) { - // Do not use redirectService to avoid circular dependencies - this.router.navigate([ '/404' ], { state: { type, obj }, skipLocationChange: true }) + private buildErrorMessage (err: any) { + if (err.error instanceof Error) { + // A client-side or network error occurred. Handle it accordingly. + const errorMessage = err.error.detail || err.error.title + console.error('An error occurred:', errorMessage) + + return errorMessage } - return observableThrowError(() => obj) + if (typeof err.error === 'string') { + return err.error + } + + if (err.status !== undefined) { + const errorMessage = this.buildServerErrorMessage(err) + console.error(`Backend returned code ${err.status}, errorMessage is: ${errorMessage}`) + + return errorMessage + } + + console.error(err) + return err + } + + private buildServerErrorMessage (err: any) { + // A server-side error occurred. + if (err.error?.errors) { + const errors = err.error.errors + + return Object.keys(errors) + .map(key => errors[key].msg) + .join('. ') + } + + if (err.error?.error) { + return err.error.error + } + + if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) { + return $localize`Media is too large for the server. Please contact you administrator if you want to increase the limit size.` + } + + if (err.status === HttpStatusCode.TOO_MANY_REQUESTS_429) { + const secondsLeft = err.headers.get('retry-after') + + if (secondsLeft) { + const minutesLeft = Math.floor(parseInt(secondsLeft, 10) / 60) + return $localize`Too many attempts, please try again after ${minutesLeft} minutes.` + } + + return $localize`Too many attempts, please try again later.` + } + + if (err.status === HttpStatusCode.INTERNAL_SERVER_ERROR_500) { + return $localize`Server error. Please retry later.` + } + + return $localize`Unknown server error` } } -- cgit v1.2.3