PackagerErrorHandler.java

package esa.bscs.pds4.packager.cli;

import java.lang.Thread.UncaughtExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanCreationException;

import esa.bscs.pds4.packager.exception.ClassificationException;
import esa.bscs.pds4.packager.exception.PackagerException;
import esa.bscs.pds4.packager.exception.PackagerMessage;
import esa.bscs.pds4.validate.exception.UnknownDictionaryStackException;
import esa.bscs.pds4.validate.exception.ValidationException;

public class PackagerErrorHandler implements UncaughtExceptionHandler {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(PackagerErrorHandler.class);
    
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        
        if(BeanCreationException.class.equals(e.getClass()) && UnknownDictionaryStackException.class.equals(((BeanCreationException) e).getRootCause().getClass())) {
            // Check if initialization failed because the dictionary stack id is not in the list            
            LOGGER.error(((BeanCreationException) e).getRootCause().getMessage());
        
        } else if(ClassificationException.class.equals(e.getClass())) {
            // Classification failed
            final ClassificationException exception = (ClassificationException) e;
            
            for(String message : exception.getMessages()) {
                LOGGER.error(message);
            }        
        
        } else if(PackagerException.class.equals(e.getClass())) {
            // Packager raised issues
            final PackagerException exception = (PackagerException) e;
            
            for(PackagerMessage message : exception.getMessages()) {
                LOGGER.error(message.getContent());
            }
        
        } else if(ValidationException.class.equals(e.getClass())) {
            // Validation failed
            LOGGER.error("Could not generate delivery because of validation issues.");
        
        } else {
            // Technical error
            LOGGER.error("Technical error: ", e);        
        }
    }
}