conorgriffin:
public String getSanitisedMessage() {
Throwable rootCause = context.getRootCauseException();
if(rootCause != null) {
return Optional.ofNullable(rootCause.getMessage())
.map(message -> Stream.of(
// clean message substrings we want to find
"Connection timed out",
"Connection reset",
"Connection was lost",
"FTP Fails"
).filter(subString -> message
.toLowerCase()
.contains(subString.toLowerCase())
).findFirst().orElse("NA")
).orElse("NA");
} else return "NA";
}
目的は確認することですThrowable
そう返し、ストリングのためのメッセージをしてから部分文字列を返した場合NA
。どちらcontext.getRootCauseException()
とThrowable.getMessage()
呼び出しが返すことができますnull
。
また:
1つの可能な方法で使用することがあるflatMap
とfindFirst
の代わりmap
として:
// method argument is just for the sake of an example and clarification here
public String getSanitisedMessage(Throwable rootCause, Set<String> primaryCauses) {
return Optional.ofNullable(rootCause)
.map(Throwable::getMessage)
.map(String::toLowerCase)
.flatMap(message -> primaryCauses.stream()
.map(String::toLowerCase)
.filter(message::contains)
.findFirst())
.orElse("NA");
}
または三項演算子はまた、それを表すために使用することができます:
return rootCause == null || rootCause.getMessage() == null ? "NA" :
primaryCauses.stream().map(String::toLowerCase).filter(subString -> rootCause.getMessage()
.toLowerCase().contains(subString)).findFirst().orElse("NA");