[ARIES-1922] Fix effect handling

If any exception happens in any of the onAddingBefore or onAddingAfter
points the effects need to be undone. onAddingAfter must take into
account the effects of the language.
diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
index 4fc58f0..9c5526b 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/OSGi.java
@@ -617,36 +617,39 @@
 					try {
 						Runnable terminator = op.publish(t);
 
+						OSGiResult result = () -> {
+							try {
+								onRemovedBefore.accept(t);
+							}
+							catch (Exception e) {
+								//TODO: logging
+							}
+
+							try {
+								terminator.run();
+							}
+							catch (Exception e) {
+								//TODO: logging
+							}
+
+							try {
+								onRemovedAfter.accept(t);
+							}
+							catch (Exception e) {
+								//TODO: logging
+							}
+						};
+
 						try {
 							onAddedAfter.accept(t);
 						}
 						catch (Exception e) {
-							//TODO: logging
+							result.run();
+
+							throw e;
 						}
 
-						return
-							() -> {
-								try {
-									onRemovedBefore.accept(t);
-								}
-								catch (Exception e) {
-									//TODO: logging
-								}
-
-								try {
-									terminator.run();
-								}
-								catch (Exception e) {
-									//TODO: logging
-								}
-
-								try {
-									onRemovedAfter.accept(t);
-								}
-								catch (Exception e) {
-									//TODO: logging
-								}
-							};
+						return result;
 					}
 					catch (Exception e) {
 						try {
diff --git a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
index f243e73..ec95355 100644
--- a/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
+++ b/component-dsl/src/main/java/org/apache/aries/component/dsl/internal/EffectsOSGi.java
@@ -17,6 +17,8 @@
 
 package org.apache.aries.component.dsl.internal;
 
+import org.apache.aries.component.dsl.OSGiResult;
+
 /**
  * @author Carlos Sierra Andrés
  */
@@ -32,36 +34,39 @@
             try {
                 Runnable terminator = op.publish(null);
 
+                OSGiResult result = () -> {
+                    try {
+                        onRemovingBefore.run();
+                    }
+                    catch (Exception e) {
+                        //TODO: logging
+                    }
+
+                    try {
+                        terminator.run();
+                    }
+                    catch (Exception e) {
+                        //TODO: logging
+                    }
+
+                    try {
+                        onRemovingAfter.run();
+                    }
+                    catch (Exception e) {
+                        //TODO: logging
+                    }
+                };
+
                 try {
                     onAddingAfter.run();
                 }
                 catch (Exception e) {
-                    //TODO: logging
+                    result.run();
+
+                    throw e;
                 }
 
-                return new OSGiResultImpl(
-                    () -> {
-                        try {
-                            onRemovingBefore.run();
-                        }
-                        catch (Exception e) {
-                            //TODO: logging
-                        }
-
-                        try {
-                            terminator.run();
-                        }
-                        catch (Exception e) {
-                            //TODO: logging
-                        }
-
-                        try {
-                            onRemovingAfter.run();
-                        }
-                        catch (Exception e) {
-                            //TODO: logging
-                        }
-                 });
+                return new OSGiResultImpl(result);
             }
             catch (Exception e) {
                 try {
diff --git a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
index a2cf489..c8ad473 100644
--- a/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
+++ b/itests/src/main/java/org/apache/aries/component/dsl/test/DSLTest.java
@@ -268,6 +268,31 @@
     }
 
     @Test
+    public void testEffectsErrorOnAddedAfter() {
+        ArrayList<Object> effects = new ArrayList<>();
+
+        OSGi<Integer> program = just(Arrays.asList(1, 2, 3, 4)).
+            recoverWith((__, e) -> nothing()).
+            effects(
+            effects::add,
+            i -> {
+                if (i % 2 == 0) {
+                    throw new RuntimeException();
+                }
+            },
+            __ -> {},
+            effects::remove
+        );
+
+        try (OSGiResult result = program.run(bundleContext)) {
+            assertEquals(Arrays.asList(1, 3), effects);
+        }
+
+        assertEquals(Collections.emptyList(), effects);
+    }
+
+
+    @Test
     public void testCoalesce() {
         ProbeImpl<String> program1 = new ProbeImpl<>();
         ProbeImpl<String> program2 = new ProbeImpl<>();