-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
497c37c
commit a738b9f
Showing
42 changed files
with
615 additions
and
159 deletions.
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.
82 changes: 82 additions & 0 deletions
82
...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,82 @@ | ||
/* | ||
* 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 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.shared.exception.ServerSideException; | ||
|
||
@Singleton | ||
public class FailureShownRestCallbackFactory { | ||
|
||
@Inject | ||
private TranslationService translationService; | ||
|
||
@Inject | ||
private ServerSideExceptionDeserializer serverSideExceptionDeserializer; | ||
|
||
public abstract class FailureShownRestCallback<T> extends RestCallback<T> { | ||
|
||
private Consumer<Response> onError = response -> { | ||
ServerSideException exception = serverSideExceptionDeserializer.deserializeFromJsonString(response.getText()); | ||
GWT.getUncaughtExceptionHandler().onUncaughtException(new RESTException(exception, translationService)); | ||
}; | ||
|
||
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
87 changes: 87 additions & 0 deletions
87
...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,87 @@ | ||
/* | ||
* 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.exception; | ||
|
||
import org.jboss.errai.ui.client.local.spi.TranslationService; | ||
import org.optaweb.employeerostering.shared.exception.ServerSideException; | ||
|
||
public class RESTException extends Exception { | ||
|
||
private ServerSideException serverSideException; | ||
private TranslationService translationService; | ||
|
||
public RESTException(ServerSideException serverSideException, TranslationService translationService) { | ||
super(getExceptionFrom(serverSideException)); | ||
this.serverSideException = serverSideException; | ||
this.translationService = translationService; | ||
} | ||
|
||
private static Exception getExceptionFrom(ServerSideException 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()); | ||
} | ||
|
||
StackTraceElement[] stackTrace = new StackTraceElement[serverSideException.getStackTrace().size()]; | ||
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); | ||
} | ||
} | ||
out.setStackTrace(stackTrace); | ||
return out; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return translationService.format(serverSideException.getI18nKey(), | ||
serverSideException.getMessageParameters().toArray()); | ||
} | ||
|
||
private static class ServerException extends Exception { | ||
|
||
private 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.