Skip to content
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

Feat: setting error response dto #1

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/

### YML ###
application-*

### STS ###
.apt_generated
.classpath
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/co/orange/ddanzi/common/response/Error.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package co.orange.ddanzi.common.response;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum Error {
// Default
ERROR(HttpStatus.BAD_REQUEST, "Request processing failed"),

// 400 BAD REQUEST


// 401 UNAUTHORIZED


// 403 Forbidden


// 404 NOT FOUND


// 405 METHOD_NOT_ALLOWED


// 409 CONFLICT



// 500 INTERNAL SERVER ERROR
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error"),
;

private final HttpStatus httpStatus;
private final String message;

public int getHttpStatusCode() {
return httpStatus.value();
}
}
44 changes: 44 additions & 0 deletions src/main/java/co/orange/ddanzi/common/response/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package co.orange.ddanzi.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorResponse {

private int status;
private String message;
private LocalDateTime time;
private String path;
private String trace;
private List<VaildationError> vaildErrors;

@Data
@RequiredArgsConstructor
private static class VaildationError {
private final String field;
private final String message;
}

public void addVaildationError(String field, String message) {
if (Objects.isNull(vaildErrors)){
vaildErrors = new ArrayList<>();
}
vaildErrors.add(new VaildationError(field, message));
}

public ErrorResponse(int status, String message, String path, LocalDateTime time) {
this.status = status;
this.message = message;
this.path = path;
this.time = time;
}
}
27 changes: 27 additions & 0 deletions src/main/java/co/orange/ddanzi/common/response/Success.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package co.orange.ddanzi.common.response;


import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum Success {
// Default
SUCCESS(HttpStatus.OK, "Request successfully processed"),

// 200 OK SUCCESS

// 201 CREATED

;

private final HttpStatus httpStatus;
private final String message;

public int getHttpStatusCode() {
return httpStatus.value();
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

spring:
profiles:
active: local