Skip to content

Commit

Permalink
[LOG] 🔊 Exceptions are logged both side when using JMS store
Browse files Browse the repository at this point in the history
Signed-off-by: Cécile Chemin <[email protected]>
  • Loading branch information
CChemin committed Dec 11, 2023
1 parent 1b27382 commit 36b87c6
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- [BUG] :goal_net: Catch all LdapException when using mono connection
- [REL] :rocket: release version 2.1.2


# 2.1.2

- [BUG] :bug: Fix max time before a connection is dropped from ldap connection pool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ProviderResponse implements Serializable {
String exceptionType;
Object entity;

String hostname;

public ProviderResponse(
String entityId,
String requestId,
Expand All @@ -42,6 +44,10 @@ public ProviderResponse(

public ProviderResponse() {}

public ProviderResponse(String hostname) {
this.hostname = hostname;
}

public enum ProviderResponseStatus {
// Request is executed and result is effective at once
OK,
Expand Down Expand Up @@ -105,4 +111,8 @@ public String getExceptionType() {
public void setExceptionType(String exceptionType) {
this.exceptionType = exceptionType;
}

public String getHostname() {
return hostname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fr.insee.sugoi.springdoc.contact.email=tomcat2
fr.insee.sugoi.config.ldap.profils.url=localhost
fr.insee.sugoi.config.ldap.profils.port=10389
fr.insee.sugoi.config.ldap.profils.branche=cn=profil-contact-WebServicesLdap,ou=WebServicesLdap_Objets,ou=WebServicesLdap,ou=applications,o=insee,c=fr
fr.insee.sugoi.config.ldap.profils.pattern=cn=Profil_{realm}_WebServiceLdap


## LDAP PROPERTIES (use by default writer if set to LdapReaderStore or LdapWriterStore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import fr.insee.sugoi.model.Realm;
import fr.insee.sugoi.model.User;
import fr.insee.sugoi.model.UserStorage;
import fr.insee.sugoi.model.exceptions.JmsResponseException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -44,6 +45,8 @@ public class JmsWriterStore implements WriterStore {

private UserStorage userStorage;

private static final String hostName = System.getenv("HOSTNAME");

public JmsWriterStore(
JmsWriter jmsWriter,
String queueRequestName,
Expand Down Expand Up @@ -381,6 +384,7 @@ private ProviderResponse checkAndSend(

// Send Request
params.put(JmsAtttributes.PROVIDER_REQUEST, providerRequest);
params.put(JmsAtttributes.PROVIDER_HOSTNAME, hostName);
String correlationId =
providerRequest.isAsynchronousAllowed()
? jmsWriter.writeRequestInQueueAsynchronous(queueAsyncRequestName, method, params)
Expand All @@ -407,7 +411,7 @@ private ProviderResponse checkAndSend(
response = br.getProviderResponse();

if (response.getStatus() == ProviderResponseStatus.KO) {
throw createExceptionFromResponse(response);
throw createExceptionFromResponse(response, correlationId);
}
} catch (JmsException e) {
response.setStatus(ProviderResponseStatus.REQUESTED);
Expand All @@ -418,13 +422,17 @@ private ProviderResponse checkAndSend(
return response;
}

private RuntimeException createExceptionFromResponse(ProviderResponse providerResponse) {
private RuntimeException createExceptionFromResponse(
ProviderResponse providerResponse, String correlationID) {
try {
Class<?> c = Class.forName(providerResponse.getExceptionType());
return (RuntimeException)
c.getDeclaredConstructor(String.class, Throwable.class)
.newInstance(
providerResponse.getException().getMessage(), providerResponse.getException());
RuntimeException e =
(RuntimeException)
c.getDeclaredConstructor(String.class, Throwable.class)
.newInstance(
providerResponse.getException().getMessage(),
providerResponse.getException());
return new JmsResponseException(e, correlationID, providerResponse.getHostname());
} catch (Exception e) {
return new RuntimeException(
providerResponse.getException().getMessage(), providerResponse.getException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public class JmsRequestRouter {

@Autowired private OrganizationService orgService;

private static final String hostName = System.getenv("HOSTNAME");

public ProviderResponse exec(BrokerRequest request) throws Exception {
ProviderResponse response = new ProviderResponse();
ProviderResponse response = new ProviderResponse(hostName);
try {
String realm = (String) request.getmethodParams().get("realm");
String userStorage = (String) request.getmethodParams().get("userStorage");
Expand Down Expand Up @@ -256,6 +258,16 @@ public ProviderResponse exec(BrokerRequest request) throws Exception {
}
response.setStatus(ProviderResponseStatus.OK);
} catch (RuntimeException e) {
if (!e.getClass().getPackage().getName().startsWith("fr.insee.sugoi")
|| e.getClass().getSuperclass().getCanonicalName().equals("java.lang.RuntimeException")) {
logger.error(
"An error occurred while processing request "
+ request.getCorrelationId()
+ " for "
+ request.getmethodParams().get(JmsAtttributes.PROVIDER_HOSTNAME)
+ " : ",
e);
}
response.setStatus(ProviderResponseStatus.KO);
response.setException(e);
response.setExceptionType(e.getClass().getCanonicalName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public class JmsAtttributes {
public static final String WEBHOOK_TAG = "webhookTag";

public static final String SHOULD_RESET_PASSWORD = "should-reset-password";

public static final String PROVIDER_HOSTNAME = "hostname";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.insee.sugoi.model.exceptions;

public class JmsResponseException extends RuntimeException {

public JmsResponseException(Throwable e, String correlationID, String hostname) {
super(
"Error message from "
+ hostname
+ " when processing "
+ correlationID
+ " : "
+ e.getMessage(),
e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
package fr.insee.sugoi.commons.services.controller.technics;

import fr.insee.sugoi.commons.services.view.ErrorView;
import fr.insee.sugoi.model.exceptions.BadRequestException;
import fr.insee.sugoi.model.exceptions.ConflictException;
import fr.insee.sugoi.model.exceptions.ForbiddenException;
import fr.insee.sugoi.model.exceptions.NotFoundException;
import fr.insee.sugoi.model.exceptions.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -63,6 +60,8 @@ private HttpStatus computeStatusFromException(Exception e) {
return HttpStatus.NOT_IMPLEMENTED;
} else if (e instanceof HttpRequestMethodNotSupportedException) {
return HttpStatus.METHOD_NOT_ALLOWED;
} else if (e instanceof JmsResponseException) {
return computeStatusFromException((Exception) e.getCause());
} else {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
Expand Down

0 comments on commit 36b87c6

Please sign in to comment.