SUPPORT-8725:add own exception

This commit is contained in:
adel.kalimullin 2024-12-11 13:25:34 +03:00
parent a9468f783f
commit 4ebbe22291
10 changed files with 104 additions and 22 deletions

View file

@ -11,6 +11,7 @@ import ru.micord.ervu.dto.SubpoenaResponseDto;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RestController;
import proto.ervu.rp.summons.SummonsResponseData;
import ru.micord.ervu.exception.ProtobufParsingException;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
import ru.micord.ervu.security.webbpm.jwt.util.SecurityUtil;
@ -54,7 +55,7 @@ public class ErvuDataController {
return converter.convert(responseData);
}
catch (InvalidProtocolBufferException e) {
throw new RuntimeException("Failed to parse data", e);
throw new ProtobufParsingException("Failed to parse data", e);
}
}
}

View file

@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import rtl.pgs.ervu.proto.ExtractRegistry;
import rtl.pgs.ervu.proto.ResponseData;
import ru.micord.ervu.dto.ExtractRequestDto;
import ru.micord.ervu.exception.ProtobufParsingException;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
import ru.micord.ervu.security.webbpm.jwt.util.SecurityUtil;
@ -60,7 +61,7 @@ public class ExtractController {
.body(resource);
}
catch (InvalidProtocolBufferException e) {
throw new RuntimeException("Failed to parse data", e);
throw new ProtobufParsingException("Failed to parse data", e);
}
}
}

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.exception;
/**
* @author Adel Kalimullin
*/
public class ProtobufParsingException extends RuntimeException{
public ProtobufParsingException(String message) {
super(message);
}
public ProtobufParsingException(String message, Throwable cause) {
super(message, cause);
}
public ProtobufParsingException(Throwable cause) {
super(cause);
}
}

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.kafka.exception;
/**
* @author Adel Kalimullin
*/
public class KafkaMessageException extends RuntimeException {
public KafkaMessageException(String message) {
super(message);
}
public KafkaMessageException(String message, Throwable cause) {
super(message, cause);
}
public KafkaMessageException(Throwable cause) {
super(cause);
}
}

View file

@ -7,6 +7,7 @@ import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.requestreply.ReplyingKafkaTemplate;
import org.springframework.kafka.requestreply.RequestReplyFuture;
import ru.micord.ervu.kafka.exception.KafkaMessageException;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
/**
@ -22,10 +23,10 @@ public abstract class BaseReplyingKafkaService<T, V> implements ReplyingKafkaSer
try {
return Optional.ofNullable(replyFuture.get())
.map(ConsumerRecord::value)
.orElseThrow(() -> new RuntimeException("Kafka return result is null"));
.orElseThrow(() -> new KafkaMessageException("Kafka return result is null"));
}
catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Failed to get kafka response", e);
throw new KafkaMessageException("Failed to get kafka response", e);
}
}
protected abstract ReplyingKafkaTemplate<String, T, V> getTemplate();

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.security.esia.exception;
/**
* @author Adel Kalimullin
*/
public class EsiaException extends RuntimeException{
public EsiaException(String message) {
super(message);
}
public EsiaException(String message, Throwable cause) {
super(message, cause);
}
public EsiaException(Throwable cause) {
super(cause);
}
}

View file

@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContext;
import ru.micord.ervu.security.esia.exception.EsiaException;
import ru.micord.ervu.kafka.model.Document;
import ru.micord.ervu.kafka.model.Person;
import ru.micord.ervu.kafka.model.Response;
@ -122,7 +123,7 @@ public class EsiaAuthService {
return makeRequest(url, params);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -142,7 +143,7 @@ public class EsiaAuthService {
return URLEncoder.encode(s, "UTF-8")
.replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -214,12 +215,12 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
}
if (tokenResponse.getError() != null) {
throw new RuntimeException(tokenResponse.getErrorDescription());
throw new EsiaException(tokenResponse.getErrorDescription());
}
String accessToken = tokenResponse.getAccessToken();
String verifyResult = verifyToken(accessToken);
if (verifyResult != null) {
throw new RuntimeException(verifyResult);
throw new EsiaException(verifyResult);
}
String refreshToken = tokenResponse.getRefreshToken();
EsiaAccessToken esiaAccessToken = personalDataService.readToken(accessToken);
@ -293,12 +294,12 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
String responseString = postResp.body();
EsiaTokenResponse tokenResponse = objectMapper.readValue(responseString, EsiaTokenResponse.class);
if (tokenResponse != null && tokenResponse.getError() != null) {
throw new RuntimeException(tokenResponse.getErrorDescription());
throw new EsiaException(tokenResponse.getErrorDescription());
}
String accessToken = tokenResponse.getAccessToken();
String verifyResult = verifyToken(accessToken);
if (verifyResult != null) {
throw new RuntimeException(verifyResult);
throw new EsiaException(verifyResult);
}
String newRefreshToken = tokenResponse.getRefreshToken();
EsiaAccessToken esiaAccessToken = personalDataService.readToken(accessToken);
@ -310,7 +311,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
createTokenAndAddCookie(response, esiaAccessToken.getSbjId(), ervuIdResponse.getErvuId(), expiresIn);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -336,13 +337,13 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
private void errorHandler(HttpResponse httpResponse) {
if (httpResponse.statusCode() != 200) {
throw new RuntimeException(httpResponse.statusCode() + " " + httpResponse.body());
throw new EsiaException(httpResponse.statusCode() + " " + httpResponse.body());
}
}
@ -361,7 +362,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
return makeRequest(url, params);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -375,7 +376,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
return objectMapper.readValue(kafkaResponse, Response.class);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -467,7 +468,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
return response;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
}

View file

@ -8,6 +8,7 @@ import java.time.Duration;
import java.util.Base64;
import com.fasterxml.jackson.databind.ObjectMapper;
import ru.micord.ervu.security.esia.exception.EsiaException;
import ru.micord.ervu.security.esia.config.EsiaConfig;
import ru.micord.ervu.security.esia.model.EsiaAccessToken;
import ru.micord.ervu.security.esia.model.EsiaHeader;
@ -41,7 +42,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return personModel;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -62,7 +63,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return objectMapper.readValue(getRespDoc.body(), PassportModel.class);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -82,7 +83,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return objectMapper.readValue(getResp.body(), PersonModel.class);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -101,7 +102,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return esiaAccessToken;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -120,7 +121,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return esiaHeader;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
}

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.security.exception;
/**
* @author Adel Kalimullin
*/
public class UnauthorizedException extends RuntimeException{
public UnauthorizedException(String message) {
super(message);
}
public UnauthorizedException(String message, Throwable cause) {
super(message, cause);
}
public UnauthorizedException(Throwable cause) {
super(cause);
}
}

View file

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import ru.micord.ervu.security.esia.token.TokensStore;
import ru.micord.ervu.security.exception.UnauthorizedException;
import ru.micord.ervu.security.webbpm.jwt.model.Token;
import ru.cg.webbpm.modules.resources.api.ResourceMetadataUtils;
@ -100,7 +101,7 @@ public class JwtTokenService {
return ids[0];
}
else {
throw new RuntimeException("Failed to get auth data. User unauthorized.");
throw new UnauthorizedException("Failed to get auth data. User unauthorized.");
}
}
}