Skip to content

Commit

Permalink
Merge pull request #7 from ia7ck/error-message
Browse files Browse the repository at this point in the history
パース失敗時のメッセージを表示
  • Loading branch information
ia7ck authored Oct 21, 2024
2 parents 8205fcb + 81a8ebf commit 9546643
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 14 additions & 4 deletions app/graph/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export default function Graph() {
text: string;
option: { indexing: Indexing };
};
// parse error
type Error = {
line: number;
message: string;
};

type Element =
| {
Expand All @@ -42,7 +47,7 @@ export default function Graph() {
data: { n: 3, edges: [{ from: 1, to: 2 }] },
params: { text: graphText, option: { indexing } },
});
const [invalid, setInvalid] = useState(false);
const [error, setError] = useState<null | Error>(null);

const handleTextAreaChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const value = e.target.value;
Expand All @@ -64,9 +69,9 @@ export default function Graph() {
const graph = parseGraph(text, { indexStart: option.indexing });
if (graph.ok) {
setGraph({ data: graph.data, params: { text, option } });
setInvalid(false);
setError(null);
} else {
setInvalid(true);
setError(graph.error);
}
};

Expand Down Expand Up @@ -106,12 +111,17 @@ export default function Graph() {
rows={4}
className={clsx(
"block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6",
invalid && "text-red-900 ring-red-300 focus:ring-red-500",
error && "text-red-900 ring-red-300 focus:ring-red-500",
)}
value={graphText}
onChange={handleTextAreaChange}
/>
</div>
{error && (
<div className="text-sm text-red-700">
line {error.line + 1}: {error.message}
</div>
)}
<VSpace size="M" />
<fieldset>
<legend className="text-sm font-semibold leading-6 text-gray-900">
Expand Down
4 changes: 3 additions & 1 deletion app/graph/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ export function parseGraph(
const [from, to, weight] = lines[i].trim().split(" ");
const e = Edge.safeParse({ from, to });
if (e.error) {
const lb = option.indexStart === "0-indexed" ? 0 : 1;
const ub = option.indexStart === "0-indexed" ? n.data - 1 : n.data;
return {
ok: false,
error: {
field: "edge",
line: i,
message: `Invalid edge: "${lines[i].trim()}". It must be space-separated integers.`,
message: `Invalid edge: "${lines[i].trim()}". It must be space-separated integers between ${lb} and ${ub}.`,
},
};
}
Expand Down

0 comments on commit 9546643

Please sign in to comment.