Skip to content

Commit

Permalink
RHDM-919: Improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli committed May 9, 2019
1 parent 497c37c commit a738b9f
Show file tree
Hide file tree
Showing 42 changed files with 615 additions and 159 deletions.
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,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);
}
};
}
}
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,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;
}
}
}

0 comments on commit a738b9f

Please sign in to comment.