我已經使用 Spring Boot 創建了一個簡單的 GraphQL 端點,并且我正在使用 DefaultGraphQLErrorHandler() 來處理 GraphQL 錯誤。
但是,當我從我的應用程式中拋出一個自定義例外時,GraphQL 產生的錯誤回應包含例外堆疊跟蹤,這會泄露太多資訊。我想防止這種情況。
{
"data": {
"CommercialAsset": null
},
"errors": [
{
"message": "Exception while fetching data (/CommercialAsset) : Asset not fround in Data source",
"path": [
"CommercialAsset"
],
"exception": {
"cause": null,
"stackTrace": [
{
"classLoaderName": null,
"moduleName": null,
"moduleVersion": null,
"methodName": "getAssetById",
"fileName": "CommercialAssetDremioRepositoryImpl.java",
"lineNumber": 49,
"className": "com.dell.dremioclient.repository.impl.CommercialAssetDremioRepositoryImpl",
"nativeMethod": false
},
...
{
"classLoaderName": null,
"moduleName": "java.base",
"moduleVersion": "11.0.14",
"methodName": "run",
"fileName": "Thread.java",
"lineNumber": 834,
"className": "java.lang.Thread",
"nativeMethod": false
}
],
"message": "Asset not fround in Data source",
"locations": null,
"errorType": null,
"path": null,
"extensions": null,
"suppressed": [],
"localizedMessage": "Asset not fround in Data source"
},
"locations": [
{
"line": 2,
"column": 5,
"sourceName": null
}
],
"extensions": null,
"errorType": "DataFetchingException"
}
]
}
我正在使用的 GraphQL 依賴版本:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.4</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
有什么辦法可以使用自定義錯誤處理程式來做到這一點?像這樣的東西-
public class CustomGraphqlErrorHandler implements GraphQLErrorHandler {
@Override
public List<GraphQLError> processErrors(List<GraphQLError> errors) {
List<GraphQLError> errorList = new ArrayList<>();
errors.stream()
.forEach( e -> {
if(this.isServerError(e)) {
GraphqlDremioClientException gexp = new GraphqlDremioClientException(e.getMessage());
gexp.setStackTrace(null); /* This causes failure Bad POST request: parsing failed
java.lang.NullPointerException: null
at java.base/java.lang.Throwable.setStackTrace(Throwable.java:865) */
errorList.add(gexp);
} else {
errorList.add(e);
}
});
return errorList;
}
private boolean isServerError(GraphQLError error) {
return (error instanceof ExceptionWhileDataFetching || error instanceof Throwable);
}
@Override
public boolean errorsPresent(List<GraphQLError> errors) {
return !CollectionUtils.isEmpty(errors);
}
}
uj5u.com熱心網友回復:
我想阻止 GraphQL 在錯誤回應中顯示堆疊跟蹤。一個簡單的解決方案是添加一個自定義 GraphQL 錯誤處理程式來處理我的服務拋出的例外。然后,我創建了一個自定義的例外類,它可以在構造程序中啟用或禁用堆疊跟蹤。
自定義例外類:
public class GraphqlDremioClientException extends RuntimeException implements GraphQLError {
private static final long serialVersionUID = 1L;
private final String message;
private boolean writeStacktrace = false;
@Override
public String getMessage() {
return message;
}
/* other constructors */
public GraphqlDremioClientException(String message, boolean writeStacktrace) {
super(message, null, false, writeStacktrace);
this.writeStacktrace = writeStacktrace;
this.message = message;
}
public GraphqlDremioClientException(String message, Exception ex) {
super();
this.message = message;
}
@Override
public List<SourceLocation> getLocations() {
return null;
}
@Override
public ErrorType getErrorType() {
return null;
}
}
自定義 GraphQL 錯誤處理程式:
@Slf4j
@Component
public class CustomGraphqlErrorHandler implements GraphQLErrorHandler {
@Override
public List<GraphQLError> processErrors(List<GraphQLError> list) {
return list.stream().map(this::getNested).collect(Collectors.toList());
}
private GraphQLError getNested(GraphQLError error) {
log.error(error.getMessage(), error);
if (error instanceof ExceptionWhileDataFetching) {
ExceptionWhileDataFetching exceptionError = (ExceptionWhileDataFetching) error;
if (exceptionError.getException() instanceof GraphQLError) {
return new GraphqlDremioClientException(exceptionError.getMessage(), false);
}
}
return error;
}
}
現在的錯誤回應:
{
"data": {
"CommercialAsset": null
},
"errors": [
{
"cause": null,
"stackTrace": [],
"message": "Exception while fetching data (/CommercialAsset) : Asset not fround in Data source",
"writeStacktrace": false,
"locations": null,
"errorType": null,
"path": null,
"extensions": null,
"suppressed": [],
"localizedMessage": "Exception while fetching data (/CommercialAsset) : Asset not fround in Data source"
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/449988.html
