-
Notifications
You must be signed in to change notification settings - Fork 165
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
RHDM-919: Improved error messages #238
Merged
Christopher-Chianelli
merged 8 commits into
kiegroup:master
from
Christopher-Chianelli:RHDM-919
May 28, 2019
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a738b9f
RHDM-919: Improved error messages
Christopher-Chianelli 23d15fe
Refactor code to use enums, remove some code smells
Christopher-Chianelli 4ef063c
Fixed some bugs
Christopher-Chianelli fa0e945
Add a HierarchyTree data type to find the most specific exception cla…
Christopher-Chianelli 0888da2
Changed catch Throwable to catch Exception, used a List to remove nul…
Christopher-Chianelli 8e05376
Fixed a formatting issue
Christopher-Chianelli 6e0efb6
Add client side tests for reading errors from the server
Christopher-Chianelli 4cd97ad
Remove Error Popup Icon, Add ExceptionMapper tests
Christopher-Chianelli File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
66 changes: 0 additions & 66 deletions
66
...main/java/org/optaweb/employeerostering/gwtui/client/common/FailureShownRestCallback.java
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
...va/org/optaweb/employeerostering/gwtui/client/common/FailureShownRestCallbackFactory.java
This file contains 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,90 @@ | ||
/* | ||
* Copyright 2018 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.optaweb.employeerostering.gwtui.client.common; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import com.github.nmorel.gwtjackson.rest.api.RestCallback; | ||
import com.google.gwt.core.client.GWT; | ||
import com.google.gwt.http.client.Response; | ||
import org.jboss.errai.ui.client.local.spi.TranslationService; | ||
import org.optaweb.employeerostering.gwtui.client.exception.RESTException; | ||
import org.optaweb.employeerostering.gwtui.client.gwtjackson.ServerSideExceptionDeserializer; | ||
import org.optaweb.employeerostering.gwtui.client.notification.NotificationFactory; | ||
import org.optaweb.employeerostering.shared.exception.ServerSideExceptionInfo; | ||
|
||
@Singleton | ||
public class FailureShownRestCallbackFactory { | ||
|
||
@Inject | ||
private TranslationService translationService; | ||
|
||
@Inject | ||
private NotificationFactory notificationFactory; | ||
|
||
@Inject | ||
private ServerSideExceptionDeserializer serverSideExceptionDeserializer; | ||
|
||
public abstract class FailureShownRestCallback<T> extends RestCallback<T> { | ||
|
||
private Consumer<Response> onError = response -> { | ||
if (response.getHeader("Content-Type").equals(MediaType.APPLICATION_JSON)) { | ||
ServerSideExceptionInfo exception = serverSideExceptionDeserializer.deserializeFromJsonString(response.getText()); | ||
GWT.getUncaughtExceptionHandler().onUncaughtException(new RESTException(exception, translationService)); | ||
} else { | ||
notificationFactory.showErrorMessage(response.getText()); | ||
} | ||
}; | ||
|
||
private Consumer<Throwable> onFailure = throwable -> | ||
GWT.getUncaughtExceptionHandler().onUncaughtException(throwable); | ||
|
||
@Override | ||
public void onError(final Response response) { | ||
onError.accept(response); | ||
} | ||
|
||
@Override | ||
public void onFailure(final Throwable throwable) { | ||
onFailure.accept(throwable); | ||
} | ||
|
||
public FailureShownRestCallback<T> onError(final Consumer<Response> onError) { | ||
this.onError = onError; | ||
return this; | ||
} | ||
|
||
public FailureShownRestCallback<T> onFailure(final Consumer<Throwable> onFailure) { | ||
this.onFailure = onFailure; | ||
return this; | ||
} | ||
} | ||
|
||
public <T> FailureShownRestCallback<T> onSuccess(final Consumer<T> onSuccess) { | ||
return new FailureShownRestCallback<T>() { | ||
|
||
@Override | ||
public void onSuccess(final T ret) { | ||
onSuccess.accept(ret); | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
This file contains 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
90 changes: 90 additions & 0 deletions
90
...tui/src/main/java/org/optaweb/employeerostering/gwtui/client/exception/RESTException.java
This file contains 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,90 @@ | ||
/* | ||
* Copyright 2019 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.optaweb.employeerostering.gwtui.client.exception; | ||
|
||
import org.jboss.errai.ui.client.local.spi.TranslationService; | ||
import org.optaweb.employeerostering.shared.exception.ServerSideExceptionInfo; | ||
|
||
public class RESTException extends Exception { | ||
|
||
private final transient ServerSideExceptionInfo serverSideException; | ||
private final transient TranslationService translationService; | ||
|
||
public RESTException(ServerSideExceptionInfo serverSideException, TranslationService translationService) { | ||
super(getExceptionFrom(serverSideException)); | ||
this.serverSideException = serverSideException; | ||
this.translationService = translationService; | ||
} | ||
|
||
private static Exception getExceptionFrom(ServerSideExceptionInfo serverSideException) { | ||
Exception out; | ||
if (serverSideException.getExceptionCause() != null) { | ||
out = new ServerException(getExceptionFrom(serverSideException.getExceptionCause()), serverSideException.getExceptionClass() + ": " + serverSideException.getExceptionMessage()); | ||
} else { | ||
out = new ServerException(serverSideException.getExceptionClass() + ": " + serverSideException.getExceptionMessage()); | ||
} | ||
out.setStackTrace(extractStackTrace(serverSideException)); | ||
return out; | ||
} | ||
|
||
private static StackTraceElement[] extractStackTrace(ServerSideExceptionInfo serverSideException) { | ||
Christopher-Chianelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
StackTraceElement[] stackTrace = new StackTraceElement[serverSideException.getStackTrace().size()]; | ||
Christopher-Chianelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int i = 0; i < stackTrace.length; i++) { | ||
String stackTraceLine = serverSideException.getStackTrace().get(i); | ||
int fileNameLocation = stackTraceLine.indexOf('('); | ||
int lineNumberLocation = stackTraceLine.indexOf(':', fileNameLocation); | ||
int methodNameLocation = stackTraceLine.lastIndexOf('.', fileNameLocation); | ||
String className = stackTraceLine.substring(0, methodNameLocation); | ||
String methodName = stackTraceLine.substring(methodNameLocation + 1, fileNameLocation); | ||
|
||
if (lineNumberLocation != -1) { | ||
String fileName = stackTraceLine.substring(fileNameLocation + 1, lineNumberLocation); | ||
int lineNumber = Integer.parseInt(stackTraceLine.substring(lineNumberLocation + 1, stackTraceLine.length() - 1)); | ||
stackTrace[i] = new StackTraceElement(className, methodName, fileName, lineNumber); | ||
} else { | ||
stackTrace[i] = new StackTraceElement(className, methodName, "Unknown Source", 0); | ||
} | ||
} | ||
return stackTrace; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return translationService.format(serverSideException.getI18nKey(), | ||
serverSideException.getMessageParameters().toArray()); | ||
} | ||
|
||
private static class ServerException extends Exception { | ||
|
||
private final String message; | ||
|
||
public ServerException(Throwable cause, String message) { | ||
super(cause); | ||
this.message = message; | ||
} | ||
|
||
public ServerException(String message) { | ||
super(); | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
} | ||
} |
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.
Would you please explain why is the behaviour of
onError
different than ofonFailure
?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.
onError
occurs when we have a Response (which may have a Json Body and if so contain a Server Exception);onFailure
occurs when we only have a Throwable (and thus cannot extract a Server Exception from it).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.
Thank you