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

ru.tinkoff.kora.validation.common.ViolationException Maven / Gradle / Ivy

There is a newer version: 1.1.16
Show newest version
package ru.tinkoff.kora.validation.common;

import jakarta.annotation.Nonnull;
import java.util.List;

public final class ViolationException extends RuntimeException {

    private String _message;
    private final List violations;

    public ViolationException(@Nonnull List violations) {
        super();
        this.violations = violations;
    }

    @Nonnull
    public List getViolations() {
        return violations;
    }

    @Override
    public String getMessage() {
        if (_message == null) {
            _message = buildViolationMessage(violations);
        }

        return _message;
    }

    private static String buildViolationMessage(List violations) {
        final StringBuilder builder = new StringBuilder("Validation failed with violations:\n");
        for (int i = 1; i <= violations.size(); i++) {
            final Violation violation = violations.get(i - 1);
            builder.append(i)
                .append(") Path '")
                .append(violation.path().full())
                .append("' violation: ")
                .append(violation.message())
                .append(';');

            if (i != violations.size()) {
                builder.append("\n");
            }
        }

        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy