Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show the error in the right box #150

Merged
merged 5 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/components/editor/hooks/useRunTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import React from 'react';
import { DefaultRoleManager, newEnforcer, newModel, StringAdapter, Util } from 'casbin';
import { newEnforceContext } from '@/app/components/editor/hooks/useSetupEnforceContext';
import { setError } from '@/app/utils/errorManager';

interface RunTestProps {
model: string;
Expand Down Expand Up @@ -178,11 +179,15 @@ async function enforcer(props: RunTestProps) {

const stopTime = performance.now();

setError(null);

props.onResponse(<div>{'Done in ' + (stopTime - startTime).toFixed(2) + 'ms'}</div>);
props.onResponse(result);
} catch (e) {
props.onResponse(<div>{(e as any).message}</div>);
const errorMessage = (e as any).message;
props.onResponse(<div>{errorMessage}</div>);
props.onResponse([]);
setError(errorMessage);
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { extractPageContent } from '../../utils/contentExtractor';
import { buttonPlugin } from './ButtonPlugin';
import { useLang } from '@/app/context/LangContext';
import LanguageMenu from '@/app/components/LanguageMenu';
import { linter } from '@codemirror/lint';
import { casbinLinter } from '@/app/utils/casbinLinter';

export const EditorScreen = () => {
const {
Expand Down Expand Up @@ -238,6 +240,7 @@ export const EditorScreen = () => {
indentUnit.of(' '),
EditorView.lineWrapping,
buttonPlugin(openDrawerWithMessage, extractContent, 'model'),
linter(casbinLinter),
]}
className={'function flex-grow h-[300px]'}
value={modelText}
Expand Down
31 changes: 31 additions & 0 deletions app/utils/casbinLinter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Diagnostic } from '@codemirror/lint';
import { EditorView } from '@codemirror/view';
import { getError } from './errorManager';

export const casbinLinter = (view: EditorView): Diagnostic[] => {
const diagnostics: Diagnostic[] = [];

const runTestError = getError();
if (runTestError) {
const lineMatch = runTestError.match(/line (\d+)/);
if (lineMatch) {
const errorLine = parseInt(lineMatch[1], 10);
const line = view.state.doc.line(errorLine);
diagnostics.push({
from: line.from,
to: line.to,
severity: 'error',
message: runTestError,
});
} else {
diagnostics.push({
from: 0,
to: view.state.doc.length,
severity: 'error',
message: runTestError,
});
}
}

return diagnostics;
};
7 changes: 7 additions & 0 deletions app/utils/errorManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let currentError: string | null = null;

export const setError = (error: string | null) => {
currentError = error;
};

export const getError = () => {return currentError};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@codemirror/lang-javascript": "^6.2.1",
"@codemirror/language": "^6.10.1",
"@codemirror/legacy-modes": "^6.3.3",
"@codemirror/lint": "^6.8.1",
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.24.1",
"@lezer/highlight": "^1.2.0",
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@
"@codemirror/view" "^6.0.0"
crelt "^1.0.5"

"@codemirror/lint@^6.8.1":
version "6.8.1"
resolved "https://registry.yarnpkg.com/@codemirror/lint/-/lint-6.8.1.tgz#6427848815baaf68c08e98c7673b804d3d8c0e7f"
integrity sha512-IZ0Y7S4/bpaunwggW2jYqwLuHj0QtESf5xcROewY6+lDNwZ/NzvR4t+vpYgg9m7V8UXLPYqG+lu3DF470E5Oxg==
dependencies:
"@codemirror/state" "^6.0.0"
"@codemirror/view" "^6.0.0"
crelt "^1.0.5"

"@codemirror/search@^6.0.0":
version "6.5.6"
resolved "https://registry.yarnpkg.com/@codemirror/search/-/search-6.5.6.tgz#8f858b9e678d675869112e475f082d1e8488db93"
Expand Down
Loading