common-ui/src/lib/services/error-message.service.ts

23 lines
786 B
TypeScript

import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class ErrorMessageService {
constructor(private readonly _translateService: TranslateService) {
}
getMessage(error: HttpErrorResponse, defaultMessage?: string): string {
return defaultMessage
? (this._translateService.instant(defaultMessage, { error: this._parseErrorResponse(error) }) as string)
: this._parseErrorResponse(error);
}
private _parseErrorResponse(err: HttpErrorResponse): string {
return (err?.error?.message?.match('"message":"(.*?)\\"')?.[0] as string ) || (err?.error?.message as string);
}
}