Skip to content

Commit e39147b

Browse files
authored
chore: better error management (#12)
* chore: better error management * chore: handle error when target.Len == 0
1 parent 2b7327b commit e39147b

File tree

5 files changed

+24
-31
lines changed

5 files changed

+24
-31
lines changed

pkg/jsonpath/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (p *JSONPath) parse() error {
5555
}
5656

5757
func (p *JSONPath) parseFailure(target *token.TokenInfo, msg string) error {
58-
return errors.New(p.tokenizer.ErrorTokenString(target, msg))
58+
return errors.New(p.tokenizer.ErrorString(target, msg))
5959
}
6060

6161
// peek returns true if the upcoming token matches the given token type.

pkg/jsonpath/token/token.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ func (t Tokenizer) ErrorString(target *TokenInfo, msg string) string {
315315

316316
// Write the caret symbol pointing to the target token
317317
errorBuilder.WriteString(spaces)
318-
dots := strings.Repeat(".", target.Len-1)
318+
dots := ""
319+
if target.Len > 0 {
320+
dots = strings.Repeat(".", target.Len-1)
321+
}
319322
errorBuilder.WriteString("^" + dots + "\n")
320323

321324
return errorBuilder.String()

web/src/Playground.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ function Playground() {
7070
const [shareUrl, setShareUrl] = useState("");
7171
const [shareUrlLoading, setShareUrlLoading] = useState(false);
7272
const isSmallScreen = useMediaQuery("(max-width: 768px)");
73+
const clearError = useCallback(() => {
74+
setError("");
75+
}, []);
7376
const defaultLayout = useMemo(
7477
() => (isSmallScreen ? [20, 60, 20] : [30, 40, 30]),
7578
[],
@@ -313,7 +316,7 @@ function Playground() {
313316
onClick={getShareUrl}
314317
disabled={shareUrlLoading}
315318
>
316-
Share
319+
Short URL
317320
</Button>
318321
<div className="flex items-center gap-x-2 grow">
319322
{shareUrl ? <CopyButton value={shareUrl} /> : null}
@@ -324,7 +327,21 @@ function Playground() {
324327
</div>
325328
</div>
326329
</div>
327-
{error && <Alert variant={"error"}>{error}</Alert>}
330+
{error && (
331+
<Alert onDismiss={clearError} variant={"error"}>
332+
{error.split("\n").length > 1 ? (
333+
<>
334+
{error.split("\n")[0]}
335+
<br />
336+
<div className="text-left whitespace-pre">
337+
<pre>{error.split("\n").slice(1).join("\n")}</pre>
338+
</div>
339+
</>
340+
) : (
341+
error
342+
)}
343+
</Alert>
344+
)}
328345
<div
329346
style={{
330347
display: "flex",

web/src/assets/wasm/lib.wasm

-6.4 KB
Binary file not shown.

web/src/index.css

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,6 @@ h1 {
3636
}
3737

3838
button {
39-
border-radius: 8px;
4039
border: 1px solid transparent;
41-
padding: 0.6em 1.2em;
42-
font-size: 1em;
43-
font-weight: 500;
44-
font-family: inherit;
45-
background-color: #1a1a1a;
46-
cursor: pointer;
4740
transition: border-color 0.25s;
4841
}
49-
button:hover {
50-
border-color: #646cff;
51-
}
52-
button:focus,
53-
button:focus-visible {
54-
outline: 4px auto -webkit-focus-ring-color;
55-
}
56-
57-
@media (prefers-color-scheme: light) {
58-
:root {
59-
color: #213547;
60-
background-color: #ffffff;
61-
}
62-
a:hover {
63-
color: #747bff;
64-
}
65-
button {
66-
background-color: #f9f9f9;
67-
}
68-
}

0 commit comments

Comments
 (0)