Skip to content

Commit

Permalink
fix: add consulting type error handler for conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Apr 23, 2024
1 parent f300a3c commit 1744f20
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.caritas.cob.consultingtypeservice.api;

import de.caritas.cob.consultingtypeservice.api.exception.httpresponses.ConflictException;
import de.caritas.cob.consultingtypeservice.api.service.LogService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -31,6 +32,13 @@ public ResponseEntity<Object> handleInternal(
return new ResponseEntity<>(EMPTY_HEADERS, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler({ConflictException.class})
public ResponseEntity<Object> handleInternal(
final ConflictException ex, final WebRequest request) {
LogService.logInternalServerError(ex);
return new ResponseEntity<>(EMPTY_HEADERS, HttpStatus.CONFLICT);
}

@ExceptionHandler({AccessDeniedException.class})
public ResponseEntity<Object> handleInternal(
final AccessDeniedException ex, final WebRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public Optional<ConsultingTypeEntity> addConsultingType(final ConsultingType con
|| isConsultingTypeWithGivenSlugPresent(consultingType)) {
LogService.logWarning(
String.format(
"Could not add consulting type. id %s or slug %s is not unique",
consultingType.getId(), consultingType.getSlug()));
"Could not add consulting type. id %s or slug %s for tenant %s is not unique",
consultingType.getId(), consultingType.getSlug(), consultingType.getTenantId()));
return Optional.empty();
} else {
final ConsultingTypeEntity consultingTypeEntity = new ConsultingTypeEntity();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.caritas.cob.consultingtypeservice.api.exception.httpresponses;

import de.caritas.cob.consultingtypeservice.api.service.LogService;

/** Representation of a 409 - CONFLICT. */
public class ConflictException extends CustomHttpStatusException {

public ConflictException(String message) {
super(message, LogService::logWarning);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.caritas.cob.consultingtypeservice.api.auth.Authority.AuthorityValue;
import de.caritas.cob.consultingtypeservice.api.consultingtypes.ConsultingTypeConverter;
import de.caritas.cob.consultingtypeservice.api.consultingtypes.ConsultingTypeRepositoryService;
import de.caritas.cob.consultingtypeservice.api.exception.httpresponses.InternalServerErrorException;
import de.caritas.cob.consultingtypeservice.api.exception.httpresponses.ConflictException;
import de.caritas.cob.consultingtypeservice.api.mapper.BasicConsultingTypeMapper;
import de.caritas.cob.consultingtypeservice.api.mapper.ConsultingTypeMapper;
import de.caritas.cob.consultingtypeservice.api.mapper.ExtendedConsultingTypeMapper;
Expand Down Expand Up @@ -127,9 +127,10 @@ public FullConsultingTypeResponseDTO createConsultingType(
return ConsultingTypeMapper.mapConsultingType(
createdConsultingType.get(), FullConsultingTypeMapper::mapConsultingType);
} else {
throw new InternalServerErrorException(
throw new ConflictException(
String.format(
"Could not create a new consulting type with slug %s", consultingTypeDTO.getSlug()));
"Could not create a new consulting type with slug %s for tenant id %s",
consultingTypeDTO.getSlug(), consultingType.getTenantId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void doFilterInternal(
filterChain.doFilter(request, response);
TenantContext.clear();
} else {
log.info(
log.debug(
"Skipping tenant filter for request: {} as it belongs to a tenancy whitelist.",
request.getRequestURI());
filterChain.doFilter(request, response);
Expand Down

0 comments on commit 1744f20

Please sign in to comment.