galovics commented on code in PR #2718:
URL: https://github.com/apache/fineract/pull/2718#discussion_r1018965771


##########
fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java:
##########
@@ -64,69 +66,58 @@ public class SynchronousCommandProcessingService implements 
CommandProcessingSer
     private final ApplicationContext applicationContext;
     private final ToApiJsonSerializer<Map<String, Object>> toApiJsonSerializer;
     private final ToApiJsonSerializer<CommandProcessingResult> 
toApiResultJsonSerializer;
-    private final CommandSourceRepository commandSourceRepository;
     private final ConfigurationDomainService configurationDomainService;
     private final CommandHandlerProvider commandHandlerProvider;
+    private final IdempotencyKeyResolver idempotencyKeyResolver;
     private final IdempotencyKeyGenerator idempotencyKeyGenerator;
-    private final FineractProperties fineractProperties;
+    private final CommandSourceService commandSourceService;
 
     @Override
-    @Transactional
     @Retry(name = "executeCommand", fallbackMethod = "fallbackExecuteCommand")
     public CommandProcessingResult executeCommand(final CommandWrapper 
wrapper, final JsonCommand command,
             final boolean isApprovedByChecker) {
 
         final boolean rollbackTransaction = 
configurationDomainService.isMakerCheckerEnabledForTask(wrapper.taskPermissionName());
+        String idempotencyKey = idempotencyKeyResolver.resolve(wrapper);
+        checkExistingCommand(wrapper, idempotencyKey);
 
-        final NewCommandSourceHandler handler = findCommandHandler(wrapper);
+        commandSourceService.saveInitial(wrapper, command, 
context.authenticatedUser(wrapper), idempotencyKey);
 
         final CommandProcessingResult result;
         try {
-            result = handler.processCommand(command);
+            result = findCommandHandler(wrapper).processCommand(command);
         } catch (Throwable t) {
-            publishHookErrorEvent(wrapper, command, t);
+            ErrorInfo ex;
+            if (t instanceof final RuntimeException e) {
+                ex = ErrorHandler.handler(e);
+            } else {
+                ex = new ErrorInfo(500, 9999, "{\"Exception\": " + 
t.toString() + "}");
+            }
+            publishHookEvent(wrapper.entityName(), wrapper.actionName(), 
command, ex);
+            commandSourceService.saveFailed(ex.getMessage(), 
commandSourceService.findCommandSource(wrapper, idempotencyKey));

Review Comment:
   So why do you need to call "findCommandSource" once more here? Why don't you 
reuse the result of the saveInitial call from above?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/jpa/JpaExceptionHandler.java:
##########
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.core.config.jpa;
+
+import static 
org.apache.fineract.commands.domain.CommandProcessingResultType.UNDER_PROCESSING;
+
+import java.sql.SQLIntegrityConstraintViolationException;
+import javax.servlet.http.HttpServletRequest;
+import 
org.apache.fineract.infrastructure.core.exception.DuplicateCommandException;
+import org.eclipse.persistence.exceptions.DatabaseException;
+import org.eclipse.persistence.exceptions.ExceptionHandler;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+@Component
+public class JpaExceptionHandler implements ExceptionHandler, 
ApplicationContextAware {

Review Comment:
   Can you pls share the other implementation where you tried to catch the 
exception? I'm having doubts to understand why this exception cannot be caught 
simply on a higher level and handled properly.
   
   On the other hand, if this class is really needed, it still shouldn't be a 
Bean as well as instantiated by EclipseLink. Mixing objects like that is a 
smell.
   If you need access to the AppContext from a static context, rather create a 
separate class that's ONLY a Spring Bean and provides access statically.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/exceptionmapper/DuplicateCommandExceptionMapper.java:
##########
@@ -0,0 +1,70 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.core.exceptionmapper;
+
+import static 
org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse.serverSideError;
+
+import com.google.gson.Gson;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
+import 
org.apache.fineract.infrastructure.core.exception.CommandFailedException;
+import 
org.apache.fineract.infrastructure.core.exception.CommandProcessedException;
+import 
org.apache.fineract.infrastructure.core.exception.CommandUnderProcessingException;
+import 
org.apache.fineract.infrastructure.core.exception.DuplicateCommandException;
+import org.springframework.stereotype.Component;
+
+@Provider
+@Component
+@Slf4j
+public class DuplicateCommandExceptionMapper implements 
ExceptionMapper<DuplicateCommandException> {
+
+    @Override
+    public Response toResponse(final DuplicateCommandException exception) {
+        log.debug("Duplicate request: {}", exception.getMessage());
+        if (exception instanceof CommandProcessedException) {

Review Comment:
   I'm sorry but I don't understand it. Why the if statements here?
   Why not just create separate exception mappers for each type of exception.



##########
fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java:
##########
@@ -64,69 +66,58 @@ public class SynchronousCommandProcessingService implements 
CommandProcessingSer
     private final ApplicationContext applicationContext;
     private final ToApiJsonSerializer<Map<String, Object>> toApiJsonSerializer;
     private final ToApiJsonSerializer<CommandProcessingResult> 
toApiResultJsonSerializer;
-    private final CommandSourceRepository commandSourceRepository;
     private final ConfigurationDomainService configurationDomainService;
     private final CommandHandlerProvider commandHandlerProvider;
+    private final IdempotencyKeyResolver idempotencyKeyResolver;
     private final IdempotencyKeyGenerator idempotencyKeyGenerator;
-    private final FineractProperties fineractProperties;
+    private final CommandSourceService commandSourceService;
 
     @Override
-    @Transactional
     @Retry(name = "executeCommand", fallbackMethod = "fallbackExecuteCommand")
     public CommandProcessingResult executeCommand(final CommandWrapper 
wrapper, final JsonCommand command,
             final boolean isApprovedByChecker) {
 
         final boolean rollbackTransaction = 
configurationDomainService.isMakerCheckerEnabledForTask(wrapper.taskPermissionName());
+        String idempotencyKey = idempotencyKeyResolver.resolve(wrapper);
+        checkExistingCommand(wrapper, idempotencyKey);
 
-        final NewCommandSourceHandler handler = findCommandHandler(wrapper);
+        commandSourceService.saveInitial(wrapper, command, 
context.authenticatedUser(wrapper), idempotencyKey);

Review Comment:
   Why don't you move the context.authenticatedUser logic into the saveInitial 
call? That' mean less parameters here. Same for the idempotency key.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to