-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: 잡포스트 리팩토링 #77
Changes from 4 commits
68bd611
6c5ad99
f4432f9
d08bb92
928b5f6
cd058f4
fc571f1
cab58a0
644dd5b
e653162
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.prgrms2.java.bitta.apply.controller.advice; | ||
|
||
import com.prgrms2.java.bitta.apply.exception.ApplyTaskException; | ||
import jakarta.validation.ConstraintViolationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Map; | ||
|
||
@RestControllerAdvice | ||
public class ApplyControllerAdvice { | ||
|
||
@ExceptionHandler(ApplyTaskException.class) | ||
public ResponseEntity<Map<String, String>> handleApplyTaskException(ApplyTaskException e) { | ||
return ResponseEntity.status(e.getCode()) | ||
.body(Map.of("error", e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<Map<String, String>> handleValidationException(MethodArgumentNotValidException e) { | ||
return ResponseEntity.status(e.getStatusCode()) | ||
.body(Map.of("error", e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
public ResponseEntity<Map<String, String>> handleConstraintViolationException(ConstraintViolationException e) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(Map.of("error", e.getMessage())); | ||
} | ||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jobPostId와 memberId의 |
YooJHyun marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 데이터 검증을 사용하는 어노테이션에는 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마찬가지로 MethodArgumentNotValidException, ConstraintViolationException은 GlobalControllerAdvice에서 처리하고 있으므로 제외해야 할 것 같습니다. 만약 여러 ControllerAdvice가 같은 Exception을 처리하게 될 경우, 잘 모르겠지만 Ambiguous 관련 오류가 발생할 것 같습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.prgrms2.java.bitta.jobpost.controller.advice; | ||
|
||
import com.prgrms2.java.bitta.jobpost.exception.JobPostTaskException; | ||
import jakarta.validation.ConstraintViolationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@RestControllerAdvice | ||
public class JobPostControllerAdvice { | ||
@ExceptionHandler(JobPostTaskException.class) | ||
public ResponseEntity<Map<String, String>> handleArgsException(JobPostTaskException e) { | ||
return ResponseEntity.status(e.getCode()) | ||
.body(Map.of("error", e.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<Map<String, String>> handleValidationException(MethodArgumentNotValidException e) { | ||
String errors = e.getBindingResult().getAllErrors().stream() | ||
.map(ObjectError::getDefaultMessage) | ||
.collect(Collectors.joining(", ")); | ||
|
||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.body(Map.of("error", errors)); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
public ResponseEntity<Map<String, String>> handleArgsException(ConstraintViolationException e) { | ||
return ResponseEntity.badRequest() | ||
.body(Map.of("error", e.getMessage())); | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. startDate와 endDate의 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MethodArgumentNotValidException, ConstraintViolationException은 GlobalControllerAdvice에서 처리하고 있으므로 제외해야 할 것 같습니다.