Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 0acae1e

Browse files
committed
Develop exception collection and lookup
1 parent 1beb590 commit 0acae1e

File tree

29 files changed

+978
-47
lines changed

29 files changed

+978
-47
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.easypeelsecurity.springdog.agent;
18+
19+
import java.time.LocalDateTime;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.ResponseEntity;
23+
24+
import com.fasterxml.jackson.annotation.JsonFormat;
25+
26+
/**
27+
* Common response.
28+
*
29+
* @param <T> The type of the response detail
30+
*/
31+
public class CommonResponse<T> {
32+
33+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
34+
private final LocalDateTime timestamp = LocalDateTime.now();
35+
private final T detail;
36+
private final String message;
37+
private final ResponseStatus result;
38+
39+
/**
40+
* Constructor.
41+
*/
42+
public CommonResponse(T detail, String message, ResponseStatus result) {
43+
this.detail = detail;
44+
this.message = message;
45+
this.result = result;
46+
}
47+
48+
/**
49+
* Constructor for success response.
50+
*
51+
* @param detail The response detail
52+
*/
53+
public CommonResponse(T detail) {
54+
this(detail, "The request was successfully processed.", ResponseStatus.SUCCESS);
55+
}
56+
57+
/**
58+
* Create a error response.
59+
*
60+
* @param message The error message
61+
* @return The error response
62+
*/
63+
public static <T> ResponseEntity<CommonResponse<T>> responseError(String message) {
64+
return ResponseEntity
65+
.status(HttpStatus.BAD_REQUEST)
66+
.body(new CommonResponse<>(
67+
null,
68+
message,
69+
ResponseStatus.FAILURE));
70+
}
71+
72+
/**
73+
* Create a error response with exception's message.
74+
*
75+
* @param message The error message
76+
* @param e The exception to contain the message
77+
* @return The error response
78+
*/
79+
public static <T> ResponseEntity<CommonResponse<T>> responseError(String message, Exception e) {
80+
return ResponseEntity
81+
.status(HttpStatus.BAD_REQUEST)
82+
.body(new CommonResponse<>(
83+
null,
84+
message + " " + e.getLocalizedMessage(),
85+
ResponseStatus.FAILURE));
86+
}
87+
88+
/**
89+
* Get the timestamp.
90+
*/
91+
public LocalDateTime getTimestamp() {
92+
return timestamp;
93+
}
94+
95+
/**
96+
* Get the response detail.
97+
*/
98+
public T getDetail() {
99+
return detail;
100+
}
101+
102+
/**
103+
* Get the response message.
104+
*
105+
* @return The message
106+
*/
107+
public String getMessage() {
108+
return message;
109+
}
110+
111+
/**
112+
* Get the response status.
113+
*
114+
* @return The status
115+
*/
116+
public String getResult() {
117+
return result.name();
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.easypeelsecurity.springdog.agent;
18+
19+
/**
20+
* Response status.
21+
*/
22+
enum ResponseStatus {
23+
SUCCESS,
24+
FAILURE
25+
}

springdog-project/springdog-agent/src/main/java/org/easypeelsecurity/springdog/agent/SpringdogAPI.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,41 @@
1616

1717
package org.easypeelsecurity.springdog.agent;
1818

19+
import static org.springframework.http.HttpStatus.NO_CONTENT;
20+
1921
import org.springframework.web.bind.annotation.GetMapping;
2022
import org.springframework.web.bind.annotation.PathVariable;
2123
import org.springframework.web.bind.annotation.RequestParam;
2224
import org.springframework.web.bind.annotation.RestController;
2325

2426
import org.easypeelsecurity.springdog.domain.errortracing.model.ExceptionListingService;
27+
import org.easypeelsecurity.springdog.shared.dto.ErrorTracingDto;
2528

2629
/**
2730
* RestController for springdog.
2831
*/
2932
@RestController
3033
@SpringdogAgentController
3134
@SuppressWarnings("checkstyle:MissingJavadocMethod")
32-
public class SpringdogAPI {
35+
public class SpringdogAPI extends SpringdogAPIExceptionHandler {
3336

3437
private final ExceptionListingService exceptionListingService;
3538

3639
public SpringdogAPI(ExceptionListingService exceptionListingService) {
3740
this.exceptionListingService = exceptionListingService;
3841
}
3942

43+
@org.springframework.web.bind.annotation.ResponseStatus(NO_CONTENT)
4044
@GetMapping("/error-tracing/configuration/{exceptionClassId}")
4145
public void errorExceptionMonitoringStatus(
4246
@PathVariable(name = "exceptionClassId") long exceptionClassId,
4347
@RequestParam("enabled") boolean enabled) {
4448
exceptionListingService.changeMonitoringStatus(exceptionClassId, enabled);
4549
}
50+
51+
@GetMapping("/error-tracing/{errorTracingId}")
52+
public CommonResponse<ErrorTracingDto> getErrorTracing(
53+
@PathVariable(name = "errorTracingId") long errorTracingId) {
54+
return new CommonResponse<>(exceptionListingService.getErrorTrace(errorTracingId));
55+
}
4656
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.easypeelsecurity.springdog.agent;
18+
19+
import static org.easypeelsecurity.springdog.agent.CommonResponse.responseError;
20+
import static org.springframework.http.HttpStatus.BAD_REQUEST;
21+
22+
import org.springframework.http.ResponseEntity;
23+
import org.springframework.http.converter.HttpMessageNotReadableException;
24+
import org.springframework.web.bind.MethodArgumentNotValidException;
25+
import org.springframework.web.bind.annotation.ExceptionHandler;
26+
27+
/**
28+
* Springdog API Exception Handler.
29+
*/
30+
abstract class SpringdogAPIExceptionHandler {
31+
/**
32+
* If an Exception related invalid input is thrown, return an error message conforming to the Response
33+
* specification.
34+
*/
35+
@org.springframework.web.bind.annotation.ResponseStatus(BAD_REQUEST)
36+
@ExceptionHandler({
37+
MethodArgumentNotValidException.class,
38+
IllegalArgumentException.class,
39+
HttpMessageNotReadableException.class})
40+
protected ResponseEntity<CommonResponse<Void>> handleInvalidInput(Exception e) {
41+
return responseError("Invalid input value.", e);
42+
}
43+
44+
/**
45+
* If an IllegalStateException is thrown, return an error message conforming to the Response specification.
46+
*/
47+
@org.springframework.web.bind.annotation.ResponseStatus(BAD_REQUEST)
48+
@ExceptionHandler(value = {IllegalStateException.class})
49+
protected ResponseEntity<CommonResponse<Void>> handleIllegalStateException(IllegalStateException e) {
50+
return responseError("This is an inappropriate call.", e);
51+
}
52+
}

springdog-project/springdog-agent/src/main/java/org/easypeelsecurity/springdog/agent/SpringdogAgentView.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.easypeelsecurity.springdog.domain.statistics.StatisticsService;
3939
import org.easypeelsecurity.springdog.shared.configuration.SpringdogProperties;
4040
import org.easypeelsecurity.springdog.shared.dto.EndpointDto;
41+
import org.easypeelsecurity.springdog.shared.dto.ErrorTracingDto;
4142
import org.easypeelsecurity.springdog.shared.util.Assert;
4243

4344
/**
@@ -134,6 +135,13 @@ public String modifyRateLimit(@PathVariable(name = "endpointId") long endpointId
134135
return viewRateLimitSpecific(endpointId, model);
135136
}
136137

138+
@GetMapping("/error-tracing")
139+
public String errorTracingHome(Model model) {
140+
List<ErrorTracingDto> causes = exceptionListingService.getAllParentCauses();
141+
model.addAttribute("causes", causes);
142+
return "/templates/content/error-tracing/list.html";
143+
}
144+
137145
@GetMapping("/error-tracing/configuration")
138146
public String errorTracingConfiguration(Model model) {
139147
model.addAttribute("exceptionClasses", exceptionListingService.getExceptionListing());

springdog-project/springdog-agent/src/main/resources/static/css/bootstrap.min.css

Lines changed: 0 additions & 7 deletions
This file was deleted.

springdog-project/springdog-agent/src/main/resources/static/css/styles.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ tbody {
5454
--bs-gray-700: #495057;
5555
--bs-gray-800: #343a40;
5656
--bs-gray-900: #212529;
57-
--bs-primary: #0d6efd;
57+
--bs-primary: #239560;
5858
--bs-secondary: #6c757d;
5959
--bs-success: #198754;
60-
--bs-info: #0dcaf0;
60+
--bs-info: #529978;
6161
--bs-warning: #ffc107;
6262
--bs-danger: #dc3545;
6363
--bs-light: #f8f9fa;
6464
--bs-dark: #212529;
65-
--bs-primary-rgb: 13, 110, 253;
66-
--bs-secondary-rgb: 108, 117, 125;
65+
--bs-primary-rgb: 35, 149, 96;
66+
--bs-secondary-rgb: 25, 135, 84;
6767
--bs-success-rgb: 25, 135, 84;
68-
--bs-info-rgb: 13, 202, 240;
68+
--bs-info-rgb: 82, 153, 120;
6969
--bs-warning-rgb: 255, 193, 7;
7070
--bs-danger-rgb: 220, 53, 69;
7171
--bs-light-rgb: 248, 249, 250;

springdog-project/springdog-agent/src/main/resources/static/js/bootstrap.min.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springdog-project/springdog-agent/src/main/resources/static/js/chart.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springdog-project/springdog-agent/src/main/resources/static/js/popper.min.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springdog-project/springdog-agent/src/main/resources/static/js/springdog-api.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
window.springdogFetch = function (url, options, data, successCallback,
18-
errorCallback) {
17+
window.springdogFetch = function (url, options, data) {
1918
if (springdogBasePath === undefined) {
2019
console.error(
2120
`springdogBasePath is not defined. Please define it in your HTML file.
@@ -45,12 +44,40 @@ window.springdogFetch = function (url, options, data, successCallback,
4544
}
4645

4746
return fetch(springdogBasePath + url, fetchOptions)
48-
.then(response => {
49-
if (!response.ok) {
50-
return response.json().then(errorData => {
51-
throw new Error(errorData.message || 'Network response was not ok');
52-
});
47+
.then(async response => {
48+
if (response.status === 204) {
49+
return;
5350
}
54-
return response.json().catch(() => ({}));
51+
52+
let json;
53+
try {
54+
json = await response.json();
55+
} catch (error) {
56+
throw new Error('Invalid JSON response');
57+
}
58+
59+
if (response.status === 201) {
60+
return json.message;
61+
} else if (response.status === 200) {
62+
if (json.result === undefined) {
63+
throw new Error('Unexpected response');
64+
}
65+
66+
if (json.result === 'SUCCESS') {
67+
return json.detail;
68+
} else {
69+
throw new Error(json.message);
70+
}
71+
} else {
72+
if (json.message === undefined) {
73+
throw new Error('Unexpected response');
74+
} else {
75+
throw new Error(json.message);
76+
}
77+
}
78+
})
79+
.catch(error => {
80+
console.error('Error fetching data:', error);
81+
throw error;
5582
});
5683
}

springdog-project/springdog-agent/src/main/resources/templates/content/error-tracing/configuration.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@
7777
springdogFetch(`/error-tracing/configuration/${exceptionId}?enabled=${isChecked}`, {
7878
method: 'GET'
7979
})
80-
.then(data => {
81-
console.log(`Exception ${exceptionId} monitoring status changed to ${isChecked}`);
82-
// 성공 시 추가 작업이 필요하다면 여기에 작성
83-
})
8480
.catch(error => {
8581
console.error('There was a problem with the fetch operation:', error);
8682
// Revert the checkbox state if the API call fails

0 commit comments

Comments
 (0)