galovics commented on code in PR #2718: URL: https://github.com/apache/fineract/pull/2718#discussion_r1015240931
########## fineract-provider/src/main/java/org/apache/fineract/commands/service/CommandSaver.java: ########## @@ -0,0 +1,75 @@ +/** + * 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.commands.service; + +import static org.apache.fineract.commands.domain.CommandProcessingResultType.ERROR; +import static org.apache.fineract.commands.domain.CommandProcessingResultType.UNDER_PROCESSING; + +import org.apache.fineract.commands.domain.CommandSource; +import org.apache.fineract.commands.domain.CommandSourceRepository; +import org.apache.fineract.commands.domain.CommandWrapper; +import org.apache.fineract.commands.exception.CommandNotFoundException; +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.useradministration.domain.AppUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * Two phase transactional command processing: save initial...work...finish/failed to handle idempotent requests. + */ +@Component +public class CommandSaver { + + @Autowired Review Comment: Please us constructor injection in combination with @RequiredArgsConstructor ########## fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/exceptionmapper/DuplicateCommandExceptionMapper.java: ########## @@ -0,0 +1,52 @@ +/** + * 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.commands.domain.CommandProcessingResultType.ERROR; +import static org.apache.fineract.commands.domain.CommandProcessingResultType.PROCESSED; +import static org.apache.fineract.commands.domain.CommandProcessingResultType.UNDER_PROCESSING; + +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.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.toString()); + if (PROCESSED.getValue() == exception.getProcessingResult() || ERROR.getValue() == exception.getProcessingResult()) { Review Comment: Nope. Rather create specific exceptions for specific use-cases. It's always fishy to use an exception and pass a parameter within that to decide later on some logic. ########## 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: I don't get why you need this class especially with mixing Spring's and JPA's lifecycle. Cant you just put a try catch around the command execution and unwrap the proper exception there? -- 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]
