Skip to content

Commit

Permalink
Merge pull request #76 from woowacourse-teams/feature/#71
Browse files Browse the repository at this point in the history
커스텀 예외 처리
  • Loading branch information
ay-eonii authored Jul 20, 2024
2 parents 794490f + 39674b5 commit 7942162
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mouda.backend.exception;

public record ErrorResponse(
String message
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mouda.backend.exception;

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
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(MoudaException.class)
public ResponseEntity<ErrorResponse> handleMoudaException(MoudaException exception) {
return ResponseEntity.status(exception.getHttpStatus()).body(new ErrorResponse(exception.getMessage()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException() {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ErrorResponse("서버 오류가 발생했습니다."));
}
}
14 changes: 14 additions & 0 deletions backend/src/main/java/mouda/backend/exception/MoudaException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mouda.backend.exception;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class MoudaException extends RuntimeException {

private HttpStatus httpStatus;
private String message;
}
6 changes: 5 additions & 1 deletion backend/src/main/java/mouda/backend/moim/domain/Moim.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.time.LocalDate;
import java.time.LocalTime;

import org.springframework.http.HttpStatus;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -11,6 +13,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mouda.backend.moim.exception.MoimErrorMessage;
import mouda.backend.moim.exception.MoimException;

@Entity
@Getter
Expand Down Expand Up @@ -40,7 +44,7 @@ public class Moim {

public void join() {
if (currentPeople + 1 > maxPeople) {
throw new IllegalArgumentException();
throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MAX_PEOPLE);
}
currentPeople++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mouda.backend.moim.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum MoimErrorMessage {

NOT_FOUND("모임이 존재하지 않습니다."),
MAX_PEOPLE("모임 최대 인원 수를 초과합니다."),
;

private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mouda.backend.moim.exception;

import org.springframework.http.HttpStatus;

import mouda.backend.exception.MoudaException;

public class MoimException extends MoudaException {

public MoimException(HttpStatus httpStatus, MoimErrorMessage moimErrorMessage) {
super(httpStatus, moimErrorMessage.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -12,6 +13,8 @@
import mouda.backend.moim.dto.response.MoimDetailsFindResponse;
import mouda.backend.moim.dto.response.MoimFindAllResponse;
import mouda.backend.moim.dto.response.MoimFindAllResponses;
import mouda.backend.moim.exception.MoimErrorMessage;
import mouda.backend.moim.exception.MoimException;
import mouda.backend.moim.repository.MoimRepository;

@Transactional
Expand All @@ -38,20 +41,20 @@ public MoimFindAllResponses findAllMoim() {
@Transactional(readOnly = true)
public MoimDetailsFindResponse findMoimDetails(long id) {
Moim moim = moimRepository.findById(id)
.orElseThrow(IllegalArgumentException::new);
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND));

return MoimDetailsFindResponse.toResponse(moim);
}

public void joinMoim(MoimJoinRequest moimJoinRequest) {
Moim moim = moimRepository.findById(moimJoinRequest.moimId())
.orElseThrow(() -> new IllegalArgumentException("모임이 존재하지 않습니다."));
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND));
moim.join();
}

public void deleteMoim(long id) {
Moim moim = moimRepository.findById(id)
.orElseThrow(IllegalArgumentException::new);
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND));

moimRepository.delete(moim);
}
Expand Down

0 comments on commit 7942162

Please sign in to comment.