Skip to content
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
merged 8 commits into from
May 28, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.jboss.errai.ioc.client.api.EntryPoint;
import org.jboss.errai.ui.shared.api.annotations.Bundle;
import org.optaweb.employeerostering.gwtui.client.app.NavigationController;
import org.optaweb.employeerostering.gwtui.client.common.FailureShownRestCallback;
import org.optaweb.employeerostering.gwtui.client.common.FailureShownRestCallbackFactory;
import org.optaweb.employeerostering.gwtui.client.notification.NotificationFactory;
import org.optaweb.employeerostering.gwtui.client.pages.Pages;
import org.optaweb.employeerostering.gwtui.client.resources.i18n.I18nKeys;
Expand Down Expand Up @@ -56,6 +56,9 @@ public class OptaWebEntryPoint {
@Inject
private NotificationFactory notificationFactory;

@Inject
private FailureShownRestCallbackFactory restCallbackFactory;

private boolean isPageLoaded;

@PostConstruct
Expand Down Expand Up @@ -94,7 +97,7 @@ public void onNoTenants(final @Observes TenantStore.NoTenants noTenants) {
}

private void healthCheck() {
TenantRestServiceBuilder.getTenantList(FailureShownRestCallback.onSuccess(tenantList -> {
TenantRestServiceBuilder.getTenantList(restCallbackFactory.onSuccess(tenantList -> {
if (null == tenantList) {
notificationFactory.showErrorMessage(I18nKeys.OptaWebEntryPoint_cannotContactServer, Window.Location.getHref());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

import java.util.function.Consumer;

import javax.inject.Inject;

public class CallbackFactory {

public <T> FailureShownRestCallback<T> onSuccess(final Consumer<T> onSuccess) {
return new FailureShownRestCallback<T>() {
@Inject
private FailureShownRestCallbackFactory restCallbackFactory;

@Override
public void onSuccess(final T ret) {
onSuccess.accept(ret);
}
};
public <T> FailureShownRestCallbackFactory.FailureShownRestCallback<T> onSuccess(final Consumer<T> onSuccess) {
return restCallbackFactory.onSuccess(onSuccess);
}
}

This file was deleted.

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) {
Copy link
Collaborator

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 of onFailure ?

Copy link
Collaborator Author

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).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

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);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class StringListToSkillSetConverter implements Converter<Set<Skill>, List
@Inject
private EventManager eventManager;

@Inject
private FailureShownRestCallbackFactory restCallbackFactory;

private Map<String, Skill> skillMap;

@PostConstruct
Expand Down Expand Up @@ -112,7 +115,7 @@ private void updateSkillMappings(List<Skill> skillList) {
}

private Promise<List<Skill>> getSkillList() {
return new Promise<>((resolve, reject) -> SkillRestServiceBuilder.getSkillList(tenantStore.getCurrentTenantId(), FailureShownRestCallback
return new Promise<>((resolve, reject) -> SkillRestServiceBuilder.getSkillList(tenantStore.getCurrentTenantId(), restCallbackFactory
.onSuccess(newSkillList -> {
resolve.onInvoke(newSkillList);
})));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class StringToContractConverter implements Converter<Contract, String> {
@Inject
private EventManager eventManager;

@Inject
private FailureShownRestCallbackFactory restCallbackFactory;

private Map<String, Contract> contractMap;

@PostConstruct
Expand Down Expand Up @@ -106,7 +109,7 @@ private void updateContactMappings(List<Contract> contractList) {
}

private Promise<List<Contract>> getContractList() {
return new Promise<>((resolve, reject) -> ContractRestServiceBuilder.getContractList(tenantStore.getCurrentTenantId(), FailureShownRestCallback
return new Promise<>((resolve, reject) -> ContractRestServiceBuilder.getContractList(tenantStore.getCurrentTenantId(), restCallbackFactory
.onSuccess(resolve::onInvoke)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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;
}

protected 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;
}

protected static StackTraceElement[] extractStackTrace(ServerSideExceptionInfo serverSideException) {
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 {
if (stackTraceLine.substring(fileNameLocation).equals("(Native Method)")) {
stackTrace[i] = new StackTraceElement(className, methodName, null, -2);
} else if (stackTraceLine.substring(fileNameLocation).equals("(Unknown Source)")) {
stackTrace[i] = new StackTraceElement(className, methodName, null, -1);
} else {
String fileName = stackTraceLine.substring(fileNameLocation + 1, stackTraceLine.length() - 1);
stackTrace[i] = new StackTraceElement(className, methodName, fileName, -1);
}
}
}
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;
}
}
}