API Error 응답 구조체 설계

에러 응답 처리에 대한 공통 Object 규격을 갖고있어야겠다 생각하여 만들어봄.
ApiErrorDetail은 빌더패턴을 이용하여 생성이 가능하도록 구성..

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class ApiError implements Serializable {
    private static final long serialVersionUID = 4863574457052508120L;

    private HttpStatus httpStatus;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    private LocalDateTime timestamp;
    private String message;
    private String returnUrl;
    private List<ApiErrorDetail> errors = new ArrayList<ApiErrorDetail>();

    public ApiError() {
        super();
    }

    public ApiError(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public ApiError(HttpStatus httpStatus, Throwable e) {
        this.httpStatus = httpStatus;
        this.message = "Unknown Exception";
        this.errors.add(new ApiErrorDetail.ApiErrorDetailBuilder().setExceptionName(e.getLocalizedMessage()).build());
    }

    public ApiError(HttpStatus httpStatus, List<ApiErrorDetail> errors) {
        this.httpStatus = httpStatus;
        this.errors = errors;
    }

    public ApiError(HttpStatus httpStatus, String message, Throwable e) {
        this.httpStatus = httpStatus;
        this.message = message;
        this.errors.add(new ApiErrorDetail.ApiErrorDetailBuilder().setExceptionName(e.getLocalizedMessage()).build());
    }

    public ApiError(HttpStatus httpStatus, String message, List<ApiErrorDetail> errors) {
        this.httpStatus = httpStatus;
        this.message = message;
        this.errors = errors;
    }

    public ApiError(HttpStatus httpStatus, String message, String returnUrl, Throwable e) {
        this.httpStatus = httpStatus;
        this.message = message;
        this.returnUrl = returnUrl;
        this.errors.add(new ApiErrorDetail.ApiErrorDetailBuilder().setExceptionName(e.getLocalizedMessage()).build());
    }

    public ApiError(HttpStatus httpStatus, String message, String returnUrl, List<ApiErrorDetail> errors) {
        this.httpStatus = httpStatus;
        this.message = message;
        this.returnUrl = returnUrl;
        this.errors = errors;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public List<ApiErrorDetail> getErrors() {
        return errors;
    }

    public void setErrors(List<ApiErrorDetail> errors) {
        this.errors = errors;
    }

    public void addErrors(ApiErrorDetail errors){
        this.errors.add(errors);
    }


}

 

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiErrorDetail implements Serializable {
    private static final long serialVersionUID = 6991430857880276989L;
    private String field;
    private String code;
    private String message;
    private String exceptionName;

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getExceptionName() {
        return exceptionName;
    }

    public void setExceptionName(String exceptionName) {
        this.exceptionName = exceptionName;
    }

    public ApiErrorDetail() {
    }

    public ApiErrorDetail(ApiErrorDetailBuilder builder) {
        this.field = builder.field;
        this.code = builder.code;
        this.message = builder.message;
        this.exceptionName = builder.exceptionName;
    }

    public static class ApiErrorDetailBuilder {
        private String field = "";
        private String code = "";
        private String message = "";
        private String exceptionName = "";

        public ApiErrorDetailBuilder() {
        }

        public ApiErrorDetailBuilder setField(String field) {
            this.field = field;
            return this;
        }

        public ApiErrorDetailBuilder setCode(String code) {
            this.code = code;
            return this;
        }

        public ApiErrorDetailBuilder setMessage(String message) {
            this.message = message;
            return this;
        }

        public ApiErrorDetailBuilder setExceptionName(String exceptionName) {
            this.exceptionName = exceptionName;
            return this;
        }

        public ApiErrorDetail build() {
            return new ApiErrorDetail(this);
        }

    }

    public static ApiErrorDetail.ApiErrorDetailBuilder Builder(){
        return new ApiErrorDetail.ApiErrorDetailBuilder();
    }
}

 

You may also like...

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.