Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.io7m.changelog.parser.api.CParseErrorHandlers Maven / Gradle / Ivy
/*
* Copyright © 2017 http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.io7m.changelog.parser.api;
import com.io7m.junreachable.UnreachableCodeException;
import org.slf4j.Logger;
import java.net.URI;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
/**
* Functions to handle parse errors.
*/
public final class CParseErrorHandlers
{
private CParseErrorHandlers()
{
throw new UnreachableCodeException();
}
/**
* A parse error handler that formats and logs all errors to the given
* logger.
*
* @param logger The logger
*
* @return An error handler
*/
public static Consumer loggingHandler(
final Logger logger)
{
Objects.requireNonNull(logger, "Logger");
return error -> {
final Optional file_opt = error.lexical().file();
final Optional ex_opt = error.exception();
switch (error.severity()) {
case WARNING: {
onWarn(logger, error, file_opt, ex_opt);
break;
}
case ERROR: {
onError(logger, error, file_opt, ex_opt);
break;
}
case CRITICAL: {
onError(logger, error, file_opt, ex_opt);
break;
}
}
};
}
private static void onWarn(
final Logger logger,
final CParseError error,
final Optional file_opt,
final Optional ex_opt)
{
if (file_opt.isPresent()) {
if (ex_opt.isPresent()) {
logger.warn(
"{}:{}:{}: {}: ",
file_opt.get(),
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message(),
ex_opt.get());
} else {
logger.warn(
"{}:{}:{}: {}",
file_opt.get(),
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message());
}
} else {
if (ex_opt.isPresent()) {
logger.warn(
"{}:{}: {}: ",
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message(),
ex_opt.get());
} else {
logger.warn(
"{}:{}: {}",
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message());
}
}
}
private static void onError(
final Logger logger,
final CParseError error,
final Optional file_opt,
final Optional ex_opt)
{
if (file_opt.isPresent()) {
if (ex_opt.isPresent()) {
logger.error(
"{}:{}:{}: {}: ",
file_opt.get(),
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message(),
ex_opt.get());
} else {
logger.error(
"{}:{}:{}: {}",
file_opt.get(),
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message());
}
} else {
if (ex_opt.isPresent()) {
logger.error(
"{}:{}: {}: ",
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message(),
ex_opt.get());
} else {
logger.error(
"{}:{}: {}",
Integer.valueOf(error.lexical().line()),
Integer.valueOf(error.lexical().column()),
error.message());
}
}
}
}