package fr.insee.pogues.transforms;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PipeLine {


    private String output;
    private List<Runnable> transforms = new ArrayList<>();

    public PipeLine from(InputStream input) throws Exception {
        try {
            output = IOUtils.toString(input, Charset.forName("UTF-8"));
            return this;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public PipeLine from(String input) {
        this.output = input;
        return this;
    }

    public PipeLine map(Transform<String, String> t, Map<String, Object> params) {
        transforms.add(() -> {
            try {
                output = t.apply(output, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return this;
    }


    public String transform() {
        for (Runnable t : transforms) {
            t.run();
        }
        return output;
    }


    @FunctionalInterface
    public interface Transform<I, O> {
        O apply(I i, Map<String, Object> params) throws Exception;
    }

}



@@ -557,41 +557,8 @@
 ) %7B%0A
-            e.printStackTrace();%0A
     
@@ -769,16 +769,33 @@
  params)
+ throws Exception
  %7B%0A     
@@ -944,26 +944,164 @@
     
-e.printStackTrace(
+throw new RuntimeException(%0A                        String.format(%22Exception occured while executing mapping function: %25s%22, e.getMessage())%0A                
 );%0A 
@@ -1183,16 +1183,33 @@
 nsform()
+ throws Exception
  %7B%0A     
@@ -1260,15 +1260,110 @@
    t
-.run();
+ry %7B%0A                t.run();%0A            %7D catch(Exception e)%7B%0A                throw e;%0A            %7D
 %0A   

Enhance exception handling in transformation pipeline