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; background-color: #212121;
} }
.hint {
color: #9c9c9c;
}
.calculator { .calculator {
width: 850px; width: 850px;
height: 350px; height: 350px;
@ -33,7 +37,12 @@
<body> <body>
<section id="wrapper"> <section id="wrapper">
<div class="calculator"> <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> </div>
</section> </section>
</body> </body>

View File

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