Extend error with isUserError flag, to catch user and internal server exceptions

This commit is contained in:
Mikhail Trofimov 2025-06-17 16:57:56 +03:00
parent b9a98d9e0b
commit 16b3f085cb
3 changed files with 11 additions and 5 deletions

View file

@ -12,6 +12,10 @@ public class GlobalExceptionHandler {
@ExceptionHandler(value = SignalErrorException.class, produces = MediaType.APPLICATION_XML_VALUE) @ExceptionHandler(value = SignalErrorException.class, produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<CommonError> handleInvalidUser(SignalErrorException ex) { public ResponseEntity<CommonError> handleInvalidUser(SignalErrorException ex) {
return ResponseEntity.badRequest().body(ex.getError()); if (ex.isUserError()) {
return ResponseEntity.badRequest().body(ex.getError());
} else {
return ResponseEntity.internalServerError().body(ex.getError());
}
} }
} }

View file

@ -7,9 +7,11 @@ import ru.nbch.credit_tracker.enums.SignalError;
@Getter @Getter
public class SignalErrorException extends RuntimeException { public class SignalErrorException extends RuntimeException {
private final boolean isUserError;
private final CommonError error; private final CommonError error;
public SignalErrorException(SignalError signalError, String tag, CommonError error) { public SignalErrorException(SignalError signalError, String tag, CommonError error, boolean isUserError) {
this.isUserError = isUserError;
this.error = createErrorMessage(error, signalError, tag); this.error = createErrorMessage(error, signalError, tag);
} }

View file

@ -264,20 +264,20 @@ public class SignalFacade {
try { try {
AuthorizationService.AuthResult authResult = authorizationService.findAuthInfo(activePackagesRequest.getUserId(), activePackagesRequest.getPassword()); AuthorizationService.AuthResult authResult = authorizationService.findAuthInfo(activePackagesRequest.getUserId(), activePackagesRequest.getPassword());
if (!authResult.isSuccess()) { if (!authResult.isSuccess()) {
throw new SignalErrorException(authResult.getError(), null, new MonitoringActivePackagesError()); throw new SignalErrorException(authResult.getError(), null, new MonitoringActivePackagesError(), true);
} }
MonitoringActivePackagesResponse response = signalService.findActivePackages(activePackagesRequest.getUserId()); MonitoringActivePackagesResponse response = signalService.findActivePackages(activePackagesRequest.getUserId());
if (response != null) { if (response != null) {
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} else { } else {
throw new SignalErrorException(SignalError.ERROR_015, null, new MonitoringActivePackagesError()); throw new SignalErrorException(SignalError.ERROR_015, null, new MonitoringActivePackagesError(), true);
} }
} catch (Exception e) { } catch (Exception e) {
if (e instanceof SignalErrorException) { if (e instanceof SignalErrorException) {
throw (SignalErrorException) e; throw (SignalErrorException) e;
} }
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new SignalErrorException(SignalError.ERROR_013, null, new MonitoringActivePackagesError()); throw new SignalErrorException(SignalError.ERROR_013, null, new MonitoringActivePackagesError(), false);
} }
} }
} }