package at.gv.egiz.eaaf.core.impl.idp.auth.services; import java.text.MessageFormat; import java.util.Collections; import java.util.HashSet; import java.util.Map; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import at.gv.egiz.eaaf.core.api.IRequest; import at.gv.egiz.eaaf.core.api.IStatusMessenger; import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; import at.gv.egiz.eaaf.core.api.idp.IConfiguration; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; import lombok.Builder; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @Slf4j public class DefaultErrorService implements IErrorService { private static final String TECH_LOG_MSG = "errorCode={0} Message={1}"; private static final String CONFIG_PROP_LOGGER_ON_INFO_LEVEL = "core.logging.level.info.errorcodes"; @Autowired IConfiguration basicConfig; @Autowired IStatusMessenger statusMessager; private final HashSet logOnInfoLevel = new HashSet<>(); @Override public String getExternalCodeFromInternal(String internalCode) { return statusMessager.mapInternalErrorToExternalError(internalCode); } @Override public IHandleData createHandleData(Throwable throwable, IRequest protocolRequest) throws EaafException { String internalErrorId = extractInternalErrorCode(throwable); return HandleData.builder() .throwable(throwable) .internalErrorCode(internalErrorId) .actionType(ActionType.NO_TICKET) .logLevel(logOnInfoLevel.contains(internalErrorId) ? LogLevel.INFO : LogLevel.WARN) .build(); } @Override public void displayErrorData(ModifyableGuiBuilderConfiguration c, IHandleData errorData, HttpServletRequest httpReq) throws EaafException { log.trace("Do nothing because Tickets are not supported by: {}", DefaultErrorService.class.getName()); } private String extractInternalErrorCode(Throwable throwable) { Throwable originalException; if (throwable instanceof TaskExecutionException && ((TaskExecutionException) throwable).getOriginalException() != null) { originalException = ((TaskExecutionException) throwable).getOriginalException(); } else { originalException = throwable; } if (!(originalException instanceof EaafException)) { return IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC; } else { return ((EaafException) originalException).getErrorId(); } } @PostConstruct private void initialize() throws EaafException { log.info("initErrorTicketService"); logOnInfoLevel.addAll(KeyValueUtils.getListOfCsvValues( basicConfig.getBasicConfiguration(CONFIG_PROP_LOGGER_ON_INFO_LEVEL))); log.info("Set errorCodes={} to LogLevel:INFO", String.join(",", logOnInfoLevel)); } @Builder static class HandleData implements IHandleData { @Getter private String errorIdTokenForRedirect; @Getter private final Throwable throwable; @Getter private String internalErrorCode; @Getter private ActionType actionType; @Getter private LogLevel logLevel; @Override public Map getAdditionalGuiModelElements() { return Collections.emptyMap(); } public String getPreFormatedErrorMessage() { return MessageFormat.format(TECH_LOG_MSG, internalErrorCode, throwable.getMessage()); } } }