Skip to content

Commit

Permalink
Hardening error handling. (#607)
Browse files Browse the repository at this point in the history
Co-authored-by: chris.ditcher <[email protected]>
  • Loading branch information
cditcher and chris.ditcher authored Jan 11, 2024
1 parent 29afaa3 commit 9de7a7e
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import ca.bc.gov.educ.api.gradstudent.service.GraduationStatusService;
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants;
import ca.bc.gov.educ.api.gradstudent.util.JsonUtil;
import ca.bc.gov.educ.api.gradstudent.util.LogHelper;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.nats.client.*;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -25,14 +26,14 @@ public class FetchGradStatusSubscriber implements MessageHandler {
private Dispatcher dispatcher;
private final GraduationStatusService graduationStatusService;

private final EducGradStudentApiConstants constants;
private static final String TOPIC = Topics.GRAD_STUDENT_API_FETCH_GRAD_STATUS_TOPIC.toString();

private static final Logger log = LoggerFactory.getLogger(FetchGradStatusSubscriber.class);

@Autowired
public FetchGradStatusSubscriber(final Connection natsConnection, GraduationStatusService graduationStatusService, EducGradStudentApiConstants constants) {
this.natsConnection = natsConnection;
this.graduationStatusService = graduationStatusService;
this.constants = constants;
}

@PostConstruct
Expand All @@ -44,7 +45,7 @@ public void subscribe() {
@Override
public void onMessage(Message message) {
val eventString = new String(message.getData());
LogHelper.logMessagingEventDetails(eventString, constants.isSplunkLogHelperEnabled());
log.debug(eventString);
String response;
try {
Event event = JsonUtil.getJsonObjectFromString(Event.class, eventString);
Expand All @@ -53,7 +54,9 @@ public void onMessage(Message message) {
response = getResponse(graduationStatus);
} catch (Exception e) {
response = getErrorResponse(e);
LogHelper.logMessagingEventDetails("NATS message exception at FetchGradStatusSubscriber: " + response + " when processing: " + eventString, constants.isSplunkLogHelperEnabled());
if(!(e instanceof EntityNotFoundException)){
log.error(String.format("NATS message exception at FetchGradStatusSubscriber: %s when processing: %s", e.getMessage(), eventString));
}
}
this.natsConnection.publish(message.getReplyTo(), response.getBytes());
}
Expand Down

0 comments on commit 9de7a7e

Please sign in to comment.