Skip to content

Commit

Permalink
Merge pull request #6 from SOPT-33-iOS-Team-1/feat/#5-create-common-o…
Browse files Browse the repository at this point in the history
…bjects

Feat/#5 create common objects
  • Loading branch information
yummygyudon authored Nov 25, 2023
2 parents 78eb29a + 95b6514 commit c53665f
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/sopt/sopkerton/common/exception/GlobalError.java
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;
}

}
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);
}
}
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;
}

}
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();
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.sopt.sopkerton.common.exception.base;

public interface RootEnum {
}
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 src/main/java/org/sopt/sopkerton/common/response/ApiResponse.java
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);
}
}
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)
);
}

}

0 comments on commit c53665f

Please sign in to comment.