Comparing version 0.8.4 to 0.8.5
@@ -21,9 +21,6 @@ 'use strict'; | ||
if (onErrorStrategy === false) { | ||
throw new Error(_Element2.default.errors.STOP_PROCESSING); | ||
} else if (onErrorStrategy === true) { | ||
if (onErrorStrategy === true) { | ||
throw new Error(_Element2.default.errors.CONTINUE_PROCESSING); | ||
} else { | ||
// swallowing the error | ||
} | ||
throw new Error(_Element2.default.errors.STOP_PROCESSING); | ||
} else { | ||
@@ -30,0 +27,0 @@ throw error; |
{ | ||
"name": "actml", | ||
"version": "0.8.4", | ||
"version": "0.8.5", | ||
"description": "Like jsx but for your business logic", | ||
@@ -16,4 +16,5 @@ "main": "lib", | ||
"keywords": [ | ||
"dialect", | ||
"jsx" | ||
"actml", | ||
"jsx", | ||
"react" | ||
], | ||
@@ -20,0 +21,0 @@ "author": "Krasimir Tsonev", |
@@ -343,4 +343,84 @@ # <ActML /> :rocket: <!-- omit in toc --> | ||
... | ||
Because `run` returns a promise we can just `catch` the error at a _global_ level: | ||
```js | ||
const Problem = function() { | ||
return iDontExist; // throws an error "iDontExist is not defined" | ||
}; | ||
const App = function() {}; | ||
run( | ||
<App> | ||
<Problem /> | ||
</App> | ||
).catch(error => { | ||
console.log('Ops, an error: ', error.message); | ||
// Ops, an error: iDontExist is not defined | ||
}); | ||
``` | ||
That's all fine but it is not really practical. What we may want is to handle the error inside our ActML. In such cases we have the special `onError` prop. It accepts another ActML element which receives the error as a prop. | ||
```js | ||
const Problem = function() { | ||
return iDontExist; | ||
}; | ||
const App = function() {}; | ||
const HandleError = ({ error }) => console.log(error.message); // logs "iDontExist is not defined" | ||
run( | ||
<App> | ||
<Problem onError={ <HandleError /> } /> | ||
</App> | ||
); | ||
``` | ||
ActML stops the execution of the current logic. However, if our handler returns `true` it continues. For example: | ||
```js | ||
const Problem = function() { | ||
return iDontExist; | ||
}; | ||
const App = function() {}; | ||
const HandleError = () => true; | ||
const AfterError = () => console.log('I am still here :)'); | ||
run( | ||
<App exports='answer'> | ||
<Problem onError={ <HandleError /> } /> | ||
<AfterError /> | ||
</App> | ||
); | ||
// outputs "I am still here :)" even tho there's an error | ||
``` | ||
And by stopping the execution we mean only the current branch. For example: | ||
```js | ||
const Problem = function() { | ||
return iDontExist; | ||
}; | ||
const App = function() {}; | ||
const Wrapper = function() {}; | ||
const HandleError = () => {}; | ||
const Z = () => console.log('Z'); | ||
const B = () => console.log('B'); | ||
const C = () => console.log('C'); | ||
await run( | ||
<App exports='answer'> | ||
<Wrapper> | ||
<Problem onError={ <HandleError /> } /> | ||
<Z /> | ||
</Wrapper> | ||
<Wrapper> | ||
<B /> | ||
<C /> | ||
</Wrapper> | ||
</App> | ||
); | ||
``` | ||
We will see `B` followed by `C` but not `Z` because there's an error at that level. | ||
## Examples | ||
@@ -347,0 +427,0 @@ |
384608
429
1014