-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…bjects Feat/#5 create common objects
- Loading branch information
Showing
9 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/org/sopt/sopkerton/common/exception/GlobalError.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,32 @@ | ||
package org.sopt.sopkerton.common.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.sopt.sopkerton.common.exception.base.ErrorBase; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
public enum GlobalError implements ErrorBase { | ||
|
||
; | ||
|
||
private final HttpStatus status; | ||
private final String errorMessage; | ||
|
||
|
||
@Override | ||
public int getHttpStatusCode() { | ||
return status.value(); | ||
} | ||
|
||
@Override | ||
public HttpStatus getHttpStatus() { | ||
return this.status; | ||
} | ||
|
||
|
||
@Override | ||
public String getErrorMessage() { | ||
return this.errorMessage; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/sopt/sopkerton/common/exception/GlobalException.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,13 @@ | ||
package org.sopt.sopkerton.common.exception; | ||
|
||
import org.sopt.sopkerton.common.exception.base.ExceptionBase; | ||
|
||
/** | ||
* 전역적 혹은 시스템 전체 범위에서 발생하는 예외 | ||
*/ | ||
public class GlobalException extends ExceptionBase { | ||
|
||
public GlobalException(GlobalError error) { | ||
super(error); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/sopt/sopkerton/common/exception/GlobalSuccess.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,30 @@ | ||
package org.sopt.sopkerton.common.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.sopt.sopkerton.common.exception.base.SuccessBase; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
public enum GlobalSuccess implements SuccessBase { | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String successMessage; | ||
|
||
|
||
@Override | ||
public int getHttpStatusCode() { | ||
return this.status.value(); | ||
} | ||
|
||
@Override | ||
public HttpStatus getHttpStatus() { | ||
return this.status; | ||
} | ||
|
||
@Override | ||
public String getSuccessMessage() { | ||
return this.successMessage; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/sopt/sopkerton/common/exception/base/ErrorBase.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,10 @@ | ||
package org.sopt.sopkerton.common.exception.base; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface ErrorBase extends RootEnum{ | ||
int getHttpStatusCode(); | ||
|
||
HttpStatus getHttpStatus(); | ||
String getErrorMessage(); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.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,20 @@ | ||
package org.sopt.sopkerton.common.exception.base; | ||
|
||
|
||
/** | ||
* 하위 도메인 예외는 본 Exception 클래스를 상속 받습니다. | ||
* - 객체 생 | ||
*/ | ||
public abstract class ExceptionBase extends RuntimeException{ | ||
private static final String ERROR_MESSAGE_HEADER = "ERROR : "; | ||
|
||
private final ErrorBase errorBase; | ||
protected ExceptionBase(ErrorBase errorBase) { | ||
super(ERROR_MESSAGE_HEADER + errorBase.getErrorMessage()); | ||
this.errorBase = errorBase; | ||
} | ||
|
||
public ErrorBase getError() { | ||
return errorBase; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/org/sopt/sopkerton/common/exception/base/RootEnum.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,4 @@ | ||
package org.sopt.sopkerton.common.exception.base; | ||
|
||
public interface RootEnum { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/sopt/sopkerton/common/exception/base/SuccessBase.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,11 @@ | ||
package org.sopt.sopkerton.common.exception.base; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface SuccessBase extends RootEnum{ | ||
int getHttpStatusCode(); | ||
|
||
HttpStatus getHttpStatus(); | ||
String getSuccessMessage(); | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/org/sopt/sopkerton/common/response/ApiResponse.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,38 @@ | ||
package org.sopt.sopkerton.common.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.sopkerton.common.exception.base.ErrorBase; | ||
import org.sopt.sopkerton.common.exception.base.SuccessBase; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ApiResponse<T> { | ||
private final int code; | ||
private final String message; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
private T data; | ||
|
||
public static ApiResponse<?> success(SuccessBase success) { | ||
return new ApiResponse<>(success.getHttpStatusCode(), success.getSuccessMessage()); | ||
} | ||
|
||
public static <T> ApiResponse<T> success(SuccessBase success, T data) { | ||
return new ApiResponse<T>(success.getHttpStatusCode(), success.getSuccessMessage(), data); | ||
} | ||
|
||
public static <T> ApiResponse <T> error(ErrorBase error) { | ||
return new ApiResponse<>(error.getHttpStatusCode(), error.getErrorMessage()); | ||
} | ||
|
||
public static <T> ApiResponse<T> error(ErrorBase error, T data) { | ||
return new ApiResponse<>(error.getHttpStatusCode(), error.getErrorMessage(), data); | ||
} | ||
|
||
public static ApiResponse <Exception> error(ErrorBase error, Exception exception) { | ||
return new ApiResponse<>(error.getHttpStatusCode(), error.getErrorMessage(), exception); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.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,26 @@ | ||
package org.sopt.sopkerton.common.response; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.sopt.sopkerton.common.exception.base.ErrorBase; | ||
import org.sopt.sopkerton.common.exception.base.ExceptionBase; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
@RequiredArgsConstructor | ||
public class CommonControllerAdvice { | ||
|
||
@ExceptionHandler(value = ExceptionBase.class) | ||
public ResponseEntity<?> sopkathonExceptionHandler(ExceptionBase exception) { | ||
ErrorBase error = exception.getError(); | ||
return ResponseEntity | ||
.status(error.getHttpStatus()) | ||
.body( | ||
ApiResponse.error(error) | ||
); | ||
} | ||
|
||
} |