-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[AI-8th] Add RFC 7807 ProblemDetail support for SOFA exceptions #1421
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
Open
pmupkin
wants to merge
3
commits into
sofastack:master
Choose a base branch
from
pmupkin:support-problemdetail
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
.../main/java/com/alipay/sofa/boot/autoconfigure/web/SofaProblemDetailAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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.alipay.sofa.boot.autoconfigure.web; | ||
|
|
||
| import com.alipay.sofa.boot.autoconfigure.rpc.SofaRpcAutoConfiguration; | ||
| import com.alipay.sofa.boot.autoconfigure.runtime.SofaRuntimeAutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.http.ProblemDetail; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| /** | ||
| * Auto-configuration for SOFA ProblemDetail exception handling. | ||
| */ | ||
| @AutoConfiguration(after = { SofaRuntimeAutoConfiguration.class, SofaRpcAutoConfiguration.class }) | ||
| @ConditionalOnClass({ ProblemDetail.class, ResponseEntityExceptionHandler.class }) | ||
| @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) | ||
| @ConditionalOnProperty(prefix = SofaProblemDetailProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true) | ||
| @EnableConfigurationProperties(SofaProblemDetailProperties.class) | ||
| public class SofaProblemDetailAutoConfiguration { | ||
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean(value = ResponseEntityExceptionHandler.class, ignored = { | ||
| SofaRuntimeProblemDetailExceptionHandler.class, | ||
| SofaRpcProblemDetailExceptionHandler.class }) | ||
| public SofaProblemDetailExceptionHandler sofaProblemDetailExceptionHandler(SofaProblemDetailProperties properties, | ||
| Environment environment) { | ||
| return new SofaProblemDetailExceptionHandler(properties, environment); | ||
| } | ||
|
|
||
| @Bean | ||
| @ConditionalOnClass(name = "com.alipay.sofa.runtime.api.ServiceRuntimeException") | ||
| @ConditionalOnMissingBean(SofaRuntimeProblemDetailExceptionHandler.class) | ||
| public SofaRuntimeProblemDetailExceptionHandler sofaRuntimeProblemDetailExceptionHandler(SofaProblemDetailProperties properties, | ||
| Environment environment) { | ||
| return new SofaRuntimeProblemDetailExceptionHandler(properties, environment); | ||
| } | ||
|
|
||
| @Bean | ||
| @ConditionalOnClass(name = { "com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException", | ||
| "com.alipay.sofa.rpc.core.exception.SofaRpcException", | ||
| "com.alipay.sofa.rpc.core.exception.RpcErrorType" }) | ||
| @ConditionalOnMissingBean(SofaRpcProblemDetailExceptionHandler.class) | ||
| public SofaRpcProblemDetailExceptionHandler sofaRpcProblemDetailExceptionHandler(SofaProblemDetailProperties properties, | ||
| Environment environment) { | ||
| return new SofaRpcProblemDetailExceptionHandler(properties, environment); | ||
| } | ||
| } |
162 changes: 162 additions & 0 deletions
162
...c/main/java/com/alipay/sofa/boot/autoconfigure/web/SofaProblemDetailExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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.alipay.sofa.boot.autoconfigure.web; | ||
|
|
||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.HttpStatusCode; | ||
| import org.springframework.http.ProblemDetail; | ||
| import org.springframework.lang.Nullable; | ||
| import org.springframework.util.StringUtils; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.context.request.ServletWebRequest; | ||
| 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.URI; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Base MVC exception handler that customizes framework-generated ProblemDetail | ||
| * responses with SOFA-specific metadata. | ||
| */ | ||
| @RestControllerAdvice | ||
| @Order(Ordered.LOWEST_PRECEDENCE) | ||
| public class SofaProblemDetailExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| static final URI ABOUT_BLANK = URI.create("about:blank"); | ||
| static final URI RUNTIME_EXCEPTION_TYPE = URI | ||
| .create("https://sofastack.io/errors/runtime-exception"); | ||
| static final URI RPC_EXCEPTION_TYPE = URI | ||
| .create("https://sofastack.io/errors/rpc-exception"); | ||
| static final URI RPC_CONFIGURATION_TYPE = URI | ||
| .create("https://sofastack.io/errors/rpc-configuration-exception"); | ||
|
|
||
| private static final Pattern ERROR_CODE_PATTERN = Pattern | ||
| .compile("(SOFA-BOOT-\\d{2}-\\d{5})"); | ||
|
|
||
| private final SofaProblemDetailProperties properties; | ||
| private final String applicationName; | ||
|
|
||
| public SofaProblemDetailExceptionHandler(SofaProblemDetailProperties properties, | ||
| Environment environment) { | ||
| this.properties = properties; | ||
| this.applicationName = environment.getProperty("spring.application.name"); | ||
| } | ||
|
|
||
| @Override | ||
| protected org.springframework.http.ResponseEntity<Object> createResponseEntity(@Nullable Object body, | ||
| HttpHeaders headers, | ||
| HttpStatusCode statusCode, | ||
| WebRequest request) { | ||
| if (body instanceof ProblemDetail problemDetail) { | ||
| customize(problemDetail, request, null); | ||
| } | ||
| return super.createResponseEntity(body, headers, statusCode, request); | ||
| } | ||
|
|
||
| protected org.springframework.http.ResponseEntity<Object> createSofaResponseEntity(Exception ex, | ||
| HttpStatus status, | ||
| String detail, | ||
| URI type, | ||
| String title, | ||
| WebRequest request) { | ||
| ProblemDetail problemDetail = createProblemDetail(ex, status, detail, null, null, request); | ||
| problemDetail.setType(type); | ||
| problemDetail.setTitle(title); | ||
| customize(problemDetail, request, ex); | ||
| return super.createResponseEntity(problemDetail, new HttpHeaders(), status, request); | ||
| } | ||
|
|
||
| void customize(ProblemDetail problemDetail, WebRequest request, @Nullable Throwable throwable) { | ||
| if (problemDetail.getType() == null || ABOUT_BLANK.equals(problemDetail.getType())) { | ||
| problemDetail.setType(this.properties.getDefaultType()); | ||
| } | ||
|
|
||
| if (problemDetail.getInstance() == null) { | ||
| URI instance = resolveInstance(request); | ||
| if (instance != null) { | ||
| problemDetail.setInstance(instance); | ||
| } | ||
| } | ||
|
|
||
| if (this.properties.isIncludeServiceInfo() && StringUtils.hasText(this.applicationName) | ||
| && !hasProperty(problemDetail, "service")) { | ||
| problemDetail.setProperty("service", this.applicationName); | ||
| } | ||
|
|
||
| if (throwable == null) { | ||
| return; | ||
| } | ||
|
|
||
| String errorCode = extractErrorCode(throwable); | ||
| if (StringUtils.hasText(errorCode) && !hasProperty(problemDetail, "errorCode")) { | ||
| problemDetail.setProperty("errorCode", errorCode); | ||
| } | ||
|
|
||
| if (this.properties.isIncludeStackTrace() && !hasProperty(problemDetail, "stackTrace")) { | ||
| problemDetail.setProperty("stackTrace", toStackTrace(throwable)); | ||
| } | ||
| } | ||
|
|
||
| private boolean hasProperty(ProblemDetail problemDetail, String propertyName) { | ||
| Map<String, Object> properties = problemDetail.getProperties(); | ||
| return properties != null && properties.containsKey(propertyName); | ||
| } | ||
|
|
||
| @Nullable | ||
| private URI resolveInstance(WebRequest request) { | ||
| if (request instanceof ServletWebRequest servletWebRequest) { | ||
| String requestUri = servletWebRequest.getRequest().getRequestURI(); | ||
| if (StringUtils.hasText(requestUri)) { | ||
| return URI.create(requestUri); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable | ||
| private String extractErrorCode(Throwable throwable) { | ||
| Throwable current = throwable; | ||
| while (current != null) { | ||
| String message = current.getMessage(); | ||
| if (StringUtils.hasText(message)) { | ||
| Matcher matcher = ERROR_CODE_PATTERN.matcher(message); | ||
| if (matcher.find()) { | ||
| return matcher.group(1); | ||
| } | ||
| } | ||
| current = current.getCause(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private String toStackTrace(Throwable throwable) { | ||
| StringWriter stringWriter = new StringWriter(); | ||
| try (PrintWriter printWriter = new PrintWriter(stringWriter)) { | ||
| throwable.printStackTrace(printWriter); | ||
| } | ||
| return stringWriter.toString(); | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
...ure/src/main/java/com/alipay/sofa/boot/autoconfigure/web/SofaProblemDetailProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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.alipay.sofa.boot.autoconfigure.web; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| /** | ||
| * Configuration properties for SOFA ProblemDetail support. | ||
| */ | ||
| @ConfigurationProperties(SofaProblemDetailProperties.PREFIX) | ||
| public class SofaProblemDetailProperties { | ||
|
|
||
| public static final String PREFIX = "sofa.web.problem-detail"; | ||
|
|
||
| private boolean enabled = true; | ||
|
|
||
| private URI defaultType = URI.create("about:blank"); | ||
|
|
||
| private boolean includeStackTrace = false; | ||
|
|
||
| private boolean includeServiceInfo = true; | ||
|
|
||
| public boolean isEnabled() { | ||
| return this.enabled; | ||
| } | ||
|
|
||
| public void setEnabled(boolean enabled) { | ||
| this.enabled = enabled; | ||
| } | ||
|
|
||
| public URI getDefaultType() { | ||
| return this.defaultType; | ||
| } | ||
|
|
||
| public void setDefaultType(URI defaultType) { | ||
| this.defaultType = defaultType; | ||
| } | ||
|
|
||
| public boolean isIncludeStackTrace() { | ||
| return this.includeStackTrace; | ||
| } | ||
|
|
||
| public void setIncludeStackTrace(boolean includeStackTrace) { | ||
| this.includeStackTrace = includeStackTrace; | ||
| } | ||
|
|
||
| public boolean isIncludeServiceInfo() { | ||
| return this.includeServiceInfo; | ||
| } | ||
|
|
||
| public void setIncludeServiceInfo(boolean includeServiceInfo) { | ||
| this.includeServiceInfo = includeServiceInfo; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At present, none of these error types exist and the relevant descriptions need to be removed.