Skip to content

Commit

Permalink
Added GlobalExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 6f7ad71 commit 9935e86
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.zenjob.challenge.exceptionhandler;

import lombok.*;

@Getter
@Setter
@RequiredArgsConstructor
public class ErrorResponseDto {
private final int statusCode;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.zenjob.challenge.exceptionhandler;

import com.zenjob.challenge.customexception.InvalidActionException;
import com.zenjob.challenge.customexception.InvalidEndDateException;
import com.zenjob.challenge.customexception.InvalidStartDateException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@RestControllerAdvice
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(InvalidStartDateException.class)
ResponseEntity<Object> handleCustomException(InvalidStartDateException ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(InvalidEndDateException.class)
ResponseEntity<Object> handleCustomException(InvalidEndDateException ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(InvalidActionException.class)
ResponseEntity<Object> handleCustomException(InvalidActionException ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.METHOD_NOT_ALLOWED.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.METHOD_NOT_ALLOWED);
}

@ExceptionHandler(Exception.class)
ResponseEntity<Object> handleAllExceptions(Exception ex) {
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

11 changes: 4 additions & 7 deletions src/main/java/com/zenjob/challenge/service/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class JobService implements IJobService {

public Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate) {
if (startDate.isBefore(LocalDate.now()))
throw new InvalidStartDateException();
throw new InvalidStartDateException("Start date cannot be before now");

if (endDate.isBefore(startDate))
throw new InvalidEndDateException();
throw new InvalidEndDateException("End date cannot be before start date");

Job job = Job.builder()
.id(UUID.randomUUID())
Expand All @@ -57,9 +57,6 @@ public List<Shift> getShifts(UUID id) {
}

public void bookTalent(UUID talentId, UUID shiftId) {
// Optional<Shift> shiftById = shiftRepository.findById(shiftId);
// shiftById.map(shift -> shiftRepository.save(shift.setTalentId(talentId)));

Optional<Shift> shiftById = shiftRepository.findById(shiftId);
shiftById.get().setTalentId(talentId);
shiftRepository.save(shiftById.get());
Expand All @@ -69,7 +66,7 @@ public void bookTalent(UUID talentId, UUID shiftId) {
public void cancelJob(UUID companyId, UUID jobId) {
Optional<Job> job = getJob(jobId);
if (!job.get().getCompanyId().equals(companyId))
throw new InvalidActionException();
throw new InvalidActionException("You cannot cancel job of other companies");

jobRepository.deleteById(jobId);
}
Expand All @@ -83,7 +80,7 @@ public Optional<Job> getJob(UUID id) {
public void cancelShift(UUID companyId, UUID shiftId) {
Optional<Shift> shift = getShift(shiftId);
if (!shift.get().getJob().getCompanyId().equals(companyId))
throw new InvalidActionException();
throw new InvalidActionException("You cannot cancel shift of other companies");

shiftRepository.deleteById(shiftId);
}
Expand Down

0 comments on commit 9935e86

Please sign in to comment.