ajoute un prototype d'interface pour les warnings du moteur

pull/1056/head
Johan Girod 2020-05-26 16:12:16 +02:00
parent 2351726034
commit b175945d78
4 changed files with 30 additions and 19 deletions

View File

@ -35,7 +35,8 @@
"js-yaml": "^3.13.1",
"moo": "^0.5.0",
"nearley": "^2.19.0",
"publicodes": "^1.0.0-beta.3",
"publicodes": "^1.0.0-beta.4",
"focus-trap-react": "^3.1.2",
"ramda": "^0.27.0",
"react": "^16.13.1",
"react-color": "^2.14.0",

View File

@ -132,14 +132,21 @@ export const Results = ({ onClickShare, rules }: ResultsProps) => {
setCurrentTarget(last(targets))
}
})
// EN ATTENDANT d'AVOIR une meilleure gestion d'erreur, on va mocker
// console.warn
const warnings: string[] = []
const originalWarn = console.warn
console.warn = (warning: string) => warnings.push(warning)
return (
<>
{engine.getWarnings().map(warning => (
<div
css={`
background: lightyellow;
padding: 20px;
border-radius: 5px;
`}
key={warning}
>
{nl2br(warning)}
</div>
))}
<div
css={`
display: flex;
@ -176,18 +183,7 @@ export const Results = ({ onClickShare, rules }: ResultsProps) => {
</button>
</div>
</div>
{warnings.map(warning => (
<div
css={`
background: lightyellow;
padding: 20px;
border-radius: 5px;
`}
key={warning}
>
{nl2br(warning)}
</div>
))}
<ErrorBoundary>
<Documentation
engine={engine}

View File

@ -1,6 +1,6 @@
{
"name": "publicodes",
"version": "1.0.0-beta.3",
"version": "1.0.0-beta.4",
"description": "A declarative language for encoding public algorithm",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",

View File

@ -45,6 +45,7 @@ export default class Engine<Names extends string> {
defaultValues: Situation<Names>
situation: Situation<Names> = {}
cache: Cache
warnings: Array<string> = []
cacheWithoutDefault: Cache
constructor(rules: string | Rules<Names> | ParsedRules<Names>) {
@ -81,6 +82,14 @@ export default class Engine<Names extends string> {
context: string,
useDefaultValues = true
): EvaluatedNode<Names> {
// EN ATTENDANT d'AVOIR une meilleure gestion d'erreur, on va mocker
// console.warn
const warnings: string[] = []
const originalWarn = console.warn
console.warn = (warning: string) => {
this.warnings.push(warning)
originalWarn(warning)
}
const result = simplifyNodeUnit(
evaluateNode(
useDefaultValues ? this.cache : this.cacheWithoutDefault,
@ -93,6 +102,7 @@ export default class Engine<Names extends string> {
)(expression)
)
)
console.warn = originalWarn
if (Object.keys(result.defaultValue?.missingVariable ?? {}).length) {
throw evaluationError(
@ -161,6 +171,10 @@ export default class Engine<Names extends string> {
)
}
getWarnings() {
return this.warnings
}
inversionFail(): boolean {
return !!this.cache._meta.inversionFail
}