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

net.intelie.pipes.PipeException Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes;

import net.intelie.pipes.ast.SourceLocation;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class PipeException extends Exception {
    private static final long serialVersionUID = 1L;
    private static final boolean DEBUG = false;
    private final Level level;
    private final SourceLocation removedLocation;
    private final SourceLocation location;
    private final List causes;

    public PipeException(Object message) {
        this(null, Level.SEMANTIC, String.valueOf(message));
    }

    public PipeException(String message, Object... objs) {
        this(null, Level.SEMANTIC, format(message, objs));
    }

    public PipeException(SourceLocation location, Level level, String message) {
        this(location, location, level, locationMessage(location, message), new ArrayList<>());
    }

    private static String locationMessage(SourceLocation location, String message) {
        return (location != null ? "At (" + location + "): " : "") + message;
    }

    private PipeException(SourceLocation removedLocation, SourceLocation location, Level level, String message, List causes) {
        super(message, null, DEBUG, DEBUG);
        this.level = level;
        this.location = location;
        this.removedLocation = removedLocation;
        this.causes = causes;
    }


    public static void check(boolean condition, Object errorMessage) throws PipeException {
        if (!condition)
            throw new PipeException(String.valueOf(errorMessage));
    }

    public static void check(boolean condition, String errorMessage, Object... objs) throws PipeException {
        if (!condition)
            throw new PipeException(format(errorMessage, objs));
    }

    public static void checkNotNull(Object value, Object errorMessage) throws PipeException {
        if (value == null)
            throw new PipeException(String.valueOf(errorMessage));
    }

    public static void checkNotNull(Object value, String errorMessage, Object... objs) throws PipeException {
        if (value == null)
            throw new PipeException(format(errorMessage, objs));
    }

    public static PipeException handle(Throwable e) {
        if (e instanceof PipeException)
            return (PipeException) e;
        if (e instanceof InvocationTargetException)
            return handle(e.getCause());
        if (DEBUG)
            e.printStackTrace();
        return new PipeException("(%s) %s", e.getClass().getSimpleName(), e.getMessage());
    }

    public static String format(String template, Object... args) {
        template = String.valueOf(template); // null -> "null"

        // start substituting the arguments into the '%s' placeholders
        StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
        int templateStart = 0;
        int i = 0;
        while (i < args.length) {
            int placeholderStart = nextTemplateStart(template, templateStart);
            if (placeholderStart == -1) {
                break;
            }
            builder.append(template.substring(templateStart, placeholderStart));
            builder.append(args[i++]);
            templateStart = placeholderStart + 2;
        }
        builder.append(template.substring(templateStart));

        // if we run out of placeholders, append the extra args in square braces
        if (i < args.length) {
            builder.append(" [");
            builder.append(args[i++]);
            while (i < args.length) {
                builder.append(", ");
                builder.append(args[i++]);
            }
            builder.append(']');
        }

        return builder.toString();
    }

    private static int nextTemplateStart(String template, int templateStart) {
        int answer;
        do {
            answer = template.indexOf("%", templateStart);
            if (answer == -1 || answer + 1 >= template.length())
                return -1;
            templateStart += 1;
        } while (!Character.isAlphabetic(template.charAt(answer + 1)));
        return answer;
    }

    public PipeException maybeAddSource(SourceLocation location) {
        if (this.location != null) return this;
        PipeException ex = new PipeException(location, location, level,
                locationMessage(location, getMessage()), new ArrayList<>(causes));
        ex.setStackTrace(this.getStackTrace());
        return ex;
    }

    public Level level() {
        return level;
    }

    public SourceLocation location() {
        return location;
    }

    public PipeException addCause(PipeException cause) {
        this.causes.add(cause);
        return this;
    }

    public List causes() {
        return causes;
    }

    public SourceLocation removedLocation() {
        return removedLocation;
    }

    public PipeException maybeRemoveSource() {
        if (this.location == null) return this;
        PipeException ex = new PipeException(removedLocation, null, level, getMessage(), new ArrayList<>(causes));
        ex.setStackTrace(this.getStackTrace());
        return ex;
    }

    public enum Level {
        SYNTACTIC, SEMANTIC
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy