Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
exception/src/main/java/hello/exception/api/ApiExceptionV3Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package hello.exception.api; | ||
|
||
import hello.exception.UserException; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
public class ApiExceptionV3Controller { | ||
|
||
@GetMapping("/api3/members/{id}") | ||
public MemberDto getMember(@PathVariable("id") String id) { | ||
|
||
if (id.equals("ex")) { | ||
throw new RuntimeException("잘못된 사용자"); | ||
} | ||
|
||
if (id.equals("bad")) { | ||
throw new IllegalArgumentException("잘못된 입력 값"); | ||
} | ||
|
||
if (id.equals("user-ex")) { | ||
throw new UserException("사용자 오류"); | ||
} | ||
|
||
return new MemberDto(id, "hello " + id); | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
static class MemberDto { | ||
private String memberId; | ||
private String name; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
exception/src/main/java/hello/exception/exhandler/advice/ExControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hello.exception.exhandler.advice; | ||
|
||
import hello.exception.UserException; | ||
import hello.exception.exhandler.ErrorResult; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice(basePackages = "hello.exception.api") | ||
public class ExControllerAdvice { | ||
|
||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ErrorResult illegalExHandle(IllegalArgumentException e) { | ||
log.error("[exceptionHandle] ex", e); | ||
return new ErrorResult("BAD", e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler | ||
public ResponseEntity<ErrorResult> userExHandle(UserException e) { | ||
log.error("[exceptionHandle] ex", e); | ||
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage()); | ||
return new ResponseEntity<>(errorResult, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler | ||
public ErrorResult exHandle(Exception e) { | ||
log.error("[exceptionHandle] ex", e); | ||
return new ErrorResult("EX", "내부 오류"); | ||
} | ||
} |