-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInternalServerErrorDTO.java
More file actions
82 lines (72 loc) · 2.72 KB
/
InternalServerErrorDTO.java
File metadata and controls
82 lines (72 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.gam.api.common;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class InternalServerErrorDTO {
private String header = null;
private String httpMethod = null;
private String URL = null ;
private String message = null;
private String errorClass = null;
private String dateTime = "";
@Builder
private InternalServerErrorDTO(String header, String httpMethod, String URL, String message,
String errorClass, String dateTime) {
this.header = header;
this.httpMethod = httpMethod;
this.URL = URL;
this.message = message;
this.errorClass = errorClass;
this.dateTime = dateTime;
}
public static InternalServerErrorDTO of(String header, String httpMethod, String URL, String message,
String errorClass, LocalDateTime nowDateTime) {
if(Objects.isNull(nowDateTime)) {
nowDateTime = LocalDateTime.now();
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String now = nowDateTime.format(formatter);
return InternalServerErrorDTO.builder()
.header(header)
.httpMethod(httpMethod)
.URL(URL)
.message(message)
.errorClass(errorClass)
.dateTime(now)
.build();
}
public static String extractHeaders(HttpServletRequest request) {
StringBuilder headers = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headers.append(headerName).append(": ").append(headerValue);
}
return headers.toString();
}
@Override
public String toString() {
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
String jsonString = null;
try {
jsonString = writer.writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e); //todo Log
}
return jsonString;
}
}