SUPPORT-8897: Fix

This commit is contained in:
Eduard Tihomirov 2025-02-06 09:42:15 +03:00
parent 7b180ad6b6
commit ad9c1ef68f
5 changed files with 103 additions and 0 deletions

View file

@ -0,0 +1,61 @@
package ru.micord.ervu.error_handling;
import java.util.Arrays;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import ru.cg.webbpm.modules.core.app_info.api.mode.AppMode;
import ru.cg.webbpm.modules.core.error_handling.api.ProcessedWebException;
import ru.cg.webbpm.modules.core.error_handling.api.impl.UnknownExceptionHandler;
import ru.cg.webbpm.modules.core.runtime.api.MessageBundleUtils;
@Component
@Primary
public class ErvuUnknownExceptionHandler extends UnknownExceptionHandler {
protected static final MessageSourceAccessor ERVU_MESSAGE_SOURCE = MessageBundleUtils.createAccessor("exception_handler_messages");
@Override
public ProcessedWebException process(Throwable throwable) {
if (AppMode.mode() == AppMode.Mode.PROD) {
return new ProcessedWebException()
.addMessage(unknownErrorMessage());
}
else {
Throwable lastThrowable = findLastCause(throwable);
String lastThrowableStackTrace = Arrays.toString(lastThrowable.getStackTrace());
String limitLastThrowableStackTrace = lastThrowableStackTrace.length() > MAX_STACK_TRACE_LENGTH
? lastThrowableStackTrace.substring(0, MAX_STACK_TRACE_LENGTH) + " ..."
: lastThrowableStackTrace;
return new ProcessedWebException()
.addMessage(findLastNotEmptyMessage(throwable))
.setCauseStackTrace(limitLastThrowableStackTrace);
}
}
private String unknownErrorMessage() {
return ERVU_MESSAGE_SOURCE.getMessage("ervu.error.unknown", new Object[] {});
}
private Throwable findLastCause(Throwable throwable) {
if (throwable.getCause() != null) {
return findLastCause(throwable.getCause());
}
return throwable;
}
private String findLastNotEmptyMessage(Throwable throwable) {
String message = throwable.getMessage();
if(throwable.getCause() != null) {
String nextMessage = findLastNotEmptyMessage(throwable.getCause());
if(!StringUtils.isEmpty(nextMessage)) {
message = nextMessage;
}
}
return message;
}
}

View file

@ -0,0 +1 @@
ervu.error.unknown=??????? ???????? ??????????. ??????????, ????????? ??????? ?????.

View file

@ -0,0 +1 @@
ervu.error.unknown=The system is temporarily unavailable. Please try again later.

View file

@ -1,10 +1,12 @@
import {
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest
} from "@angular/common/http";
import {
ErrorResponse,
HttpSecurityErrorInterceptor,
MessagesService,
UserService
@ -13,18 +15,21 @@ import {Injectable} from "@angular/core";
import {Router} from "@angular/router";
import {Observable, throwError} from "rxjs";
import {catchError} from "rxjs/operators";
import {ErvuHttpInterceptorUtils} from "../../../util/ErvuHttpInterceptorUtils";
@Injectable()
export class ErvuHttpSecurityErrorInterceptor extends HttpSecurityErrorInterceptor
implements HttpInterceptor {
private router: Router;
private _userService: UserService;
private _messagesService: MessagesService;
constructor(router: Router, messagesService: MessagesService, userService: UserService) {
super(router, messagesService, userService);
this.router = router;
this._userService = userService;
this._messagesService = messagesService;
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
@ -67,9 +72,17 @@ export class ErvuHttpSecurityErrorInterceptor extends HttpSecurityErrorIntercept
default: {
this._userService.hideProgressBar();
console.log("Error " + error.status + " server response received by HTTP Interceptor");
this.processErrorResponseDataErvu(error);
return throwError(error);
}
}
}));
}
private processErrorResponseDataErvu(error: HttpErrorResponse) {
let errorResponse: ErrorResponse = ErvuHttpInterceptorUtils.getErrorResponseFromResponse(error);
errorResponse.messages.forEach((errorMessage) => {
this._messagesService.error(errorMessage, errorResponse);
});
}
}

View file

@ -0,0 +1,27 @@
import {HttpErrorResponse} from "@angular/common/http";
import {ErrorResponse} from "@webbpm/base-package";
export class ErvuHttpInterceptorUtils {
public static fallbackErrorMessage: string = "Система временно недоступна. Пожалуйста, повторите попытку позже";
public static getErrorResponseFromResponse(response: HttpErrorResponse): ErrorResponse {
let responseJSONObject = null;
try {
responseJSONObject = response.error;
}
catch (e) {
//ignore json parse error
}
if (responseJSONObject && (responseJSONObject.key || responseJSONObject.messages)) {
return responseJSONObject;
}
let errorResponse: ErrorResponse = new ErrorResponse();
errorResponse.messages.push(ErvuHttpInterceptorUtils.fallbackErrorMessage);
return errorResponse;
}
}