-
Notifications
You must be signed in to change notification settings - Fork 66
EPMRPP-100452 || refactor exception handling #2246
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2019 EPAM Systems | ||
* | ||
* 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.epam.ta.reportportal.exception; | ||
|
||
import com.epam.reportportal.rules.exception.ErrorType; | ||
import com.epam.reportportal.rules.exception.ReportPortalException; | ||
import com.epam.ta.reportportal.exception.message.ArgumentNotValidMessageBuilder; | ||
import com.epam.ta.reportportal.exception.message.DefaultExceptionMessageBuilder; | ||
import com.epam.ta.reportportal.exception.message.ExceptionMessageBuilder; | ||
import com.epam.ta.reportportal.exception.rest.RestErrorDefinition; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.util.Map; | ||
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.LockedException; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.MissingServletRequestParameterException; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.multipart.support.MissingServletRequestPartException; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* Set of default exception mappings | ||
* | ||
* @author Andrei Varabyeu | ||
*/ | ||
public final class ExceptionMappings { | ||
|
||
private static final ReloadableResourceBundleMessageSource MESSAGE_SOURCE = new ReloadableResourceBundleMessageSource() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
setBasename("classpath:ValidationMessages"); | ||
setDefaultEncoding("UTF-8"); | ||
} | ||
}; | ||
|
||
private static final ExceptionMessageBuilder<Exception> DEFAULT_MESSAGE_BUILDER = new DefaultExceptionMessageBuilder(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
public static Map<Class<? extends Throwable>, RestErrorDefinition> DEFAULT_MAPPING = ImmutableMap | ||
.<Class<? extends Throwable>, RestErrorDefinition>builder() | ||
.put(MethodArgumentNotValidException.class, new RestErrorDefinition<>(400, ErrorType.INCORRECT_REQUEST, new ArgumentNotValidMessageBuilder(MESSAGE_SOURCE))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(HttpMessageNotReadableException.class, new RestErrorDefinition<>(400, ErrorType.INCORRECT_REQUEST, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(MissingServletRequestPartException.class, new RestErrorDefinition<>(400, ErrorType.INCORRECT_REQUEST, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(MissingServletRequestParameterException.class, new RestErrorDefinition<>(400, ErrorType.INCORRECT_REQUEST, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(AccessDeniedException.class, new RestErrorDefinition<>(403, ErrorType.ACCESS_DENIED, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(BadCredentialsException.class, new RestErrorDefinition<>(401, ErrorType.ACCESS_DENIED, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(LockedException.class, new RestErrorDefinition<>(403, ErrorType.ADDRESS_LOCKED, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(RestClientException.class, new RestErrorDefinition<>(400, ErrorType.UNABLE_INTERACT_WITH_INTEGRATION, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.put(Throwable.class, new RestErrorDefinition<>(500, ErrorType.UNCLASSIFIED_ERROR, DEFAULT_MESSAGE_BUILDER)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
.build(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2019 EPAM Systems | ||
* | ||
* 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.epam.ta.reportportal.exception.forwarding; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
import lombok.Setter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* {@link HandlerExceptionResolver} Checks of exception contains response of downstream service and copies it to upstream response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @author Andrei Varabyeu | ||
*/ | ||
@Setter | ||
@Service | ||
public class ClientResponseForwardingExceptionHandler implements Ordered { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ClientResponseForwardingExceptionHandler.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private int order; | ||
|
||
public ResponseEntity<String> resolveException(Exception ex) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
try { | ||
ResponseForwardingException forwardingException = (ResponseForwardingException) ex; | ||
var body = new String(forwardingException.getBody(), StandardCharsets.UTF_8); | ||
return ResponseEntity | ||
.status(forwardingException.getStatus()) | ||
.contentType(Objects.requireNonNull(forwardingException.getHeaders().getContentType())) | ||
.body(body); | ||
|
||
} catch (Exception e) { | ||
LOGGER.error("Cannot forward exception", e); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return order; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2019 EPAM Systems | ||
* | ||
* 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. | ||
*/ | ||
/* | ||
* This file is part of Report Portal. | ||
* | ||
* Report Portal is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Report Portal is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Report Portal. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.epam.ta.reportportal.exception.forwarding; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
import com.google.common.io.ByteStreams; | ||
import java.io.IOException; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* Exception to be forwarded from downstream to upstream microservice | ||
* | ||
* @author Andrei Varabyeu | ||
*/ | ||
@Getter | ||
public class ResponseForwardingException extends RuntimeException { | ||
|
||
private final byte[] body; | ||
private final HttpHeaders headers; | ||
private final HttpStatus status; | ||
|
||
public ResponseForwardingException(ClientHttpResponse response) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.headers = response.getHeaders(); | ||
this.status = HttpStatus.valueOf(response.getStatusCode().value()); | ||
this.body = ByteStreams.toByteArray(response.getBody()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2019 EPAM Systems | ||
* | ||
* 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.epam.ta.reportportal.exception.message; | ||
|
||
import java.util.stream.Collectors; | ||
import org.springframework.context.i18n.LocaleContextHolder; | ||
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* {@link MethodArgumentNotValidException} message builder | ||
* | ||
* @author Andrei Varabyeu | ||
*/ | ||
public class ArgumentNotValidMessageBuilder implements ExceptionMessageBuilder<MethodArgumentNotValidException> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private final ReloadableResourceBundleMessageSource messageSource; | ||
|
||
public ArgumentNotValidMessageBuilder(ReloadableResourceBundleMessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
@Override | ||
public String buildMessage(MethodArgumentNotValidException e) { | ||
return e.getBindingResult().getAllErrors().stream() | ||
.map(err -> messageSource.getMessage(err, LocaleContextHolder.getLocale())) | ||
.collect(Collectors.joining("", "[", "] ")); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019 EPAM Systems | ||
* | ||
* 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.epam.ta.reportportal.exception.message; | ||
|
||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* Default exception builder. Just reads the exception message | ||
* | ||
* @author Andrei Varabyeu | ||
*/ | ||
public class DefaultExceptionMessageBuilder implements ExceptionMessageBuilder<Exception> { | ||
|
||
@Override | ||
public String buildMessage(Exception e) { | ||
return StringUtils.substringBefore(e.getMessage(), "\n"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019 EPAM Systems | ||
* | ||
* 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.epam.ta.reportportal.exception.message; | ||
|
||
/** | ||
* Builds custom exception strings from Exception. Lots of exceptions have custom data which should | ||
* be shown to the clients in a custom way | ||
* | ||
* @author Andrei Varabyeu | ||
*/ | ||
public interface ExceptionMessageBuilder<T extends Exception> { | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* Builds message string from provided exception | ||
* | ||
* @param e Exception message should be built from | ||
* @return Built message | ||
*/ | ||
String buildMessage(T e); | ||
} |
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.
'package' should be separated from previous line.