Skip to content

Commit

Permalink
MID-8842 ninja - error handling fixes, fixing outstanding todos
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jul 19, 2023
1 parent 5d634f6 commit e260d4e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.evolveum.midpoint.schema.validator.processor;

import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.path.ItemPath;
Expand Down Expand Up @@ -41,8 +43,6 @@ default RoleCatalogType getRoleCatalog(SystemConfigurationType system) {
}

/**
* todo are we really matching only templates?
*
* Matches object type and path template (without container ids in case of multivalue containers).
*
* @param object tested object
Expand All @@ -52,12 +52,14 @@ default RoleCatalogType getRoleCatalog(SystemConfigurationType system) {
* @param <O>
* @return true if matches
*/
default <O extends ObjectType> boolean matchObjectTypeAndPathTemplate(PrismObject<?> object, ItemPath path, Class<O> type, ItemPath expected) {
default <O extends ObjectType> boolean matchObjectTypeAndPathTemplate(
@NotNull PrismObject<?> object, @NotNull ItemPath path, @NotNull Class<O> type, @NotNull ItemPath expected) {

if (!type.isAssignableFrom(object.getCompileTimeClass())) {
return false;
}

if (!path.equivalent(expected)) {
if (!path.namedSegmentsOnly().equivalent(expected)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public VerifyResult execute() throws Exception {
}

private VerifyResult verifyFiles() throws IOException {
VerificationReporter reporter = new VerificationReporter(options, context.getPrismContext(), context.getCharset());
VerificationReporter reporter = new VerificationReporter(options, context.getPrismContext(), context.getCharset(), log);
reporter.setCreateDeltaFile(true);

try (Writer writer = NinjaUtils.createWriter(
Expand Down Expand Up @@ -115,7 +115,7 @@ private void verifyFile(File file, VerificationReporter reporter, Writer writer)
reporter.verify(writer, object);
}
} catch (Exception ex) {
NinjaUtils.logException(log, "Couldn't verify file '" + file.getPath() + "'", ex);
log.error("Couldn't verify file '{}'", ex, file.getPath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
import java.util.Objects;
import java.util.UUID;

import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.DeltaConversionOptions;
import com.evolveum.midpoint.schema.DeltaConvertor;

import com.evolveum.midpoint.util.exception.SchemaException;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
Expand All @@ -23,12 +17,17 @@

import com.evolveum.midpoint.ninja.action.VerifyOptions;
import com.evolveum.midpoint.ninja.action.VerifyResult;
import com.evolveum.midpoint.ninja.impl.Log;
import com.evolveum.midpoint.ninja.impl.NinjaException;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.DeltaConversionOptions;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.validator.*;
import com.evolveum.midpoint.util.LocalizableMessage;
import com.evolveum.midpoint.util.exception.SchemaException;

public class VerificationReporter {

Expand Down Expand Up @@ -60,6 +59,8 @@ public class VerificationReporter {

private final Charset charset;

private final Log log;

private ObjectUpgradeValidator validator;

private final VerifyResult result = new VerifyResult();
Expand All @@ -68,10 +69,11 @@ public class VerificationReporter {

private Writer deltaWriter;

public VerificationReporter(@NotNull VerifyOptions options, @NotNull PrismContext prismContext, Charset charset) {
public VerificationReporter(@NotNull VerifyOptions options, @NotNull PrismContext prismContext, @NotNull Charset charset, @NotNull Log log) {
this.options = options;
this.prismContext = prismContext;
this.charset = charset;
this.log = log;
}

public boolean isCreateDeltaFile() {
Expand Down Expand Up @@ -217,15 +219,18 @@ public <T extends Objectable> UpgradeValidationResult verify(Writer writer, Pris
}

private void writeDeltaXml(UpgradeValidationResult result) {
result.getItems().stream().filter(item -> item.getDelta() != null).forEach(item -> {
for (UpgradeValidationItem item : result.getItems()) {
if (item.getDelta() == null) {
continue;
}

try {
deltaWriter.write(DeltaConvertor.serializeDelta(
(ObjectDelta) item.getDelta(), DeltaConversionOptions.createSerializeReferenceNames(), "xml"));
} catch (SchemaException | IOException ex) {
// todo error handling
ex.printStackTrace();
log.error("Couldn't write object delta to XML file", ex);
}
});
}
}

public static String getItemPathFromRecord(CSVRecord record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public VerifyConsumerWorker(NinjaContext context, VerifyOptions options,

@Override
protected void init() {
reporter = new VerificationReporter(options, context.getPrismContext(), context.getCharset());
reporter = new VerificationReporter(options, context.getPrismContext(), context.getCharset(), log);
reporter.setCreateDeltaFile(true);
reporter.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,37 @@
package com.evolveum.midpoint.ninja.impl;

import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.ninja.util.ConsoleFormat;
import com.evolveum.midpoint.ninja.util.NinjaUtils;

/**
* Created by Viliam Repan (lazyman).
*/
public class Log {

private static final Pattern PATTERN = Pattern.compile("\\{}");

private enum Level {
ERROR, INFO, DEBUG
ERROR, INFO, DEBUG;

public ConsoleFormat.Level toConsoleFormatLevel() {
switch (this) {
case ERROR:
return ConsoleFormat.Level.ERROR;
case INFO:
return ConsoleFormat.Level.INFO;
case DEBUG:
return ConsoleFormat.Level.DEFAULT;
default:
throw new IllegalStateException("Unknown log level: " + this);
}
}
}

private final LogVerbosity level;
Expand All @@ -34,10 +55,14 @@ public void error(String message, Object... args) {
}

public void error(String message, Exception ex, Object... args) {
if (ex != null) {
message = message + ". Reason: " + ex.getMessage();
}

log(Level.ERROR, message, args);

if (ex != null && level == LogVerbosity.VERBOSE) {
log(Level.DEBUG, "Exception details", ex);
log(Level.DEBUG, "Exception details:\n{}", ex);
}
}

Expand All @@ -61,13 +86,23 @@ public void log(Level level, String message, Object... args) {
// all log levels should be printed
}

// todo simple replacement, should be improved later probably
String msg = message;
for (int i = 0; i < args.length; i++) {
String arg = args[i] != null ? args[i].toString() : "null";
msg = msg.replaceFirst("\\{}", arg);
Matcher matcher = PATTERN.matcher(message);

StringBuilder sb = new StringBuilder();

int i = 0;
while (matcher.find()) {
Object arg = args[i++];
if (arg instanceof Exception && level == Level.DEBUG) {
arg = NinjaUtils.printStackToString((Exception) arg);
}

matcher.appendReplacement(sb, Matcher.quoteReplacement(arg.toString()));
}
matcher.appendTail(sb);

ConsoleFormat.formatMessageWithParameter(level + ": ", sb.toString(), level.toConsoleFormatLevel());

stream.println(level + ": " + msg);
stream.println(level + ": " + sb);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@

public final class ConsoleFormat {

public enum Level {

DEFAULT(Ansi.Color.DEFAULT),

INFO(Ansi.Color.BLUE),

SUCCESS(Ansi.Color.GREEN),

WARN(Ansi.Color.YELLOW),

ERROR(Ansi.Color.RED);

final Ansi.Color color;

Level(Ansi.Color color) {
this.color = color;
}
}

private ConsoleFormat() {
}

Expand All @@ -19,34 +38,34 @@ public static String formatActionStartMessage(Action action) {
}

public static String formatSuccessMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Ansi.Color.GREEN);
return formatMessageWithParameter(message, parameter, Level.SUCCESS);
}

public static String formatWarnMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Ansi.Color.YELLOW);
return formatMessageWithParameter(message, parameter, Level.WARN);
}

public static String formatErrorMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Ansi.Color.RED);
return formatMessageWithParameter(message, parameter, Level.ERROR);
}

public static String formatInfoMessageWithParameter(String message, Object parameter) {
return formatMessageWithParameter(message, parameter, Ansi.Color.BLUE);
return formatMessageWithParameter(message, parameter, Level.INFO);
}

public static String formatMessageWithParameter(String message, Object parameter, Ansi.Color parameterColor) {
return Ansi.ansi().a(message).fg(parameterColor).a(parameter).reset().toString();
public static String formatMessageWithParameter(String message, Object parameter, Level level) {
return Ansi.ansi().a(message).fg(level.color).a(parameter).reset().toString();
}

public static String formatWarn(String message) {
return format(message, Ansi.Color.YELLOW);
return format(message, Level.WARN);
}

public static String formatError(String message) {
return format(message, Ansi.Color.RED);
return format(message, Level.ERROR);
}

public static String format(String message, Ansi.Color parameterColor) {
return Ansi.ansi().fg(parameterColor).a(message).reset().toString();
public static String format(String message, Level level) {
return Ansi.ansi().fg(level.color).a(message).reset().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.evolveum.midpoint.ninja.action.BaseOptions;
import com.evolveum.midpoint.ninja.action.ConnectionOptions;
import com.evolveum.midpoint.ninja.impl.Command;
import com.evolveum.midpoint.ninja.impl.Log;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.impl.NinjaException;
import com.evolveum.midpoint.prism.*;
Expand Down Expand Up @@ -286,10 +285,4 @@ public static String readInput(Function<String, Boolean> inputValidation) throws

return line;
}

public static void logException(Log log, String msg, Exception ex) {
log.error(ConsoleFormat.formatErrorMessageWithParameter(msg, ex.getMessage()));

log.debug("Exception stack:\n{}", printStackToString(ex));
}
}

0 comments on commit e260d4e

Please sign in to comment.