All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dragome.forms.bindings.client.command.ExceptionManager Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
package com.dragome.forms.bindings.client.command;

import java.util.HashMap;

/**
 * A class that can be used to process exceptions from async callbacks.
 * 
 * ExceptionManger exceptionManager = ...;
 *
 * // Configure our exception handling.
 * // Some exceptions just translate to messages...
 * exceptionManager.onCatching(UserNameInUseException.class)
 *    .publishError(constants.userNameInUseMessage());
 *
 * // other exceptions need explicit handling...
 * exceptionManager.onCatching(StaleEntityException.class)
 *    .invoke(new ExceptionHandler() {
 *        public void handle(StaleEntityException error) {
 *           // do things like refreshing the entity...
 *           ...
 *           // and finally publish the error
 *           publishError(messages.staleEntityMessage());
 *        }
 *    });
 *
 * // We also need a default handler for all those RPC exceptions..
 * exceptionManager.onUnregisteredExceptionsInvoke(new ExceptionHandler() {
 *    public void handle(Throwable error) {
 *       // handle things like status code exceptions etc.
 *    }
 * });
 *
 *
 * // And finally we can use it in our async command
 * public void performAsyncOperation(final AsyncCommandCallback commandCallback) {
 *    service.doStuff(asAsyncCallback(commandCallback, exceptionManager));
 * }
 * 
* * The manager also provides the method {@link #containsHandlerFor(Class)} so you can perform unit testing * to ensure a handler is registered for all the declared exceptions of the service. *
 * public void testAllExceptionsAreHandled() {
 *   for(Class exceptionType : reflectivelyGetServiceExceptions(MyService.class, "doStuff")) {
 *     assertTrue(exceptionManager.containsHandlerFor(exceptionType));
 *   }
 * }
 * 
* */ public class ExceptionManager { private ExceptionHandler defaultHandler; private HashMap, ExceptionHandler> handlers= new HashMap, ExceptionHandler>(); public ExceptionHandlerBuilder onCatching(Class type) { if (containsHandlerFor(type)) { throw new IllegalStateException("Exception type already registered: " + type); } return new ExceptionHandlerBuilder(type); } public void onUnregisteredExceptionsInvoke(ExceptionHandler callback) { defaultHandler= callback; } public void processException(Throwable caught, AsyncCommandCallback callback) { ExceptionHandler handler= getHandlerFor(caught); if (handler == null) { throw new IllegalStateException("No handler registered for: " + caught); } handler.process(caught, callback); } private ExceptionHandler getHandlerFor(Throwable caught) { ExceptionHandler handler= handlers.get(caught.getClass()); return (handler != null) ? handler : defaultHandler; } public boolean containsHandlerFor(Class type) { return handlers.containsKey(type); } public class ExceptionHandlerBuilder { private Class type; public ExceptionHandlerBuilder(Class type) { this.type= type; } /** * Invokes the custom error handler. The handler can perform additional operations * and choose publish a error or abort the command. * @param handler the handler to invoke. */ public void invoke(ExceptionHandler handler) { handlers.put(type, handler); } /** * Publishes the specified error. * @param error the error to publish. */ public void publishError(final E error) { handlers.put(type, new ExceptionHandler() { @Override public void handle(T caught) { publishError(error); } }); } /** * Aborts the execution and not error will be published. */ public void abort() { handlers.put(type, new ExceptionHandler() { @Override public void handle(Throwable error) { abort(); } }); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy