Fixed kalk_web error handling

This commit is contained in:
PaddiM8 2021-01-04 20:47:33 +01:00
parent ced63a84de
commit 27f314e57b
2 changed files with 19 additions and 9 deletions

View File

@ -16,6 +16,10 @@
background-color: #212121;
}
.hint {
color: #9c9c9c;
}
.calculator {
width: 850px;
height: 350px;
@ -33,7 +37,12 @@
<body>
<section id="wrapper">
<div class="calculator">
<kalk-calculator backgroundColor="#2e2e2e" />
<kalk-calculator>
<console-line>kalk</console-line>
<console-line>
<span class="hint">Type 'help' for instructions.</span>
</console-line>
</kalk-calculator>
</div>
</section>
</body>

View File

@ -26,20 +26,20 @@
function calculate(
kalk: Kalk,
input: string
): [result: string, error: string] {
): [result: string, success: boolean] {
try {
if (!kalkContext) kalkContext = new kalk.Context();
const result = kalkContext.evaluate(input) ?? "";
if (result) {
const sciNot = result.toScientificNotation();
if (sciNot.exponent > 7 || sciNot.exponent < -6) {
return [sciNot.toString(), null];
return [sciNot.toString(), true];
}
}
return [result.toString(), null];
return [result?.toString(), true];
} catch (err) {
return [undefined, err];
return [err, false];
}
}
@ -55,10 +55,10 @@
href="https://kalk.netlify.app/#usage"
target="blank">Link to usage guide</a>`;
} else {
const [result, err] = calculate(kalk, input);
output =
highlight(result)[0] ??
`<span style="color: ${errorColor}">${err}</span>`;
const [result, success] = calculate(kalk, input);
output = success
? highlight(result)[0]
: `<span style="color: ${errorColor}">${result}</span>`;
}
outputLines = output
@ -178,6 +178,7 @@
}
function highlight(input: string): [string, number] {
if (!input) return ["", 0];
let result = input;
let offset = 0;
result = result.replaceAll(