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

com.github.dakusui.actionunit.exceptions.ExceptionMapping Maven / Gradle / Ivy

There is a newer version: 6.1.5
Show newest version
package com.github.dakusui.actionunit.exceptions;

import java.lang.reflect.InvocationTargetException;

import static java.lang.String.format;

public interface ExceptionMapping {
  Class getApplicationExceptionClass();

  Class getNativeExceptionClass();

  /**
   * @param  Application exception
   */
  interface Resolver {
    AE resolve(Throwable t);

    enum Factory {
      ;

      public static , AE extends RuntimeException> Resolver create(final Class mappingClass) {
        return new Resolver() {
          @Override
          public AE resolve(Throwable t) {
            if (t == null) {
              throw new Error("null is not allowed here");
            }
            for (T each : mappingClass.getEnumConstants()) {
              if (matches(each, t.getClass())) {
                throw instantiate(each.getApplicationExceptionClass(), t);
              }
            }
            ////
            // For unknown type of checked exception. Once this line is executed, consider
            // adding new mapping to your enumeration.
            throw error(
                "Consider adding a new mapping to " + mappingClass.getCanonicalName() +
                    "for " + t.getClass().getCanonicalName() + ". : %s", t);
          }

          private boolean matches(T each, Class originalExceptionClass) {
            return each.getNativeExceptionClass().isAssignableFrom(originalExceptionClass);
          }

          private AE instantiate(Class exceptionClass, Throwable nested) {
            try {
              return exceptionClass.getDeclaredConstructor(String.class, Throwable.class).newInstance(nested.getMessage(), nested);
            } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
              Throwable t = e instanceof InvocationTargetException
                  ? ((InvocationTargetException) e).getTargetException()
                  : e;
              throw error(format(
                  "Failed to instantiate '%s' through its constructor whose parameters are (String, Throwable)"
                      + " since %s was thrown."
                      + " Make sure it exists, is public, and does not throw an exception.",
                  exceptionClass,
                  t.getClass().getSimpleName()) + ": %s",
                  t);
            }
          }

          private Error error(String messageFormat, Throwable nested) {
            throw new Error(format(messageFormat, nested.getMessage()), nested);
          }
        };
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy