Skip to content

Commit

Permalink
add internal error exception
Browse files Browse the repository at this point in the history
  • Loading branch information
deyvisonborges committed Apr 25, 2024
1 parent 15129b4 commit 3b1591a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import com.eshop.backendpaymentapi.core.artifacts.payment.PaymentSearchQuery;
import com.eshop.backendpaymentapi.core.artifacts.payment.repository.PaymentRepositoryContract;
import com.eshop.backendpaymentapi.lib.Pagination;
import com.eshop.backendpaymentapi.lib.exception.InternalErrorException;
import com.eshop.backendpaymentapi.lib.exception.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
Expand All @@ -20,7 +19,6 @@

@Repository
public class PaymentJPARepository implements PaymentRepositoryContract {
private final Logger logger = LoggerFactory.getLogger(PaymentJPARepository.class);
private final PaymentJPARepositoryContract repositoryContract;

public PaymentJPARepository(PaymentJPARepositoryContract repositoryContract) {
Expand All @@ -33,8 +31,7 @@ public Payment save(Payment payment) {
final var jpaPayment = this.repositoryContract.save(PaymentJPAEntity.from(payment));
return PaymentJPAEntity.toAggregate(jpaPayment);
} catch (Exception e) {
this.logger.info("Repository -> Error while saving payment", e);
throw e;
throw new InternalErrorException(e.getMessage());
}
}

Expand All @@ -49,8 +46,7 @@ public void delete(String id) {
this.findById(id);
this.repositoryContract.deleteById(id);
} catch (Exception e) {
this.logger.info(MessageFormat.format("Error while delete Payment with id: {0}", id), e);
throw e;
throw new InternalErrorException(e.getMessage());
}
}

Expand All @@ -65,22 +61,27 @@ public Optional<Payment> findById(String id) {
);
return Optional.of(PaymentJPAEntity.toAggregate(paymentOpt));
} catch (Exception e) {
this.logger.info(MessageFormat.format("Error while fetching Payment with id: {0}", id), e);
throw e;
throw new InternalErrorException(e.getMessage());
}
}

@Override
public void saveAll(List<Payment> payments) {
final var entities = payments.stream()
try {
final var entities = payments.stream()
.map(PaymentJPAEntity::from)
.collect(Collectors.toList()); // Coletar as entidades mapeadas em uma lista
this.repositoryContract.saveAll(entities); // Salvar a lista de entidades
.collect(Collectors.toList());
this.repositoryContract.saveAll(entities);
} catch (Exception e) {
throw new InternalErrorException(e.getMessage());
}

}

@Override
public Pagination<Payment> findAll(final PaymentSearchQuery query) {
/*
try {
/*
* Pagination
* */
final var page = PageRequest.of(
Expand Down Expand Up @@ -109,10 +110,17 @@ public Pagination<Payment> findAll(final PaymentSearchQuery query) {
pageResult.getTotalElements(),
pageResult.map(PaymentJPAEntity::toAggregate).toList()
);
} catch (Exception e) {
throw new InternalErrorException(e.getMessage());
}
}

@Override
public List<Payment> findAll() {
return this.repositoryContract.findAll().stream().map(PaymentJPAEntity::toAggregate).toList();
try {
return this.repositoryContract.findAll().stream().map(PaymentJPAEntity::toAggregate).toList();
} catch (Exception e) {
throw new InternalErrorException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.eshop.backendpaymentapi.core.artifacts.payment.constant.PaymentMethod;
import com.eshop.backendpaymentapi.core.artifacts.payment.constant.PaymentStatus;


public record CreatePaymentCommand(
double value,
PaymentStatus status,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eshop.backendpaymentapi.lib.exception;

public class InternalErrorException extends NoStackTracingException{
public InternalErrorException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eshop.backendpaymentapi.lib.exception.handler;

import com.eshop.backendpaymentapi.lib.exception.BadRequestException;
import com.eshop.backendpaymentapi.lib.exception.InternalErrorException;
import com.eshop.backendpaymentapi.lib.exception.NotFoundException;
import com.eshop.backendpaymentapi.lib.exception.UnauthorizedException;
import com.eshop.backendpaymentapi.lib.exception.dto.ErrorResponseRecord;
Expand Down Expand Up @@ -45,4 +46,11 @@ public ResponseEntity<ErrorResponseRecord> nullPointerException(NullPointerExcep
new ErrorResponseRecord(n.getMessage(), HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST.value())
);
}

@ExceptionHandler(InternalErrorException.class)
public ResponseEntity<ErrorResponseRecord> internalError(InternalError n) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
new ErrorResponseRecord(n.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR.value())
);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ h2:
url: jdbc:h2:mem:payments_db;MODE=MYSQL;DATABASE_TO_LOWER=TRUE

spring:
jpa:
hibernate:
ddl-auto: create-drop
flyway:
enabled: true
url: ${h2.url}
Expand Down

0 comments on commit 3b1591a

Please sign in to comment.