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

net.serenitybdd.screenplay.Complaint Maven / Gradle / Ivy

There is a newer version: 4.2.9
Show newest version
package net.serenitybdd.screenplay;

public class Complaint {

    public static final String NO_VALID_CONSTRUCTOR = "%s should have a constructor with the signature MyException(String message) and MyException(String message, Throwable cause)";

    public static Error from(Class complaintType,
                                        String complaintDetails,
                                        Throwable actualError) {

        if ((complaintDetails == null) && (actualError.getMessage() == null)) {
            return from(complaintType, actualError);
        }

        complaintDetails = errorMessageFrom(complaintDetails, actualError);

        try {
            return complaintType.getConstructor(String.class, Throwable.class).newInstance(complaintDetails, actualError);
        } catch (Exception e) {
            return new AssertionError(String.format(NO_VALID_CONSTRUCTOR, complaintType.getSimpleName()));
        }
    }

    private static String errorMessageFrom(String complaintDetails, Throwable actualError) {
        if (complaintDetails == null) {
            return actualError.getMessage();
        }
        if (actualError.getMessage() == null) {
            return complaintDetails;
        }
        return complaintDetails + " - " + actualError.getMessage();
    }

    public static Error from(Class complaintType, Throwable actualError) {
        try {
            return complaintType.getConstructor(Throwable.class).newInstance(actualError);
        } catch (Exception e) {
            try {
                return complaintType.getConstructor(String.class, Throwable.class).newInstance(actualError.getMessage(), actualError);
            } catch (Exception e1) {
                return new AssertionError(String.format(NO_VALID_CONSTRUCTOR, complaintType.getSimpleName()));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy