Skip to content

Commit

Permalink
[pinpoint-apm#12021] Replace ErrorAttributes with ProblemDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Feb 20, 2025
1 parent a2e7b81 commit 6bef764
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
SpringDataWebAutoConfiguration.class,
RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class,
RedisReactiveAutoConfiguration.class
RedisReactiveAutoConfiguration.class,
})
@Import({
PinpointWebModule.class,
Expand Down
5 changes: 4 additions & 1 deletion web-starter/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ spring:
default-view-inclusion: true
profiles:
active: release, metric
mvc:
problemdetails:
enabled: true

server:
port: 8080
Expand All @@ -30,7 +33,7 @@ pinpoint:
enabled: true
otlpmetric:
enabled: true

pinpoint.web.websocket:
async-send-timeout:
max-session-idle-timeout: 10800000 # 3 hours
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2025 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.web;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author intr3p1d
*/
@ControllerAdvice
final class CustomExceptionHandler extends ResponseEntityExceptionHandler {

private final String hostname;

CustomExceptionHandler() {
this.hostname = getHostName();
}

static private String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "unknown";
}
}

@ExceptionHandler({Exception.class})
@Nullable
public ResponseEntity<ProblemDetail> handleGeneralException(Exception ex, WebRequest request) throws Exception {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
ProblemDetail problemDetail = ProblemDetail.forStatus(status.value());
problemDetail.setTitle("Internal Server Error");
problemDetail.setDetail(ex.getMessage());
addProperties(problemDetail, request);
addStackTraces(problemDetail, ex);

return new ResponseEntity<>(problemDetail, status);
}

@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex,
@Nullable Object body,
HttpHeaders headers,
HttpStatusCode statusCode,
WebRequest request
) {
if (body instanceof ProblemDetail problemDetail) {
addProperties(problemDetail, request);
addStackTraces(problemDetail, ex);
}
return super.handleExceptionInternal(ex, body, headers, statusCode, request);
}

public void addProperties(ProblemDetail problemDetail, WebRequest request) {
problemDetail.setProperty("hostname", hostname);
problemDetail.setProperty("method", request.getDescription(false));
problemDetail.setProperty("parameters", request.getParameterMap());
}

public void addStackTraces(ProblemDetail problemDetail, Exception ex) {
StringWriter stackTrace = new StringWriter();
ex.printStackTrace(new PrintWriter(stackTrace));
stackTrace.flush();
problemDetail.setProperty("trace", stackTrace.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
WebServiceConfig.class,
RealtimeConfig.class,
MainDataSourceConfiguration.class,
ProblemSpringWebConfig.class,

WebPinpointIdCacheConfiguration.class,
CacheConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
* @author intr3p1d
*/
@Configuration
public class ProblemSpringWebConfig {

@Bean
@Primary
public ResponseEntityExceptionHandler pinpointExceptionHandling() {
return new CustomExceptionHandler();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import java.net.URI;
import java.net.URISyntaxException;

@RestController
@RequestMapping(value={"/api-public/error"})
@Validated
//@RestController
//@RequestMapping(value={"/api-public/error"})
//@Validated
public class NonWhiteLabelErrorController extends AbstractErrorController {
private final ErrorProperties errorProperties;

Expand Down

This file was deleted.

This file was deleted.

5 changes: 4 additions & 1 deletion web/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ spring:
jackson:
mapper:
default-view-inclusion: true
mvc:
problemdetails:
enabled: true

server:
port: 8080
error:
path: /api/error
path: /api-public/error
include-exception: true
include-message: always
include-binding-errors: always
Expand Down

0 comments on commit 6bef764

Please sign in to comment.